Skip to content

Instantly share code, notes, and snippets.

@openrijal
Last active April 21, 2021 03:26
Show Gist options
  • Save openrijal/5494970 to your computer and use it in GitHub Desktop.
Save openrijal/5494970 to your computer and use it in GitHub Desktop.
Get Path from URI in Android.
/*
Input: URI -- something like content://com.example.app.provider/table2/dataset1
Output: PATH -- something like /sdcard/DCIM/123242-image.jpg
*/
public String convertMediaUriToPath(Uri uri) {
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(column_index);
cursor.close();
return path;
}
@TibsGracia
Copy link

thanks, it really helps a lot

@AqibShxhzad
Copy link

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

DATA is Now Deprecated

@nedaluof
Copy link

DATA Deprecated ... old way still useful with 29 > APIs

@developervineetjoshi
Copy link

DATA Deprecated ... old way still useful with 29 > APIs

please mention the method which can be used for all device above API level 23

@astha1818
Copy link

Please provide an alternative for DATA as it has been deprecated in APIs above 28

@developervineetjoshi
Copy link

I suggest u all to use a custom image or file picker for getting the path of file , as all this work is already done inside the library and all the errors are handled too.
For images u can checkout : pix

@abdullah-2019
Copy link

DATA is deprecated!

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