BitmapFactory: Unable to decode stream: java.io.FileNotFoundException
Asked Answered
R

3

10

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

Racer answered 3/3, 2017 at 5:50 Comment(1)
For Android Q, see the solution below: https://mcmap.net/q/86103/-exception-39-open-failed-eacces-permission-denied-39-on-androidFreeloader
H
22

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);
        }
Hellhole answered 3/3, 2017 at 5:53 Comment(3)
what is this EasyPermissions if it is class or field??Racer
Its library compile 'pub.devrel:easypermissions:0.2.1'Hellhole
If you do not want to use the EasyPermissions library simply follow this answer: https://mcmap.net/q/598140/-how-to-ask-permission-to-access-gallery-on-android-mAstragalus
L
3

You have ask run-time permission for READ-EXTERNAL storage when you start your CAMERA intent:

final int MyVersion = Build.VERSION.SDK_INT;
        if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
            if (!checkIfAlreadyhavePermission()) {
                ActivityCompat.requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
            } else {
               startYourCameraIntent();
            }
        }

checkIfAlreadyhavePermission() method:

private boolean checkIfAlreadyhavePermission() {
        int result = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
        return result == PackageManager.PERMISSION_GRANTED;
    }

Handle Permission Dialog "Allow" and "Deny" button action in onRequestPermission():

@Override
        public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                   startYourCameraIntent();

                } else {
                    Toast.makeText(getActivity(), "Please give your permission.", Toast.LENGTH_LONG).show();
                }
                break;
            }
        }
    }

And add this to your Manifest's application tag:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Liebfraumilch answered 3/3, 2017 at 5:59 Comment(1)
In your activity class.Liebfraumilch
O
0

Add this in your MainActivity.java file


Above 6.0 OS:

    if(ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(MainActivity.this,new String[]{
                Manifest.permission.READ_EXTERNAL_STORAGE
        },1);

    }

 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode==1){
        if(grantResults[0]==PackageManager.PERMISSION_GRANTED){

        }
        else{
            Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_LONG).show();
        }
    }
}
Olympia answered 21/5, 2020 at 11:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.