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

前端 未结 30 1594
[愿得一人]
[愿得一人] 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:26

    I'm experiencing the same. What I found is that if you go to Settings -> Application Manager -> Your App -> Permissions -> Enable Storage, it solves the issue.

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

    I had the same problem... The <uses-permission was in the wrong place. This is right:

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

    The uses-permission tag needs to be outside the application tag.

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

    I had the same problem on Samsung Galaxy Note 3, running CM 12.1. The issue for me was that i had

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

    and had to use it to take and store user photos. When I tried to load those same photos in ImageLoader i got the (Permission denied) error. The solution was to explicitly add

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

    since the above permission only limits the write permission up to API version 18, and with it the read permission.

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

    I would expect everything below /data to belong to "internal storage". You should, however, be able to write to /sdcard.

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

    In my case I was using a file picker library which returned the path to external storage but it started from /root/. And even with the WRITE_EXTERNAL_STORAGE permission granted at runtime I still got error EACCES (Permission denied).
    So use Environment.getExternalStorageDirectory() to get the correct path to external storage.

    Example:
    Cannot write: /root/storage/emulated/0/newfile.txt
    Can write: /storage/emulated/0/newfile.txt

    boolean externalStorageWritable = isExternalStorageWritable();
    File file = new File(filePath);
    boolean canWrite = file.canWrite();
    boolean isFile = file.isFile();
    long usableSpace = file.getUsableSpace();
    
    Log.d(TAG, "externalStorageWritable: " + externalStorageWritable);
    Log.d(TAG, "filePath: " + filePath);
    Log.d(TAG, "canWrite: " + canWrite);
    Log.d(TAG, "isFile: " + isFile);
    Log.d(TAG, "usableSpace: " + usableSpace);
    
    /* Checks if external storage is available for read and write */
    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }
    

    Output 1:

    externalStorageWritable: true
    filePath: /root/storage/emulated/0/newfile.txt
    isFile: false
    usableSpace: 0
    

    Output 2:

    externalStorageWritable: true
    filePath: /storage/emulated/0/newfile.txt
    isFile: true
    usableSpace: 1331007488
    
    0 讨论(0)
  • 2020-11-22 01:31

    Google has a new feature on Android Q: filtered view for external storage. A quick fix for that is to add this code in the AndroidManifest.xml file:

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

    You can read more about it here: https://developer.android.com/training/data-storage/compatibility

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