Android 图像选择器意图-联机存储照片的空路径
我使用一个图像选择器,允许用户从他们的图库中选择一个图像,我得到它的路径,然后将它传递到第三个库 在大多数情况下,它都可以正常工作,但是如果我从Google Photos(在线存储的图像)中获取一个图像,我会得到一个Android 图像选择器意图-联机存储照片的空路径,android,android-intent,path,photo-gallery,google-photos,Android,Android Intent,Path,Photo Gallery,Google Photos,我使用一个图像选择器,允许用户从他们的图库中选择一个图像,我得到它的路径,然后将它传递到第三个库 在大多数情况下,它都可以正常工作,但是如果我从Google Photos(在线存储的图像)中获取一个图像,我会得到一个null路径,尽管对于工作图像和非工作图像,我都会得到有效的URI 以下是我的意向电话: Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); st
null
路径,尽管对于工作图像和非工作图像,我都会得到有效的URI
以下是我的意向电话:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
下面是一个活动结果:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri = data.getData();
Log.e(getClass().getName(),"file uri = " + uri);
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(uri, projection,
null, null, null);
if(cursor == null) return;
Log.e(getClass().getName(),"file cursor = " + cursor);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
Log.e(getClass().getName(),"file columnIndex = " + columnIndex);
cursor.moveToFirst();
// The crash happens here
String photoPath = cursor.getString(columnIndex);
Log.e(getClass().getName(),"file photo path = " + photoPath);
cursor.close();
cropImage(photoPath);
}
以下是工作和不工作映像的日志:
工作图像:
文件uri=content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F105681/ORIGINAL/NONE/187859359
文件光标=
android.content.ContentResolver$CursorWrapperInner@8953964
文件列索引=0
文件照片路径=/storage/simulated/0/DCIM/Camera/IMG_20190523_184830.jpg
非工作图像:
文件uri=
content://com.google.android.apps.photos.contentprovider/0/1/mediakey%3A%2Flocal%253A4574915c-b4ac-40af-bc08-b1004670cab2/原件/无/477302338
文件光标=
android.content.ContentResolver$CursorWrapperInner@59448a4
文件列索引=0
文件照片路径=空
如果没有办法避免这个错误,有没有办法隐藏在线存储的照片而只显示本地照片?你所问的技术(至少)有三个问题:
- 正如您所看到的,并不是每个
条目都有MediaStore
数据的值
- 并非每个非
null
值都表示您可以访问的文件系统路径,因为数据
可以访问您无法访问的内容MediaStore
- Android Q和更高版本上没有数据列
在您的例子中,uCrop库接受一个
Uri
。编写良好的Android库知道如何处理Uri
,因此您可以将Uri
交给库,它将从库中获取。为什么不直接使用Uri
?毕竟,在Android Q和更高版本上,您无法访问数据。另外,正如您所发现的,并不是每个MediaStore
条目都有DATA
值,甚至那些没有值的条目也可能不是您可以通过文件系统访问的文件。您正在使用的“第三个库”是什么?@commonware我正在使用“uCrop”库来裁剪图像,它需要sourceUri
,uCrop.of(sourceUri,destinationUri)
但是我想我可能可以直接使用Uri
,我会尝试直接使用它,看看它是如何使用的goes@CommonsWare我不知道为什么我没有直接使用Uri
。它在Uri
上运行得非常顺利,非常感谢您的建议