James
James

Reputation: 263

Unable to Decode Stream Image to Fragment

The Code Works perfectly but Im having problem with the Permission Denied but i already put READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permission on Android Manifest any idea. Already Tried this one Unable to decode stream: java.io.FileNotFoundException (Permission denied)

BitmapFactory﹕ Unable to decode stream - null

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20201001_125759.jpg (Permission denied)

AndroidManifest.xml

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

MainActivity

    //Open phone gallery
    private void getImageFromGallery(){
        Intent i = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, GALLERY_REQUEST);
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //Check if the intent was to pick image, was successful and an image was picked
        if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            ImageView imageView = (ImageView) findViewById(R.id.the_grid_image_preview);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }

Upvotes: 0

Views: 108

Answers (1)

Mariusz Brona
Mariusz Brona

Reputation: 1609

If you're using Android version 6.0 or up, you need to ask for permissions in runtime:

https://developer.android.com/training/permissions/requesting

For this I use a nice wrapper called Dexter which simplifies it and is really easy to use.

You can then write this function in your Fragment:

private fun checkFilePermissions(onPermissionGranted: () -> Unit) {
        Dexter.withActivity(activity)
            .withPermissions(
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
            .withListener(object : BaseMultiplePermissionsListener() {
                override fun onPermissionsChecked(response: MultiplePermissionsReport) {
                    if (response.areAllPermissionsGranted()) {
                        onPermissionGranted()
                    }
                }
            })
            .check()
    }

And in when you call your method getImageFromGallery() just invoke it like this:


...
checkFilePermissions {
    getImageFromGallery()
}

Upvotes: 0

Related Questions