简体   繁体   English

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

[英]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. 我在清单中添加了WRITE_EXTERNAL_STORAGE权限。

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. stackoverflow中有类似的错误,但在尝试解决方案后(正如您将在下一行中看到的)我遇到了同样的错误。

I tryed it in both emulator with Nexus 5X api 27 oreo rom and also in a Huawei real phone with Android 4.4 我在Nexus 5X api 27 oreo rom的模拟器和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 对于Android> = 6.0的设备,您必须在运行时请求权限

For android 4, apprently adding android:maxSdkVersion="18" generate this exception, try by removing it 对于android 4,显然添加android:maxSdkVersion="18"生成此异常,请尝试删除它

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 : 编辑 :对于Android 4.4及更高版本, 如果只想写入外部存储,则不要WRITE_EXTERNAL_STORAGE:

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()) 从Android 4.4(API级别19)开始,当您的应用想要写入外部存储上自己的应用程序特定目录(getExternalFilesDir()提供的目录)时,您的应用不再需要请求WRITE_EXTERNAL_STORAGE权限

Elsewere, for android < 4.4, you need to add android:maxSdkVersion="18" for own application-specific directories on external storage Elsewere,对于android <4.4,你需要在外部存储上为自己的应用程序特定目录添加android:maxSdkVersion="18"

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. PERMISSION_READ_WRITE_REQUEST_CODE是自定义变量,我的方式是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: 昨天终于发现CommonsWare在评论中完全解释了同样的事情:

Get rid of android:maxSdkVersion="18". 摆脱android:maxSdkVersion =“18”。 That is only valid if you are using getExternalFilesDir(), getExternalCacheDir(), and similar methods on Context. 只有在Context上使用getExternalFilesDir(),getExternalCacheDir()和类似方法时,这才有效。 You are using Environment.getExternalStorageDirectory(), so you always need WRITE_EXTERNAL_STORAGE 您正在使用Environment.getExternalStorageDirectory(),因此您始终需要WRITE_EXTERNAL_STORAGE

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.io.FileNotFoundException:/storage/sdcard/MyApp/questions.txt:打开失败:EACCES(权限被拒绝) - java.io.FileNotFoundException: /storage/sdcard/MyApp/questions.txt: open failed: EACCES (Permission denied) java.io.FileNotFoundException:/storage/emulated/0/Download/myFile.pdf:打开失败:EACCES(权限被拒绝) - java.io.FileNotFoundException: /storage/emulated/0/Download/myFile.pdf: open failed: EACCES (Permission denied) java.io.FileNotFoundException:/sdcard/testwrite.txt:打开失败:EACCES(权限被拒绝) - java.io.FileNotFoundException: /sdcard/testwrite.txt: open failed: EACCES (Permission denied) java.io.FileNotFoundException:/storage/emulated/0/New file.txt:打开失败:EACCES(权限被拒绝) - java.io.FileNotFoundException: /storage/emulated/0/New file.txt: open failed: EACCES (Permission denied) android kotlin java.io.FileNotFoundException:/storage/emulated/0/number.txt:打开失败:EACCES(权限被拒绝) - android kotlin java.io.FileNotFoundException: /storage/emulated/0/number.txt: open failed: EACCES (Permission denied) FileNotFoundException:open failed:EACCES(权限被拒绝) - FileNotFoundException: open failed: EACCES (Permission denied) Android - FileNotFoundException 打开失败:EACCES(权限被拒绝) - Android - FileNotFoundException open failed: EACCES (Permission denied) java.io.filenotfoundexeption:/storage/sdcard/app.apk打开失败的Eacces(权限被拒绝) - java.io.filenotfoundexeption :/storage/sdcard/app.apk open failed Eacces (permission denied) FileNotFoundException:/storage/emulated/0/Pictures/pic.jpg:打开失败:EACCES(权限被拒绝) - FileNotFoundException: /storage/emulated/0/Pictures/pic.jpg: open failed: EACCES (Permission denied) 无法解码流:java.io.FileNotFoundException:sdcard / name / c.jpg:打开失败:EACCES(权限被拒绝) - Unable to decode stream: java.io.FileNotFoundException: sdcard/name/ c.jpg: open failed: EACCES (Permission denied)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM