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?
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.
Uri uri = data. getData(); File file = new File(uri. getPath());//create path from uri final String[] split = file. getPath().
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.
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.
@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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With