1

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");
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
Manju
  • 677
  • 5
  • 13
  • 27

2 Answers2

4

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();
Chansuk
  • 792
  • 4
  • 7
  • Hi Chansuk, Thanks for your response but your solution is not working for me. I get a string and a uri in call back after scan completed. I tried using your code to get absolute path but it is failing. path: /sdcard/, uri: content://media/external/images/media/7950 – Manju Aug 23 '13 at 07:35
  • What's the result or error? In your case I think 'Cursor c = getContentResolver().query(uri,null,null,nul,null);'is OK, for more detailed information you may refer some similar question - [question #3401579](http://stackoverflow.com/questions/3401579/get-filename-and-path-from-uri-from-mediastore?rq=1) – Chansuk Aug 23 '13 at 07:47
  • Hi Chansuk, Thanks for your response but your solution is not working for me. I get a string and a uri in call back after scan completed. I tried using your code to get absolute path but it is failing. result ==> /sdcard/ whereas my uri.toString() ==> content://media/external/images/media/7950 – Manju Aug 23 '13 at 09:39
  • Hi Chansuk, i tried another link in your response but no luck, i keep getting result as /sdcard/ – Manju Aug 23 '13 at 09:52
2

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

Community
  • 1
  • 1
chughes
  • 81
  • 5