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

时间:2022-09-28 20:32:50

I'm trying to export my DB to a CSV file. I have applied the appropriate permission to the AndroidManifest.xml. I'm just wondering why I can't write/create the file in my (actual device) Nexus 5X API level 23 where the same code works in my emulator Nexus 7 API level 22. Is the permission not enough?

我想把DB导出到CSV文件中。我已经将适当的权限应用到AndroidManifest.xml。我想知道为什么我不能在我的(实际设备)Nexus 5X API级别23中编写/创建文件,在我的仿真器Nexus 7 API级别22中,同样的代码可以工作。许可还不够吗?

Here's the code:

这是代码:

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();
        }
    }
}

My AndroidManifest:

我的AndroidManifest:

<application
...
</application>

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

Here's the log:

日志:

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 个解决方案

#1


7  

No, declaring the permission is not enough when targeting Api23.

不,在针对Api23的时候仅仅声明许可是不够的。

You have to request the permissions at runtime in Marshmallow onwards.

您必须在运行时在Marshmallow中请求权限。

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

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

You can get around this for now by targeting Api 22 instead of 23

现在你可以通过使用Api 22而不是23来解决这个问题

#1


7  

No, declaring the permission is not enough when targeting Api23.

不,在针对Api23的时候仅仅声明许可是不够的。

You have to request the permissions at runtime in Marshmallow onwards.

您必须在运行时在Marshmallow中请求权限。

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

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

You can get around this for now by targeting Api 22 instead of 23

现在你可以通过使用Api 22而不是23来解决这个问题