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

时间:2016-06-14 18:28:51

标签: java android android-permissions

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

的错误消息

我的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"

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

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();
    }
}

我在按钮

中使用了这个方法
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);
            }
        }
    });

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

4 个答案:

答案 0 :(得分:65)

如果您在Android 29上运行,则必须使用作用域存储或暂时使用以下方法来解决此问题:

android:requestLegacyExternalStorage="true"

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

答案 1 :(得分:35)

我怀疑您运行的是Android 6.0 Marshmallow(API 23)或更高版本。如果是这种情况,则在尝试读取/写入外部存储之前,必须实施runtime permissions

答案 2 :(得分:14)

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

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

转到设置&gt;应用&gt; &#34; your_app_name&#34; &gt;点击它&gt;然后点击权限&gt;然后启用存储。多数民众赞成。

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

答案 3 :(得分:0)

对于SDK 29:

String str1 = "";
folder1 = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)));
if (folder1.exists()) {str1 = folder1.toString() + File.separator;}

public static void createTextFile(String sBody, String FileName, String Where) {
    try {
        File gpxfile = new File(Where, FileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

然后您可以像这样保存文件:

createTextFile("This is Content","file.txt",str1);
相关问题