android 10 BitmapFactory.decodeFile(ImageFilePath) 返回 null

use*_*416 0 android android-studio android-10.0

我正在构建一个 android 应用程序,它允许用户从相机或从图库中选择图像,然后在 Imageview 中显示图像。如果用户从相机拍摄图片,我的应用程序工作正常,但如果用户从图库中选择图像,我的应用程序仅适用于 android 9 及以下版本,BitmapFactory.decodeFile(ImageFilePath) 在 android 10 上始终返回 null,其中 imageFilePath = "/storage/emulated/0/DCIM/Camera/20200629_114552.jpg"(路径在 android 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;
        }
    }
}



    
Run Code Online (Sandbox Code Playgroud)

bla*_*pps 7

InputStream is = getContentResolver().openInputStream(data.getData());

Bitmap bitmap = BitmapFactory.decodeStream(is);
Run Code Online (Sandbox Code Playgroud)

将此用于所有 Android 版本。

停止尝试获取 uri 的路径。