首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓10 BitmapFactory.decodeFile(ImageFilePath)返回null

安卓10 BitmapFactory.decodeFile(ImageFilePath)返回null
EN

Stack Overflow用户
提问于 2020-06-30 16:24:15
回答 2查看 2.7K关注 0票数 0

我正在构建一个安卓应用程序,允许用户从相机或画廊中挑选图像,然后在一个Imageview.My应用程序中显示图像,如果用户从相机拍摄图片工作良好,但如果用户从画廊我的应用程序只工作在安卓9和以下,BitmapFactory.decodeFile( imageFilePath )总是返回null在安卓10上的imageFilePath=null是相同的安卓9和10)。在打开图库之前,我已经申请了读取外部存储的权限,然后从onActivityResult获取图像路径,如下所示:

代码语言:javascript
复制
    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;
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-30 16:56:01

代码语言:javascript
复制
InputStream is = getContentResolver().openInputStream(data.getData());

Bitmap bitmap = BitmapFactory.decodeStream(is);

在所有Android版本中使用此选项。

停止尝试获取uri的路径。

票数 8
EN

Stack Overflow用户

发布于 2021-02-16 05:14:23

如果问题只出现在Android10上,你需要做的只是这一行:(清单) android:requestLegacyExternalStorage="true"

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62653207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档