Search code examples
androidandroid-intentandroid-image

Android : Load Image --> open failed : EACCES


Android 6.0 - Marshmallow

I just want to open the gallery and pick an image but I have the error :

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Download/my_image-1.png: open failed: EACCES (Permission denied)

Here is my permissions declaration in the manifest:

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

And here is my image picker :

((Button)view.findViewById(R.id.button_Logo)).setOnClickListener(new View.OnClickListener(){
  @Override
  public void OnClick(View v) {
   Intent getIntent = new Intent(Intent.ACTION_GETCONTENT);
   getIntent.setType("image/*");
   Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
   pickIntent.setType("image/*");
   Intent chooserIntent = Intent.createChooser(getInten, "Select Image");
   chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{pickIntent});
   startActivityForResult(chooseIntent, PICK_IMAGE);
  }
});

Solution

  • For API 23+ you need to request the read/write permissions even if they are already in your manifest.

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    
    public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity,       Manifest.permission.WRITE_EXTERNAL_STORAGE);
    
    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
    

    }