Skip to content

Instantly share code, notes, and snippets.

@akexorcist
Created June 6, 2014 08:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save akexorcist/4a01eec6e5210a779ec2 to your computer and use it in GitHub Desktop.
Save akexorcist/4a01eec6e5210a779ec2 to your computer and use it in GitHub Desktop.
Get Real Path from URI which choose from intent
// Image Media Path : /document/image:48645
// Real file Path : /storage/emulated/0/DCIM/Camera/IMG_20140606_123326982.jpg
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_GALLERY && resultCode == RESULT_OK) {
Uri uri = data.getData();
String path = getRealPathFromURI(getApplicationContext(), uri);
Log.e("Check", "URI Path : " + uri.getPath());
Log.e("Check", "Real Path : " + path);
}
}
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = getContentResolver().query(contentUri, null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":")+1);
cursor.close();
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,null
, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return path;
}
@enosh94
Copy link

enosh94 commented Jul 16, 2019

This is not working any more

@rpalmese
Copy link

This is not working any more

have you figured it out how to make it work?

@cartantino
Copy link

Hi! This should work only for images.. I am looking for a solution that works for any kind of file, do you know how to do it?

@ikhlaqmalik13
Copy link

works for me, but i don't know, if it will work on all the devices

@aadeshsethi
Copy link

@cartantino Have you found any solution for it?

@cartantino
Copy link

@cartantino Have you found any solution for it?

Hi @aadeshsethi, yeah I've found another solution.
I've found this library some times ago: https://github.com/HBiSoft/PickiT.
Give it a look, good luck,

Costantino.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment