5

First, I'll say that I've read many other posts that match this question and none of them have worked for me.

I'm testing my app on my device, a Nexus 5 running Android L. It has not been rooted. This same code works on an older Android, running API 19.

I'm 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)

On this line:

FileOutputStream fos = new FileOutputStream(imageFile);

I've also tried 10 other ways to get a filePath, and I now suspect this is a device/Android L issue.

Any idea what's happening?

Shreyans
  • 871
  • 7
  • 20

0 Answers0