2016-06-14 311 views
12

我一直在試圖加密文件並將這些文件重新寫回到同一個地方。但是我收到錯誤消息:「java.io.FileNotFoundException:/ storage/emulated/0/New file.txt:open failed:EACCES(Permission denied)」。java.io.FileNotFoundException:/ storage/emulated/0/New file.txt:打開失敗:EACCES(權限被拒絕)

我的清單文件是該

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

內這種方法我仍然無法配置此錯誤。請有人幫忙!

+0

https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it – CommonsWare

+0

請同時確認:'/ storage/emulated/0 /新的file.txt:'存在?你在瀏覽設備時看到了嗎? – ishmaelMakitla

+0

@ishmaelMakitla嗯,因爲消息是「權限被拒絕」,那麼文件確實存在(如果它沒有消息將是「文件未找到」) – niceman

回答

23

我懷疑你正在運行Android 6.0 Marshmallow(API 23)或更高版本。如果是這種情況,則在嘗試讀取/寫入外部存儲器之前,您必須使用實施runtime permissions

+1

...僅當目標SDK爲23或更高時 – devnull69

+1

我的目標SDK爲19,但仿真器爲23 – Tharindu

+0

@Tharindu Hm,如@ devnull69所述,如果您的'targetSdkVersion'爲22或更低,那麼它仍應使用舊的權限模型。這可能不是問題。 – Bryan

3

在Android 6.0 Marshmallow(API 23)或更高版本上實現運行應用程序的運行時權限。

,或者您也可以手動啓用存儲permission-

轉到設置>應用程序>「your_app_name」>點擊它>然後單擊權限>然後啓用存儲。而已。

但我建議去第一個是,在您的代碼中實現運行時權限。

相關問題