我得到了

open failed: EACCES(权限被拒绝)

myOutput = new FileOutputStream(outFileName);

我检查了根目录,并尝试了android.permission.WRITE_EXTERNAL_STORAGE。

我该如何解决这个问题?

try {
    InputStream myInput;

    myInput = getAssets().open("XXX.db");

    // Path to the just created empty db
    String outFileName = "/data/data/XX/databases/"
            + "XXX.db";

    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    // Transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
    buffer = null;
    outFileName = null;
}
catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

当前回答

当您的应用程序属于系统应用程序时,它不能访问SD卡。

其他回答

我在init中/data/下创建了一个文件夹。rc(在Nexus 7上摆弄aosp),就遇到了这个问题。

结果证明,给文件夹rw(666)权限是不够的,它必须是rwx(777),然后它就工作了!

如果你有一个根设备,可以通过以下adb命令绕过6.0后的存储权限强制执行:

root@msm8996:/ # getenforce
getenforce
Enforcing
root@msm8996:/ # setenforce 0
setenforce 0
root@msm8996:/ # getenforce
getenforce
Permissive

我希望下面的所有内容/数据都属于“内部存储”。但是,您应该能够写入/sdcard。

我在运行CM 12.1的三星Galaxy Note 3上也遇到了同样的问题。我的问题是我有

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

而且必须用它来拍摄和存储用户照片。当我试图在ImageLoader中加载这些相同的照片时,我得到了(权限被拒绝)错误。解决方案是显式添加

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

因为上面的权限只限制写权限到API版本18,以及它的读权限。

奇怪的是,在我的newFile之前加上斜杠“/”后,我的问题就解决了。我改了这个:

File myFile= new File(Environment.getExternalStorageDirectory() + "newFile");

:

File myFile= new File(Environment.getExternalStorageDirectory() + "/newFile");

更新: 正如评论中提到的,正确的做法是:

File myFile= new File(Environment.getExternalStorageDirectory(), "newFile");