简体   繁体   English

如何获取媒体uri的绝对文件路径

[英]How to get absolute file path for media uri

I have MediaScannerConnectionClient returning me path and uri like below 我有MediaScannerConnectionClient返回我的路径和URI,如下所示

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

How to find absolute path for the uri ? 如何找到uri的绝对路径? I tried below and failed and line Log.d(TAG,"after new File"); 我在下面尝试并失败,并在行Log.d(TAG,“在新文件之后”);
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. 默认情况下,使用MediaColumn表示MediaStore中的所有媒体内容,其DATA列包含数据流-绝对文件路径。 So, you can get an absolute path of any media stored in MediaStore like this: 因此,您可以获得存储在MediaStore中的任何媒体的绝对路径,如下所示:

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 我在从媒体存储获取uri的文件名和路径的答案中使用了getRealPathFromURI(uri)方法

to convert my returned camera uri of 转换我返回的相机uri

content://media/external/images/media/35733 内容:// media / external / images / media / 35733

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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