我得到了

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

当前回答

对于API 23+,您需要请求读/写权限,即使它们已经在您的清单中。

// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to grant permissions
 *
 * @param activity
 */
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

AndroidManifest.xml

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

有关请求API 23+权限的官方文档,请查看https://developer.android.com/training/permissions/requesting.html

其他回答

我也有同样的问题……<uses-permission在错误的地方。这是正确的:

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

users -permission标记需要在application标记之外。

除了所有的答案,如果客户端使用Android 6.0, Android为(Marshmallow)增加了新的权限模型。

技巧:如果你的目标版本是22或以下,你的应用程序将在安装时请求所有权限,就像它在任何运行棉花糖以下操作系统的设备上一样。如果你在模拟器上尝试,那么从android 6.0开始,你需要显式地去设置->应用程序-> YOURAPP ->权限,如果你已经给了任何权限,改变权限。

谷歌在Android Q上有一个新功能:外部存储的过滤视图。一个快速的修复方法是在AndroidManifest.xml文件中添加以下代码:

<manifest ... >
    <!-- This attribute is "false" by default on apps targeting Android Q. -->
    <application android:requestLegacyExternalStorage="true" ... >
     ...
    </application>
</manifest>

你可以在这里阅读更多信息:https://developer.android.com/training/data-storage/use-cases

编辑:我开始得到反对票,因为这个答案是过时的安卓11。看到答案的同学请点击上面的链接阅读说明。

请记住,即使您在清单中设置了所有正确的权限: 唯一允许第三方应用程序写入你的外部卡的地方是“他们自己的目录” (例如/ sdcard / Android /数据/) 试图写入其他地方:你会得到异常: EACCES(拒绝许可)

Android 10 (API 29)引入了作用域存储。更改清单以请求遗留存储并不是长期解决方案。

当我用context.getExternalFilesDir(null)替换我以前的Environment.getExternalStorageDirectory()实例(API 29已弃用)时,我修复了这个问题。

注意,如果存储位置不可用,context.getExternalFilesDir(type)可以返回null,因此无论何时检查是否具有外部权限,都要检查这一点。

点击这里阅读更多。