Álvaro Álvaro - 1 year ago 111
Android Question

Get real path from URI, Android KitKat new storage access framework

Before new gallery access in KitKat I got my real path in sdcard with this method

public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
return cursor.getString(column_index);

Now, the Intent.ACTION_GET_CONTENT return different data:





How could I manage to obtain the real path in sdcard?

Answer Source

Note: This answer addresses part of the problem. For a complete solution (in the form of a library), look at user Paul Burke's answer below.

You could use the Uri to obtain document id, and then query either MediaStore.Images.Media.EXTERNAL_CONTENT_URI or MediaStore.Images.Media.INTERNAL_CONTENT_URI (depending on the SD card situation).

To get document id:

// Will return "image:x*"
String wholeID = DocumentsContract.getDocumentId(uriThatYouCurrentlyHave);

// Split at colon, use second item in the array
String id = wholeID.split(":")[1];

String[] column = { MediaStore.Images.Media.DATA };     

// where id is equal to             
String sel = MediaStore.Images.Media._ID + "=?";

Cursor cursor = getContentResolver().
                          column, sel, new String[]{ id }, null);

String filePath = "";

int columnIndex = cursor.getColumnIndex(column[0]);

if (cursor.moveToFirst()) {
    filePath = cursor.getString(columnIndex);


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download