2

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

2 Answers 2

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(); 
Sign up to request clarification or add additional context in comments.

4 Comments

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
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
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
Hi Chansuk, i tried another link in your response but no luck, i keep getting result as /sdcard/
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

2 Comments

Hi chughes, i tried same code but always the response is /sdcard/ iam not sure what is the mistake
hi, Can you post your complete class?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.