Skip to content Skip to sidebar Skip to footer

How To Convert Content:// Uri Into Actual File Path?

how can I get the actual file path on the SD card where a content:// uri is pointing for an image?

Solution 1:

I've adapted the code which @hooked82 linked to:

protectedStringconvertMediaUriToPath(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;
}

Solution 2:

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:

longid= ContentUris.parseId(Uri.parse(contentUri));
Cursorcursor= getContentResolver()
            .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    newString[]{ MediaStore.Images.Media.DATA },
                    MediaStore.Images.Media._ID + " = ?", newString[]{ Long.toString(id) }, 
                    null);
if (cursor.moveToNext()) {
    path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();

Post a Comment for "How To Convert Content:// Uri Into Actual File Path?"