我正在构建一个安卓应用程序,允许用户从相机或画廊中挑选图像,然后在一个Imageview.My应用程序中显示图像,如果用户从相机拍摄图片工作良好,但如果用户从画廊我的应用程序只工作在安卓9和以下,BitmapFactory.decodeFile( imageFilePath )总是返回null在安卓10上的imageFilePath=null是相同的安卓9和10)。在打开图库之前,我已经申请了读取外部存储的权限,然后从onActivityResult获取图像路径,如下所示:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case GALLERY_REQUEST_CODE:
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imgDecodableString = cursor.getString(columnIndex);
cursor.close();
if (!mNextImg) {
mBiomatrics_left_identity_img
.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
} else {
mBiomatrics_right_identity_img
.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
}
mNextImg = !mNextImg;
if (mBiomatrics_left_identity_img.getDrawable() != null
&& mBiomatrics_right_identity_img.getDrawable() != null) {
mBiomatrics_btn_confirm.setTextColor(getResources()
.getColor(R.color.mainTextColor));
} else {
mBiomatrics_btn_confirm.setTextColor(getResources()
.getColor(R.color.biometrics_btn_confirm_deactive_color));
}
break;
}
}
}发布于 2020-06-30 16:56:01
InputStream is = getContentResolver().openInputStream(data.getData());
Bitmap bitmap = BitmapFactory.decodeStream(is);在所有Android版本中使用此选项。
停止尝试获取uri的路径。
发布于 2021-02-16 05:14:23
如果问题只出现在Android10上,你需要做的只是这一行:(清单) android:requestLegacyExternalStorage="true"

https://stackoverflow.com/questions/62653207
复制相似问题