Android L + Nexus 5:java.io.FileNotFoundException:打开失败:EACCES(权限被拒绝)

Shr*_*ans 5 java android nexus-5 android-5.0-lollipop

首先,我会说我读过许多其他与此问题相符的帖子,但这些帖子都没有对我有用.

我正在我的设备上测试我的应用程序,运行Android L的Nexus 5.它还没有根植.这个相同的代码适用于运行API 19的旧版Android.

我正在尝试截取屏幕截图并使用以下代码进行分享:

    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"));
Run Code Online (Sandbox Code Playgroud)

我有这些权限AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

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

在这一行:

FileOutputStream fos = new FileOutputStream(imageFile);
Run Code Online (Sandbox Code Playgroud)

我还尝试了10种其他方法来获取filePath,我现在怀疑这是一个设备/ Android L问题.

知道发生了什么事吗?