Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContentResolver - how to get file name from Uri

I call startActivityForResult with Intent ACTION_GET_CONTENT. Some app returns me data with this Uri:

content://media/external/images/media/18122

I don't know if it is image or video or some custom content. How do I use ContentResolver to get the actual file name or content title from this Uri?

like image 549
Pointer Null Avatar asked Nov 07 '12 17:11

Pointer Null


People also ask

How do you get real path from URI?

Uri selectedImageURI = data. getData(); String realPath = getRealPathFromURI(selectedImageURI); 1) In fragment you can use function like this way.. 2) And activity you can use function like that.

How do you find the absolute path of URI?

Uri uri = data. getData(); File file = new File(uri. getPath());//create path from uri final String[] split = file. getPath().

What is content URI?

A content URI is a URI that identifies data in a provider. Content URIs include the symbolic name of the entire provider (its authority) and a name that points to a table (a path). When you call a client method to access a table in a provider, the content URI for the table is one of the arguments.

How can I get file type in Android?

Retrieve a file's MIME type To get the data type of a shared file given its content URI, the client app calls ContentResolver. getType() . This method returns the file's MIME type. By default, a FileProvider determines the file's MIME type from its filename extension.


2 Answers

@Durairaj's answer is specific to getting the path of a file. If what you're searching for is the file's actual name (since you should be using Content Resolution, at which point you'll probably get a lot of content:// URIs) you'll need to do the following:

(Code copied from Durairaj's answer and modified)

        String[] projection = {MediaStore.MediaColumns.DISPLAY_NAME};
        Cursor metaCursor = cr.query(uri, projection, null, null, null);
        if (metaCursor != null) {
            try {
                if (metaCursor.moveToFirst()) {
                    fileName = metaCursor.getString(0);
                }
            } finally {
                metaCursor.close();
            }
        }

The main piece to note here is that we're using MediaStore.MediaColumns.DISPLAY_NAME, which returns the actual name of the content. You might also try MediaStore.MediaColumns.TITLE, as I'm not sure what the difference is.

like image 117
Navarr Avatar answered Sep 21 '22 07:09

Navarr


You can get file name from this code, or any other field by modifying the projection

String[] projection = {MediaStore.MediaColumns.DATA};

ContentResolver cr = getApplicationContext().getContentResolver();
Cursor metaCursor = cr.query(uri, projection, null, null, null);
if (metaCursor != null) {
    try {
        if (metaCursor.moveToFirst()) {
            path = metaCursor.getString(0);
        }
    } finally {
        metaCursor.close();
    }
}
return path;
like image 43
Durairaj Packirisamy Avatar answered Sep 24 '22 07:09

Durairaj Packirisamy