Manju
Manju

Reputation: 717

How to get absolute file path for media uri

I have MediaScannerConnectionClient returning me path and uri like below

path=/sdcard
uri= content://media/external/images/media/9834

How to find absolute path for the uri ? I tried below and failed and line Log.d(TAG,"after new File");
does not gets executed.

Looks like there is some error in executing line

new File(new URI(uri.getPath())) 

Any help is highly appreciated. -regards, Manju

File myFile=null;
try {
    myFile=new File(new URI(uri.getPath()));
    Log.d(TAG,"after new File");
} catch (URISyntaxException e) {
    e.printStackTrace();
} catch (IllegalArgumentException e){
    e.printStackTrace();
}

if(myFile!=null && myFile.exists()){
    Log.d(TAG,"file exists");
    Log.d(TAG,"FilePath: "+myFile.getAbsoluteFile());
}else{
    Log.d(TAG,"given file DOESNOT exist");

Upvotes: 2

Views: 15067

Answers (2)

chughes
chughes

Reputation: 81

I'm using the getRealPathFromURI(uri) method in the answer of Get filename and path from uri from mediastore

to convert my returned camera uri of

content://media/external/images/media/35733

to

/storage/emulated/0/DCIM/Camera/1377243365736.jpg

Upvotes: 2

Chansuk
Chansuk

Reputation: 792

As default, all media content in MediaStore represented by using MediaColumn, and its DATA column containing data stream - absolute file path. So, you can get an absolute path of any media stored in MediaStore like this:

Cursor c = getContentResolver().query(
    Uri.parse"content://media/external/images/media/1"),null,null,null,null);
c.moveToNext();
String path = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));
c.close();

Upvotes: 4

Related Questions