BitmapFactory: Unable to decode stream: java.io.FileNotFoundException

Monali picture Monali · Mar 3, 2017 · Viewed 23.9k times · Source

I have a problem concerning the BitMapFactory.decodeFile.

In my app, I want to user to be able to select an image from his/her device or take a photograph. This must then be displayed in an ImageView

Here is code snippet:

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();
                    MyImage image = new MyImage();
                    image.setTitle("Test");
                    image.setDescription("test choose a photo from gallery and add it to " + "list view");
                    image.setDatetime(System.currentTimeMillis());
                    image.setPath(picturePath); 

And I am getting this exception:

BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20170302-WA0012.jpg: open failed: EACCES (Permission denied)

How to resolve it.Please help me.Thanks in advance..

Answer

Jd Prajapati picture Jd Prajapati · Mar 3, 2017

Its permission issue, you need to add permission in manifest for external read storage then after you can able to use it and if you are using os above 6.0 then you need use Easy permission.

For Write :

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

For Read:

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

Above 6.0:

private String[] galleryPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

if (EasyPermissions.hasPermissions(this, galleryPermissions)) {
            pickImageFromGallery();
        } else {
            EasyPermissions.requestPermissions(this, "Access for storage",
                    101, galleryPermissions);
        }