Before KitKat (or before the new Gallery) the Intent.ACTION_GET_CONTENT
returned a URI like this
content://media/external/images/media/3951.
Using the ContentResolver
and quering for
MediaStore.Images.Media.DATA
returned the file URL.
In KitKat however the Gallery returns a URI (via "Last") like this:
content://com.android.providers.media.documents/document/image:3951
How do I handle this?
If anyone's interested, I made a working Kotlin version for
ACTION_GET_CONTENT
:This answer is from m3n0R on question android get real path by Uri.getPath() and I claim no credit. I just thought that people who haven't solved this issue yet could use this.
For those who is still using @Paul Burke's code with Android SDK version 23 and above, if your project met the error saying that you are missing EXTERNAL_PERMISSION, and you are very sure you have already added user-permission in your AndroidManifest.xml file. That's because you may in Android API 23 or above and Google make it necessary to guarantee permission again while you make the action to access the file in runtime.
That means: If your SDK version is 23 or above, you are asked for READ & WRITE permission while you are selecting the picture file and want to know the URI of it.
And following is my code, in addition to Paul Burke's solution. I add these code and my project start to work fine.
And in your activity&fragment where you are asking for the URI:
In my case, CompatUtils.java is where I define the verifyStoragePermissions method (as static type so I can call it within other activity).
Also it should make more sense if you make an if state first to see whether the current SDK version is above 23 or not before you call the verifyStoragePermissions method.
This Android library handles the case changes in KitKat(including the oldere versions - 2.1+):
https://github.com/iPaulPro/aFileChooser
Use the
String path = FileUtils.getPath(context, uri)
to convert the returned Uri to a path string useable on all OS version. See more about it here: https://stackoverflow.com/a/20559175/860488I've combine multiple answers into one working solution that results with file path
Mime type is irrelevant for the example purpose.
Handling result
FilePickUtils
Please try to avoid using takePersistableUriPermission method because it raised runtime exception for me. /** * Select from gallery. */
OnActivity for result to handle the image data:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {