2

I have this method for show one image from my SD card to Android studio:

public void showImage(){
    File imgFile = new  File(Environment.getExternalStorageDirectory().getPath()+PATH+"Foto.jpg");
    if(imgFile.exists()){
        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        ivProfileImage.setImageBitmap(myBitmap);
        Log.i("Image","The image exists!");
    }
}

Where ivProfileImage is an ImageView, and PATH is a correct one. I'm getting this messages:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/2673-1D16/ProfilePhotos/Foto.jpg: open failed: EACCES (Permission denied)
I/Image: The image exists!

And my AndroidManifest.xml have the permissions:

  • "android.permission.READ_EXTERNAL_STORAGE"
  • "android.permission.WRITE_EXTERNAL_STORAGE"

I have been looking for an answer, but no one works for me. Any idea?

Thanks in advance.

2 Answers2

4

You have to check for permission on runtime. It´s new in Android 6. Look here.

beta
  • 131
  • 1
  • 1
  • 3
1
private String[] apppermissions =new String[] {
            Manifest.permission.CAMERA,
            Manifest.permission.CALL_PHONE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.INTERNET,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO

    };
private boolean checkAndRequestPermission() {
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        java.util.List<String> listPermissionsNeeded = new ArrayList<>();
        for (String perm : apppermissions)
        {
            if (ContextCompat.checkSelfPermission(activity, perm) != PackageManager.PERMISSION_GRANTED)
            {
                listPermissionsNeeded.add(perm);
            }
        }

        if (!listPermissionsNeeded.isEmpty())
        {
            ActivityCompat.requestPermissions(activity, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
                    PERMISSION_REQUEST_CODE);
            return false;
        }

        return true;
    }
ilidiocn
  • 323
  • 2
  • 5