【发布时间】:2013-07-30 09:51:55
【问题描述】:
当我从图库中选择图像时,我会通过 onActivityResult 传递的参数获取意图 Uri。执行时:new File(String_Uri_given_to_me) 并执行 File.Exists(),给我 null... 我能做什么?
【问题讨论】:
-
你能在你有空值的地方发布异常或粘贴你的代码吗?
标签: android file select gallery photo
当我从图库中选择图像时,我会通过 onActivityResult 传递的参数获取意图 Uri。执行时:new File(String_Uri_given_to_me) 并执行 File.Exists(),给我 null... 我能做什么?
【问题讨论】:
标签: android file select gallery photo
看来你可以试试: 新文件(Uri_given_to_you.getpath())
可能没问题。
【讨论】:
如果以上答案不能解决您的问题,请使用此代码
private final synchronized String getPath(Uri uri) {
String res = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, proj,
null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
【讨论】:
我很难解决这个问题。我无法获得一些图像路径(即使使用 Maxim Efivmov 代码),最后决定使用谷歌关于这个主题的文档。 https://developer.android.com/guide/topics/providers/document-provider.html
这段代码用于获取位图
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
ParcelFileDescriptor parcelFileDescriptor =
getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return image;
}
您可以使用此位图在图像视图中显示它。
【讨论】: