简体   繁体   English

尝试将文件写入Android中的SD卡时,FileNotFoundException(权限被拒绝)

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

As you can notice from title, I have a problem with writing file to sdcard in Android. 正如你可以从标题中注意到的那样,我在Android中将文件写入sdcard时遇到了问题。 I've checked this question but 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. 我想写一个将在sdcard上的公共空间中的文件,以便任何其他应用程序都可以读取它。

First, I check if sdcard is mounted: 首先,我检查是否安装了SD卡:

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). 我正在模拟器上测试我的代码(模拟Nexus5 API 23)。 My minimum required SDK version is 19 (4.4 Kitkat). 我所需的最低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: 此外,一切正常,使用相同的代码将文件写入SD卡上的私人文件夹,所以我说前代码也应该工作:

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中写入公共空间或者?

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

Beginning with Android 4.4, these permissions are not required if you're reading or writing only files that are private to your app. 从Android 4.4开始,如果您只读取或写入应用程序专用的文件,则不需要这些权限。 For more information, see saving files that are app-private. 有关更多信息,请参阅保存app-private文件。

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

As you are trying to write the file in emulator having API 23(Marshmallow), You need to Request WRITE_EXTERNAL_STORAGE permission at runtime also. 当您尝试在具有API 23(Marshmallow)的模拟器中编写文件时,您还需要在运行时请求WRITE_EXTERNAL_STORAGE权限。 Check this and this for more detail. 检查了解更多详情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将图像文件写入 sdcard 时出现“Permission Denied”错误 - “Permission Denied” error when write image file into sdcard java.io.FileNotFoundException:/ mnt / sdcard / file(权限被拒绝) - java.io.FileNotFoundException: /mnt/sdcard/file (Permission denied) 尝试写入文件时,Tomcat权限被拒绝 - Tomcat permission denied when trying to write a file 尝试写入新文件时,FileNotFoundException(拒绝访问) - FileNotFoundException (Access is denied) when trying to write to a new file 尝试将字符串从Web写入文件时出现FileNotFoundException(permission) - FileNotFoundException(permission) when trying to write string from web to file 尝试从图片读取元数据时获取FileNotFoundException(权限被拒绝) - Getting FileNotFoundException (Permission denied) when trying to read metadata from a picture 在Android的SD卡中写入文件 - Write a file in SDcard in Android Android 6.0文件写入权限被拒绝 - Android 6.0 File write permission denied CameraX 将图像保存到文件,FileNotFoundException:(权限被拒绝) - CameraX Saving images to file, FileNotFoundException: (Permission Denied) Android 应用无法写入下载目录:java.io.FileNotFoundException(权限被拒绝) - Android app fails to write to downloads directory: java.io.FileNotFoundException (Permission denied)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM