java.io.FileNotFoundException:/storage/emulated/0/New file.txt:打开失败:EACCES(权限被拒绝)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37819550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:43:00  来源:igfitidea点击:

java.io.FileNotFoundException: /storage/emulated/0/New file.txt: open failed: EACCES (Permission denied)

javaandroidandroid-permissions

提问by Tharindu

I've been trying to encrypt files and write those files back on to the same place. But I got the error message saying "java.io.FileNotFoundException: /storage/emulated/0/New file.txt: open failed: EACCES (Permission denied)".

我一直在尝试加密文件并将这些文件写回同一个地方。但我收到错误消息说"java.io.FileNotFoundException: /storage/emulated/0/New file.txt: open failed: EACCES (Permission denied)".

My Manifestfile is this

我的Manifest文件是这个

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tdk.mytestapplication2">

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


<application
    android:allowBackup="true"

I think I have provided correct permission there. And the code I am using to encrypt files is this.

我想我已经在那里提供了正确的许可。我用来加密文件的代码是这样的。

public static void encrypt(SecretKey secretKey, String filePath){
    try {
        // Here you read the cleartext.
        FileInputStream fis = new FileInputStream(filePath);
        // This stream write the encrypted text. This stream will be wrapped by another stream.
        FileOutputStream fos = new FileOutputStream(filePath);

        // Create cipher
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        // Wrap the output stream
        CipherOutputStream cos = new CipherOutputStream(fos, cipher);

        // Write bytes
        int b;
        byte[] d = new byte[8];
        while ((b = fis.read(d)) != -1) {
            cos.write(d, 0, b);
        }

        // Flush and close streams.
        cos.flush();
        cos.close();
        fis.close();

    }catch(IOException e){
        e.printStackTrace();
    }catch (NoSuchAlgorithmException e){
        e.printStackTrace();
    }catch(NoSuchPaddingException e){
        e.printStackTrace();
    }catch(InvalidKeyException e){
        e.printStackTrace();
    }
}

And I used this method inside a button

我在按钮中使用了这个方法

Button btnEncrypt = (Button) findViewById(R.id.btnEnc);
    btnEncrypt.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            aesKey = EncAndDec.generateKey();
            String filePath = editText.getText().toString();
            //Generating the file hash
            String md5Hash = MD5Hash.getMD5(filePath);

            System.out.println(aesKey.toString());
            System.out.println(filePath);
            System.out.println(md5Hash);

            //Encrypting the file
            for(int i=1; i<100; i++) {
                EncAndDec.encrypt(aesKey, filePath);
            }
        }
    });

Still I couldn't configure this error. Please someone help!

我仍然无法配置此错误。请有人帮忙!

采纳答案by Bryan

I suspect you are running Android 6.0 Marshmallow (API 23) or later. If this is the case, you mustimplement runtime permissionsbefore you try to read/write external storage.

我怀疑您正在运行 Android 6.0 Marshmallow (API 23) 或更高版本。如果是这种情况,您必须在尝试读/写外部存储之前实现运行时权限

回答by Debasish Mondal

Implement runtime permission for running your app on Android 6.0 Marshmallow (API 23) or later.

实现运行时权限,以便在 Android 6.0 Marshmallow (API 23) 或更高版本上运行您的应用程序。

or you can manually enable the storage permission-

或者您可以手动启用存储权限-

goto settings>apps> "your_app_name" >click on it >then click permissions> then enable the storage. Thats it.

转到设置>应用程序>“your_app_name”>单击它>然后单击权限>然后启用存储。就是这样。

But i suggest go the for first one which is, Implement runtime permissions in your code.

但我建议使用第一个,即在代码中实现运行时权限。

回答by Prabhat Rai

if you are run in android 29 then you have to use scoped storage or for now, you can bypass this issue by using android:requestLegacyExternalStorage=" true" in manifest in the application tag

如果您在 android 29 中运行,那么您必须使用作用域存储,或者现在,您可以通过在应用程序标签的清单中使用 android:requestLegacyExternalStorage="true" 来绕过此问题