有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android上的异常“打开失败:EACCES(权限被拒绝)”

我越来越

open failed: EACCES (Permission denied)

在线OutputStream myOutput = new FileOutputStream(outFileName);

我检查了根目录,并尝试了安卓.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();
}

共 (6) 个答案

  1. # 1 楼答案

    谷歌在安卓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

    编辑:我开始得到反对票,因为这个答案对于Android 11来说已经过时了。因此,无论谁看到这个答案,请转到上面的链接并阅读说明

  2. # 2 楼答案

    我也有同样的问题。。。这个<uses-permission放错地方了。这是正确的:

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

    uses-permission标记需要位于application标记之外

  3. # 3 楼答案

    除了所有答案之外,请确保您没有将手机用作USB存储

    我在启用USB存储模式的HTC Sensation上也遇到了同样的问题。我仍然可以调试/运行应用程序,但无法保存到外部存储

  4. # 4 楼答案

    对于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

  5. # 5 楼答案

    在模拟器内运行应用程序时,我曾经观察到过这一点。在emulator设置中,您需要正确指定外部存储器(“SD卡”)的大小。默认情况下,“外部存储”字段为空,这可能意味着没有此类设备,即使在清单中授予了权限,也会抛出EACCES

  6. # 6 楼答案

    将android:requestLegacyExternalStorage=“true”添加到android清单 它在SDK 29+上与Android 10(Q)配合使用
    或者在迁移Android X之后

     <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon=""
        android:label=""
        android:largeHeap="true"
        android:supportsRtl=""
        android:theme=""
        android:requestLegacyExternalStorage="true">