打开失败:EACCES(权限被拒绝)

时间:2017-03-11 17:11:11

标签: android

尝试保存图片时,我在Galaxy J5(Android 6.0)中遇到以下错误:

java.io.FileNotFoundException: /storage/FA49-E5D3/DCIM/Camera/20170311_140435.jpg: open failed: EACCES (Permission denied)

我拥有以下权限:

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

活动:

    if (Build.VERSION.SDK_INT >= 23) {
        System.out.println("checking permissions...");
        // -------------------------------------- PERMISOS CAMARA -------------------------------------------------
        if (checkSelfPermission("android.permission.CAMERA") != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{"android.permission.CAMERA"}, 137);
            return;
        }
        // ---------------------------------- PERMISOS READ EXTERNAL ---------------------------------------------
        if (checkSelfPermission("android.permission.READ_EXTERNAL_STORAGE") != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{"android.permission.READ_EXTERNAL_STORAGE"}, 139);
            return;
        }
        // ---------------------------------- PERMISOS WRITE EXTERNAL ---------------------------------------------
        if (checkSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE") != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"}, 138);
            return;
        }
    } else {
        System.out.println("API < 23... Skip permissions...");
    }

请注意,这在摩托罗拉G3(Android 6.0)中正常运行,可以将图像保存在此路径中:

/storage/emulated/0/DCIM/Camera/IMG_20170311_115623958.jpg

1 个答案:

答案 0 :(得分:4)

您正在尝试在micro SD卡上写入文件。但它们现在仅适用于应用程序。只有系统相机应用程序才有权在SD上的DCIM文件夹中写入。

查看getExternalFilesDirs()返回的第二个项目。您的应用可以在SD卡上的特定应用目录中写入。