Skip to content

Instantly share code, notes, and snippets.

@MichaelPolla
Created July 19, 2016 12:34
Show Gist options
  • Save MichaelPolla/2438f10a42bf27187762722e3034ca09 to your computer and use it in GitHub Desktop.
Save MichaelPolla/2438f10a42bf27187762722e3034ca09 to your computer and use it in GitHub Desktop.
Get the path of a file from its URI
/* Used for example when you get an Uri within the onActivityResult :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Process the result if it's OK (user finished the action) and the request code matches.
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
imageUri = data.getData(); // <==== THERE
//...
*/
public String getRealPathFromUri( Uri contentUri) {
Cursor cursor = null;
try {
String[] projection = {MediaStore.Images.Media.DATA};
cursor = this.getContentResolver().query(contentUri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
Log.i("Log", "Uri : " + cursor.getString(column_index));
return cursor.getString(column_index);
} finally {
if (cursor != null)
cursor.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment