首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >java.io.FileNotFoundException: /存储/模拟/0/新file.txt:打开失败: EACCES (权限被拒绝)

java.io.FileNotFoundException: /存储/模拟/0/新file.txt:打开失败: EACCES (权限被拒绝)
EN

Stack Overflow用户
提问于 2016-06-14 18:28:50
回答 4查看 175K关注 0票数 77

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

我的Manifest文件是

代码语言:javascript
运行
复制
<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"

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

代码语言:javascript
运行
复制
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();
    }
}

我在一个按钮里用了这个方法

代码语言:javascript
运行
复制
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);
            }
        }
    });

但我仍然无法配置此错误。快来人帮忙!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-06-14 18:48:59

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

票数 59
EN

Stack Overflow用户

发布于 2020-03-22 08:23:52

如果您在Android 29中运行,那么您必须使用范围存储,或者就目前而言,您可以通过以下方法绕过这个问题:

android:requestLegacyExternalStorage="true"

在应用程序标记的清单中。

票数 133
EN

Stack Overflow用户

发布于 2017-10-30 13:30:33

实现在Android6.0 Marshmallow (API 23)或更高版本上运行应用程序的运行时权限。

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

goto settings>apps> "your_app_name“>单击它>然后单击permissions>,然后启用存储。就是这样。

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

票数 23
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37819550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档