This question already has an answer here:
Before the new gallery access in Android 4.4 (KitKat) I got my real path on the SD card with this method:
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
startManagingCursor(cursor);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Now, the Intent.ACTION_GET_CONTENT return different data:
Before:
content://media/external/images/media/62
Now:
content://com.android.providers.media.documents/document/image:62
How could I manage to obtain the real path on the SD card?
Try this:
Use the following in the onActivityResult:
This will get the file path from the MediaProvider, DownloadsProvider, and ExternalStorageProvider, while falling back to the unofficial ContentProvider method you mention.
These are taken from my open source library, aFileChooser.
This answer is based on your somewhat vague description. I assume that you fired an intent with action:
Intent.ACTION_GET_CONTENT
And now you get
content://com.android.providers.media.documents/document/image:62
back instead of the previously media provider URI, correct?On Android 4.4 (KitKat) the new DocumentsActivity gets opened when an
Intent.ACTION_GET_CONTENT
is fired thus leading to grid view (or list view) where you can pick an image, this will return the following URIs to calling context (example):content://com.android.providers.media.documents/document/image:62
(these are the URIs to the new document provider, it abstracts away the underlying data by providing generic document provider URIs to clients).You can however access both gallery and other activities responding to
Intent.ACTION_GET_CONTENT
by using the drawer in the DocumentsActivity (drag from left to right and you'll see a drawer UI with Gallery to choose from). Just as pre KitKat.If you still which to pick in DocumentsActivity class and need the file URI, you should be able to do the following (warning this is hacky!) query (with contentresolver):
content://com.android.providers.media.documents/document/image:62
URI and read the _display_name value from the cursor. This is somewhat unique name (just the filename on local files) and use that in a selection (when querying) to mediaprovider to get the correct row corresponding to this selection from here you can fetch the file URI as well.The recommended ways of accessing document provider can be found here (get an inputstream or file descriptor to read file/bitmap):
Examples of using documentprovider
We need to do the following changes/fixes in our earlier onActivityResult()'s gallery picker code to run seamlessly on Android 4.4 (KitKat) and on all other earlier versions as well.
I had the exact same problem. I need the filename so to be able to upload it to a website.
It worked for me, if I changed the intent to PICK. This was tested in AVD for Android 4.4 and in AVD for Android 2.1.
Add permission READ_EXTERNAL_STORAGE :
Change the Intent :
I did not have to change my code the get the actual path:
Note: This answer addresses part of the problem. For a complete solution (in the form of a library), look at Paul Burke's answer.
You could use the URI to obtain
document id
, and then query eitherMediaStore.Images.Media.EXTERNAL_CONTENT_URI
orMediaStore.Images.Media.INTERNAL_CONTENT_URI
(depending on the SD card situation).To get document id:
Reference: I'm not able to find the post that this solution is taken from. I wanted to ask the original poster to contribute here. Will look some more tonight.