2016-06-14 18 views
12

Dosyaları şifrelemeye ve bu dosyaları tekrar aynı yere yazmaya çalışıyorum. Ama "java.io.FileNotFoundException:/storage/emulated/0/New file.txt: open failed: EACCES (İzin reddedildi)" hata mesajını aldım.java.io.FileNotFoundException:/storage/emulated/0/Yeni dosya.txt: open failed: EACCES (İzin reddedildi)

Benim Bildiri dosyası ben orada doğru izni vermiş düşünüyorum bu

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

olduğunu. Ve dosyaları şifrelemek için kullandığınız kod budur.

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

Ve Hala bu hatayı yapılandıramadı bir düğmeye

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

içine bu yöntemi kullandı. Lütfen birisi yardım etsin!

+0

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

+0

Lütfen onaylayın: dosya/storage/emulated/0/Yeni file.txt: 'mevcut? Cihazınıza göz atarken görüyor musunuz? – ishmaelMakitla

+0

@ishmaelMakitla hmmm "İzin Reddedildi" mesajından beri dosya var (eğer mesaj "dosya bulunamadı" ise) – niceman

cevap

23

Android 6.0 Marshmallow (API 23) veya sonraki bir sürümünü çalıştırdığınızdan şüpheleniyorum. Bu durumda, harici depolama okuma/yazma işlemine başlamadan önce runtime permissions'u uygulamanız gerekir.

+1

... yalnızca hedef SDK 23 veya üstü – devnull69

+1

Hedef SDK'm 19 ise ancak emülatör 23 – Tharindu

+0

@Tharindu Hm, @ devnull69 olarak belirtilir, eğer 'targetSdkVersion' 22 veya daha düşükse, o zaman yine de eskiyi kullanmalıdır izinler modeli. Bu sorun olmayabilir. – Bryan

3

Uygulamanızı Android 6.0 Marshmallow (API 23) veya sonraki sürümlerde çalıştırmak için çalışma zamanı iznini uygulayın.

veya manuel>

git ayarları> Uygulamalar> "your_app_name" izne saklamayı etkinleştirmek üzerine tıklayabilirsiniz> daha sonra saklamayı etkinleştirmek> izinleri tıklayın. Bu kadar.

Ancak, kodunuzdaki Çalışma zamanı izinlerini uygula ilk olanı seçmenizi öneriyorum.

İlgili konular