how can I get the actual file path on the SD card where a content:// uri is pointing for an image?
Asked
Active
Viewed 1.8k times
9
-
1This Question has been already asked. http://stackoverflow.com/questions/3401579/get-filename-and-path-from-uri-from-mediastore – Shahzad Imam Jan 25 '12 at 12:32
-
Checkout this answer for your problem : http://stackoverflow.com/questions/19985286/convert-content-uri-to-actual-path-in-android-4-4/43679934#43679934 – A-Droid Tech Apr 28 '17 at 12:15
2 Answers
13
I've adapted the code which @hooked82 linked to:
protected 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;
}

Jonathon Horsman
- 2,303
- 1
- 20
- 18
-
4im finding that doing this on a nexus 5's default file picker doesnt work – JMRboosties Feb 03 '14 at 06:15
-
Ya im testing this on a Nexus 5 with a content Uri of `content://com.google.android.gallery3d.provider/picasa/item/5688442732610256002` and it doesn't work. – Etienne Lawlor Jun 10 '14 at 19:02
-
Yup that code was posted almost 3 years ago. Android has moved on a bit since then. – Jonathon Horsman Jun 12 '14 at 15:19
-
0
Content URIs have syntax content://authority/path/id, read here. Parse id from content URI and query MediaStore.Images.Media.EXTERNAL_CONTENT_URI as follows:
long id = ContentUris.parseId(Uri.parse(contentUri));
Cursor cursor = getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{ MediaStore.Images.Media.DATA },
MediaStore.Images.Media._ID + " = ?", new String[]{ Long.toString(id) },
null);
if (cursor.moveToNext()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();

Darshan Dorai
- 659
- 8
- 10