简体   繁体   中英

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");

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM