Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert content:// Uri into actual file path?

Tags:

android

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

like image 408
hunterp Avatar asked Aug 22 '11 22:08

hunterp


People also ask

How do you find the absolute path of URI?

Uri uri = data. getData(); File file = new File(uri. getPath());//create path from uri final String[] split = file. getPath().

How do I find the file path in Kotlin?

From what I know you can find the path of a file in the internal storage with java simply by using: String path = context. getFilesDir(). getAbsolutePath(); File file = new File(path + "/filename");


1 Answers

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;
}
like image 82
Jonathon Horsman Avatar answered Oct 06 '22 21:10

Jonathon Horsman