Java 尝试在 Android 中将文件写入 sdcard 时出现 FileNotFoundException(权限被拒绝)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35285309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:30:12  来源:igfitidea点击:

FileNotFoundException (permission denied) when trying to write file to sdcard in Android

javaandroidfilenotfoundexceptionandroid-permissions

提问by Drag0

As you can notice from title, I have a problem with writing file to sdcard in Android. I've checked this questionbut it didn't help me. I want to write file that will be in public space on sdcard so that any other app could read it.

正如您从标题中注意到的那样,我在 Android 中将文件写入 sdcard 时遇到问题。我已经检查了这个问题,但它对我没有帮助。我想在 sdcard 上的公共空间中写入文件,以便任何其他应用程序都可以读取它。

First, I check if sdcard is mounted:

首先,我检查是否安装了 sdcard:

Environment.getExternalStorageState();

Then, I run this code:

然后,我运行此代码:

File baseDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    baseDir.mkdirs();
    File file = new File(baseDir, "file.txt");
    try {
        FileOutputStream out = new FileOutputStream(file);
        out.flush();
        out.close();
        Log.d("NEWFILE", file.getAbsolutePath());
    } catch (IOException e) {
        e.printStackTrace();
    }

I have:

我有:

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

<application>
...
</application>
</manifest>

in my AndroidManifest.xml.

在我的 AndroidManifest.xml 中。

Exact error is this:

确切的错误是这样的:

java.io.FileNotFoundException: /storage/1510-2908/Download/secondFile.txt: open failed: EACCES (Permission denied)

I'm testing my code on emulator (emulating Nexus5 API 23). My minimum required SDK version is 19 (4.4 Kitkat).

我正在模拟器上测试我的代码(模拟 Nexus5 API 23)。我所需的最低 SDK 版本是 19 (4.4 Kitkat)。



Also, everything works fine with writing files to private folder on sdcard with same code so I'd say former code should work too:

此外,使用相同的代码将文件写入 sdcard 上的私有文件夹一切正常,所以我想说以前的代码也应该工作:

File newFile = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "esrxdtcfvzguhbjnk.txt");
    newFile.getParentFile().mkdirs();
    try {
        FileOutputStream out = new FileOutputStream(newFile);
        out.flush();
        out.close();
        Log.d("NEWFILE", newFile.getAbsolutePath());
    } catch (IOException e) {
        e.printStackTrace();
    }

Does anyone have any clue what could be the problem? Could it be that somehow KitKat 4.4 just doesn't allow writing to public space in sdcard anymore or?

有没有人知道可能是什么问题?可能是因为某种方式 KitKat 4.4 不再允许在 sdcard 中写入公共空间吗?

采纳答案by Dhaval Patel

  1. Everything works fine with writing files to private folder on sdcard
  1. 将文件写入 sdcard 上的私人文件夹一切正常

Beginning with Android 4.4, these permissions are not required if you're reading or writing only files that are private to your app. For more information, see saving files that are app-private.

从 Android 4.4 开始,如果您只读取或写入应用程序私有的文件,则不需要这些权限。有关更多信息,请参阅保存应用程序私有的文件。

  1. FileNotFoundException (permission denied) when trying to write file to sdcard in Android
  1. 尝试在 Android 中将文件写入 sdcard 时出现 FileNotFoundException(权限被拒绝)

As you are trying to write the file in emulator having API 23(Marshmallow), You need to Request WRITE_EXTERNAL_STORAGEpermission at runtime also. Check thisand thisfor more detail.

当您尝试在具有 API 23(Marshmallow) 的模拟器中编写文件时,您还需要WRITE_EXTERNAL_STORAGE在运行时请求权限。检查thisthis了解更多细节。