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

查看:216
本文介绍了尝试将文件写入Android中的SD卡时,FileNotFoundException(权限被拒绝)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As you can notice from title, I have a problem with writing file to sdcard in Android. 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:

Environment.getExternalStorageState();

然后,我运行此代码:

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

我有:

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

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

确切的错误是:

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

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

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

此外,一切正常,将文件写入私人文件夹带有相同代码的SD卡,所以我说前代码也应该有效:

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

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

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

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?

推荐答案



  1. 将文件写入sdcard上的私人文件夹,一切正常


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

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.



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


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

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. Check this and this for more detail.

这篇关于尝试将文件写入Android中的SD卡时,FileNotFoundException(权限被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆