Android Notes 73: How to get Real Path from Uri?

Kuray Ogun
FreakyCoder Software Blog

--

Updated: Sept 21, 2020

If you had a headache to get the real path for your file like me, don’t worry! I have your medicine :)

Here is how to get the real path of the file from Uri via MediaStore:

Markdown:

private String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e) {
Log.e(TAG, "getRealPathFromURI Exception : " + e.toString());
return "";
} finally {
if (cursor != null) {
cursor.close();
}
}
}

Gist:

If you have any question, ask me :)

Have fun!

--

--