Exception 'open failed: EACCES (Permission denied)' on Android

前端 未结 30 1418
[愿得一人]
[愿得一人] 2020-11-22 00:59

I am getting

open failed: EACCES (Permission denied)

on the line OutputStream myOutput = new FileOutputStream

相关标签:
30条回答
  • 2020-11-22 01:45

    Maybe the answer is this:

    on the API >= 23 devices, if you install app (the app is not system app), you should check the storage permission in "Setting - applications", there is permission list for every app, you should check it on! try

    0 讨论(0)
  • 2020-11-22 01:45

    I am creating a folder under /data/ in my init.rc (mucking around with the aosp on Nexus 7) and had exactly this problem.

    It turned out that giving the folder rw (666) permission was not sufficient and it had to be rwx (777) then it all worked!

    0 讨论(0)
  • 2020-11-22 01:45

    To store a file in a directory which is foreign to the app's directory is restricted above API 29+. So to generate a new file or to create a new file use your application directory like this :-

    So the correct approach is :-

    val file = File(appContext.applicationInfo.dataDir + File.separator + "anyRandomFileName/")
    

    You can write any data into this generated file !

    The above file is accessible and would not throw any exception because it resides in your own developed app's directory.

    The other option is android:requestLegacyExternalStorage="true" in manifest application tag as suggested by Uriel but its not a permanent solution !

    0 讨论(0)
  • 2020-11-22 01:46

    Strangely after putting a slash "/" before my newFile my problem was solved. I changed this:

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

    to this:

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

    UPDATE: as mentioned in the comments, the right way to do this is:

    File myFile= new File(Environment.getExternalStorageDirectory(), "newFile");
    
    0 讨论(0)
  • 2020-11-22 01:46

    I had the same problem and none of suggestions helped. But I found an interesting reason for that, on a physical device, Galaxy Tab.

    When USB storage is on, external storage read and write permissions don't have any effect. Just turn off USB storage, and with the correct permissions, you'll have the problem solved.

    0 讨论(0)
  • 2020-11-22 01:47

    For API 23+ you need to request the read/write permissions even if they are already in your manifest.

    // 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" />
    

    For official documentation about requesting permissions for API 23+, check https://developer.android.com/training/permissions/requesting.html

    0 讨论(0)
提交回复
热议问题