Android L + Nexus 5: java.io.FileNotFoundException: open failed: EACCES (Permission denied)

Firstly, I will say that I read many other posts that are relevant to this issue, and none of them worked for me.

I am testing my application on my device, Nexus 5 is running Android L. It is not rooted. The same code works on older Android running API 19.

I am trying to take a screenshot and share it using this code:

View screen = getWindow().getDecorView().getRootView(); screen.setDrawingCacheEnabled(true); Bitmap bitmap = screen.getDrawingCache(); String filename = getScreenshotName(); String filePath = Environment.getExternalStorageDirectory().getPath() + File.separator + filename; File imageFile = new File(filePath); try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos); byte[] bitmapData = bos.toByteArray(); FileOutputStream fos = new FileOutputStream(imageFile); fos.write(bitmapData); fos.flush(); fos.close(); } catch (FileNotFoundException e) { Log.e("GREC", e.getMessage(), e); } catch (IOException e) { Log.e("GREC", e.getMessage(), e); } // share Intent share = new Intent(Intent.ACTION_SEND); share.setType("image/png"); share.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath)); startActivity(Intent.createChooser(share, "Share Image")); 

I have these permissions in AndroidManifest.xml :

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

And I get this error:

 java.io.FileNotFoundException: /storage/emulated/0/2014-09-14.png: open failed: EACCES (Permission denied) 

In this line:

 FileOutputStream fos = new FileOutputStream(imageFile); 

I also tried 10 more ways to get filepath, and now I suspect that this is a problem with the device / Android L.

Any idea what is going on?

+5
source share

Source: https://habr.com/ru/post/1202598/


All Articles