Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

android 10 BitmapFactory.decodeFile(ImageFilePath) return null

I'm building an android application which allow user to pick Image from camera or From Gallery then display the image in a Imageview.My application working fine if the user take the picture from camera, but if the user pick the image from gallery my application only work on android 9 and bellow,BitmapFactory.decodeFile(ImageFilePath) always return null on android 10 with the imageFilePath = "/storage/emulated/0/DCIM/Camera/20200629_114552.jpg"(path is the same on android 9 and 10). I've requested for read external storage permission before open gallery and then get the image path from onActivityResult like bellow:

    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;
        }
    }
}



    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
InputStream is = getContentResolver().openInputStream(data.getData());

Bitmap bitmap = BitmapFactory.decodeStream(is);

Use this for all Android versions.

Stop with trying to get a path for an uri.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...