android.system.ErrnoException:打开失败:EACCES(权限被拒绝)

时间:2018-10-12 07:26:36

标签: android file-permissions android-permissions android-fileprovider

我已经尝试过this link 解释了必须在应用程序标记之外保留“写入外部存储”权限。我已经把它放在外面了。

我将代码放在这里:

cvMain.setDrawingCacheEnabled(true);
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "DEMO_" + timeStamp;
        String path = createWorkingDirectory().getAbsolutePath() + "/" + imageFileName + ".jpg";
        File fileTemp = new File(path);

        if (!fileTemp.exists()) {
            try {
                fileTemp.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Bitmap b = cvMain.getDrawingCache();
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(fileTemp);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        b.compress(Bitmap.CompressFormat.JPEG, 95, fileOutputStream);
        try {
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        cvMain.setDrawingCacheEnabled(false);

createWorkingDirectory()方法:

private File createWorkingDirectory() {
        File directory = new   File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "demo");
        if (!directory.exists()) {
            directory.mkdirs();
        }
        return directory;
    }

在特定的createNewFile()方法调用时出现此错误。 即使出现此错误,我仍在清单和应用程序运行时中保持写入权限。 我无法理解的问题是什么?请帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

我有关于上述问题的解决方案。

cvMain.setDrawingCacheEnabled(true);
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "DEMO_" + timeStamp;
        String path = createWorkingDirectory().getAbsolutePath() + "/" + imageFileName + ".jpg";
        File fileTemp = new File(path);

        if (!fileTemp.exists()) {
            try {
                fileTemp.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Bitmap b = cvMain.getDrawingCache();
        OutputStream stream = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri filePathUri = FileProvider.getUriForFile(MainActivity.this, "demoApp", fileTemp);
            try {
                stream = getContentResolver().openOutputStream(filePathUri);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            if (stream != null) {
                b.compress(Bitmap.CompressFormat.JPEG, 95, stream);
            }
        } else {
            try {
                stream = new FileOutputStream(fileTemp);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            if (stream != null)
                b.compress(Bitmap.CompressFormat.JPEG, 95, stream);
        }
        try {
            if (stream != null)
                stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        cvMain.setDrawingCacheEnabled(false);

file_provider.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="demoApp"
        path="." />
</paths>

AndroidManifest.xml标记内

<provider android:name="android.support.v4.content.FileProvider"
            android:authorities="demoApp"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_provider" />    
</provider>

添加了build.gradle(app)

implementation 'com.android.support:support-v4:27.1.1'