[英]get real path from URI of type data.getData() file:///storage/emulated/0/LenovoReaper/did
My application going to ANR
when I choose file and try to get its real path.当我选择文件并尝试获取其真实路径时,我的应用程序将进入ANR
。 my我的
public static String getRealPathFromUri(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
but same thing works fine if I choose any media file like image.但如果我选择任何媒体文件(如图像),同样的事情也能正常工作。
Works fine for data.getData() = content://media/external/images/media/22555 real path
适用于data.getData() = content://media/external/images/media/22555 real path
real path is /storage/emulated/0/DCIM/20150406_210053.jpg
真实路径是/storage/emulated/0/DCIM/20150406_210053.jpg
for non media file it goes to ANR
对于非媒体文件,它会转到ANR
data.getData() file:///storage/emulated/0/LenovoReaper/did
nothing is returned and Application goes to ANR
.没有返回任何内容,应用程序转到ANR
。 Could some one suggest how to get real path of such URI
or should I just remove file://
from the data.getData()
有人可以建议如何获取此类URI
真实路径,还是我应该从data.getData()
删除file://
For opening the gallery use this code:要打开图库,请使用以下代码:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction("android.intent.action.PICK");
startActivityForResult(intent, PICK_IMAGE_REQUEST);
In onActivityResult :在 onActivityResult 中:
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null) {
String absolutePath = "file://" + getPath(this,data.getData());
}
And the getPath function:和 getPath 函数:
public static String getPath( Context context, Uri uri ) {
String result = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver( ).query( uri, proj, null, null, null );
if(cursor != null){
if ( cursor.moveToFirst( ) ) {
int column_index = cursor.getColumnIndexOrThrow( proj[0] );
result = cursor.getString( column_index );
}
cursor.close( );
}
if(result == null) {
result = "Not found";
}
Log.d(TAG, "onActivityResult: file://"+result);
return result;
}
It will give you the absolute path of the image like "file:///storage/emulated/0/LenovoReaper/did"它将为您提供图像的绝对路径,例如“file:///storage/emulated/0/LenovoReaper/did”
Could some one suggest how to get real path of such URI有人可以建议如何获取此类 URI 的真实路径
There is no "real path" other than the Uri
itself.除了Uri
本身之外,没有“真正的路径”。
should I just remove file:// from the data.getData()我应该从 data.getData() 中删除 file://
That will not do you any good.那对你没有任何好处。
A Uri
is not a File
. 一个Uri
不是一个File
。 There is no requirement for any Uri
to have to point to a file that you can access.不需要任何Uri
必须指向您可以访问的文件。 If you want to get access to the data represented by the Uri
, use methods on ContentResolver
, such as openInputStream()
.如果您想访问由Uri
表示的数据,请使用ContentResolver
方法,例如openInputStream()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.