1

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



    
user2905416
  • 404
  • 7
  • 21
  • 1
    The .DATA column is not available under Android Q. So it is a mistery for me how you obtained a path like /storage/emulated/0/DCIM/Camera/20200629_114552.jpg. – blackapps Jun 30 '20 at 08:51

2 Answers2

9
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.

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • No luck, still it returns null in Android 10 Mobile. but it works in lower version. – Tom_Vel Dec 13 '20 at 04:04
  • DecodeStream will return null if the bitmap would become too big for available memory. Try images with lower resolution. – blackapps Dec 13 '20 at 07:45
  • It is below MB, actually it is working for lower android mobiles without any issue. It is not working in android 10. Is anything file path related issue? Also I have added legacy storage flag true in manifest file – Tom_Vel Dec 14 '20 at 14:24
  • What is MB? This is not about file paths and legacy storage is not needed. Try a picture with lower resolution. – blackapps Dec 14 '20 at 15:51
  • My image size is 700 kb – Tom_Vel Dec 17 '20 at 14:33
  • It is not about size. Its about resolution. I mentioned that twice. – blackapps Dec 17 '20 at 14:37
3

Solution: for Android 10, All you need is this line: (in manifests) android:requestLegacyExternalStorage="true"

enter image description here

Eng.OsamaYousef
  • 458
  • 2
  • 5
  • 10