FileNotFoundException: open failed: EACCES (Permission denied)

In the emulator (I use genymotion) it works fine, but when I run it on a real device (my phone is ASUS ZenFone Laser 5.0), throw a filenotfoundexception

java.io.FileNotFoundException: /storage/emulated/0/cam20160926_075819.jpg: open failed: EACCES (Permission denied)

imgBitmap = MediaStore.Images.Media.getBitmap (cr, selectedImage);

here is the onActivityResult () method

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode){ case CAMERA_REQUEST: if (resultCode == Activity.RESULT_OK){ Uri selectedImage = imageUri; getActivity().getContentResolver().notifyChange(selectedImage, null); ContentResolver cr = getActivity().getContentResolver(); Bitmap imgBitmap; try { imgBitmap = MediaStore.Images.Media.getBitmap(cr, selectedImage); accountPhoto.setImageBitmap(imgBitmap); } catch (IOException e) { e.printStackTrace(); Toast.makeText(getActivity().getApplicationContext(), "Something went wrong while taking a photo", Toast.LENGTH_LONG).show(); Log.e("Camera", e.toString()); } } } } 

I read some related questions and solutions about these EACCES, and the problem seems to be in my resolution:

 <uses-feature android:name="android.hardware.camera2" android:required="true"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

Am I missing something? thanks for the answer

+5
source share
2 answers

you must request permission before starting your instructions, because this permission is considered dangerous in Marshmallow:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, RESULT); } else { //your code } 
+1
source

I suspect that WRITE_EXTERNAL_STORAGE overrides read permission READ_EXTERNAL_STORAGE . If you look in the documentation you will see that the first also allows you to read data from the repository.

Try removing the android:maxSdkVersion and see if this works. I suspect your device is using SDK version> 18.

Check this answer for more information: / questions / 200590 / do-i-have-to-declare-both-writeexternalstorage-and-readexternalstorage / 1093562 # 1093562

0
source

All Articles