简体   繁体   中英

FileNotFoundException: /storage/sdcard0/Myfile.txt: open failed: EACCES (Permission denied)

I'm trying to write a file on a folder outside the app, in the external storage. I have added WRITE_EXTERNAL_STORAGE permission in manifest.

There are similar errors here in stackoverflow but after trying the solutions (as you will see in my next lines) I'm having the same error.

I tryed it in both emulator with Nexus 5X api 27 oreo rom and also in a Huawei real phone with Android 4.4

In both devices I'm having the same error:

FileNotFoundException: /storage/sdcard0/Myfile.txt: open failed: EACCES (Permission denied)

This is my code:

    String content = "hello world";
    File file;
    FileOutputStream outputStream;
    try {
        file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "MyFile.txt");
        outputStream = new FileOutputStream(file);
        outputStream.write(content.getBytes());
        outputStream.close();
    } catch (IOException e) {
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp">

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />

<uses-feature
    android:name="android.hardware.camera.autofocus"
    android:required="false" />
<uses-feature
    android:name="android.hardware.camera"
    android:required="false" />
<uses-feature
    android:name="android.hardware.telephony"
    android:required="false" />
<uses-feature
    android:name="android.hardware.location"
    android:required="false" />
<uses-feature
    android:name="android.hardware.location.gps"
    android:required="false" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".activities.Activity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

For devices with Android >=6.0, you have to request permissions at runtime

For android 4, apprently adding android:maxSdkVersion="18" generate this exception, try by removing it

EDIT : For Android 4.4 and above, you don't WRITE_EXTERNAL_STORAGE IF ONLY you want to write to external storage, from the doc :

beginning with Android 4.4 (API level 19), it's no longer necessary for your app to request the WRITE_EXTERNAL_STORAGE permission when your app wants to write to its own application-specific directories on external storage (the directories provided by getExternalFilesDir())

Elsewere, for android < 4.4, you need to add android:maxSdkVersion="18" for own application-specific directories on external storage

Just try to request permission with this code :

private void requestPermission() {
    ActivityCompat.requestPermissions(getActivity() ,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE ,
                    Manifest.permission.READ_EXTERNAL_STORAGE} ,
            PERMISSION_READ_WRITE_REQUEST_CODE);
}

PERMISSION_READ_WRITE_REQUEST_CODE is the custom variable , in my way is 34.

And handle it in

 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode){
        case PERMISSION_READ_WRITE_REQUEST_CODE : {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
                  //yeee permission granted
            }
        }
    }
}

Finally yesterday I discovered the same thing that CommonsWare has explained perfectly in the comments:

Get rid of android:maxSdkVersion="18". That is only valid if you are using getExternalFilesDir(), getExternalCacheDir(), and similar methods on Context. You are using Environment.getExternalStorageDirectory(), so you always need WRITE_EXTERNAL_STORAGE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM