java.io.IOException:打开失败:在API级别23中写入文件时EACCES(权限被拒绝)

时间:2016-01-21 15:10:13

标签: android android-6.0-marshmallow android-permissions ioexception

我正在尝试将我的数据库导出为CSV文件。我已将适当的权限应用于AndroidManifest.xml。我只是想知道为什么我不能在我的(实际设备)Nexus 5X API级别23中编写/创建文件,其中相同的代码在我的模拟器Nexus 7 API级别22中有效。权限是否不够?

以下是代码:

private void writeCsv() {
    File fileDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(),
             File.separator + "Example");
    if (!fileDir.exists()) {
        try {
                fileDir.mkdirs();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(),
            File.separator
            + "Example"
            + File.separator
            + (new Date()) + ".csv");
    if (!file.exists()) {
        try {
            file.createNewFile(); // THIS IS WHERE THE EXCEPTION POINTS OUT
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    if (file.exists()) {
        try {
            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter bfWriter = new BufferedWriter(fileWriter);

            bfWriter.write("Id,Text,Timestamp\n");
            mDatabase.getWritableDatabase();
            List<Text> texts = mDatabase.getAllTexts();
            for (Text text : texts) {
                bfWriter.write(text.getId() + "," + text.getText()
                        + "," + text.getTimestamp() + "\n");
            }

            bfWriter.close();
            mDatabase.close();

            Toast.makeText(getActivity(),
                    "Texts exported to " + file.getAbsolutePath(),
                    Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我的AndroidManifest

<application
...
</application>

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

这是日志:

01-21 22:55:22.589 25530-25530/com.example.android W/System.err: java.io.IOException: open failed: EACCES (Permission denied)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at java.io.File.createNewFile(File.java:939)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at com.example.android.settings.SettingsPage.writeCsv(SettingsPage.java:166)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at com.example.android.settings.SettingsPage.onClick(SettingsPage.java:89)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at android.view.View.performClick(View.java:5204)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at android.view.View$PerformClick.run(View.java:21153)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at android.os.Looper.loop(Looper.java:148)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
01-21 22:55:22.602 25530-25530/com.example.android W/System.err:     at libcore.io.Posix.open(Native Method)
01-21 22:55:22.602 25530-25530/com.example.android W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
01-21 22:55:22.603 25530-25530/com.example.android W/System.err:     at java.io.File.createNewFile(File.java:932)
01-21 22:55:22.603 25530-25530/com.example.android W/System.err:    ... 11 more

1 个答案:

答案 0 :(得分:7)

不,在定位Api23时声明权限是不够的。

您必须在Marshmallow以后的运行时请求权限。

http://developer.android.com/training/permissions/requesting.html

你现在可以通过定位Api 22而不是23

来解决这个问题