2011-05-31 45 views
2

首先,這是我得到內存不足錯誤,而加密

java.lang.OutOfMemoryError 
at coderaustin.com.FileEncryptor.encryptFile(FileEncryptor.java:56) 
at coderaustin.com.Main.onCreate(Main.java:41) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803) 
at android.app.ActivityThread.access$2300(ActivityThread.java:135) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:144) 
at android.app.ActivityThread.main(ActivityThread.java:4937) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
at dalvik.system.NativeStart.main(Native Method) 

錯誤很明顯,我知道是什麼錯誤,我只是不知道如何避免它。這是我的應用程序,

您選擇一個文件,然後單擊加密。所以顯然從那裏它需要的文件,並使用此代碼加密

try { 
    FileInputStream inFile = new FileInputStream(f.getAbsolutePath()); 
    FileOutputStream outFile = new FileOutputStream(f.getAbsoluteFile() + ".des"); 

    PBEKeySpec keySpec = new PBEKeySpec(text.toCharArray()); 
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); 
    SecretKey passwordKey = keyFactory.generateSecret(keySpec); 
    byte[] salt = new byte[8]; 
    Random rnd = new Random(); 
    rnd.nextBytes(salt); 
    int iterations = 100; 

    PBEParameterSpec paramaterSpec = new PBEParameterSpec(salt, iterations); 

    Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); 
    cipher.init(Cipher.ENCRYPT_MODE, passwordKey, paramaterSpec); 

    outFile.write(salt); 



    byte[] input = new byte[inFile.available()]; 

    int bytesRead; 
    while ((bytesRead = inFile.read(input)) != -1) 
       { 
        byte[] output = cipher.update(input, 0, bytesRead); 
        if (output != null) outFile.write(output); 
       } 

       byte[] output = cipher.doFinal(); 
       if (output != null) outFile.write(output); 
       f.delete(); 
       inFile.close(); 
       outFile.flush(); 
       outFile.close(); 

是的我知道代碼是非常醜陋的,但它的工作原理。有什麼建議麼?

感謝所有:)

編輯:這是行56

byte[] input = new byte[inFile.available()];

+0

該文件有多大? – 2011-05-31 03:20:53

回答

5

奇:如果該文件是非常大的,我可以看到,可能是一個問題。你爲什麼一次讀取整個文件,而不是以小塊和處理方式讀取它?

編輯:試試這個。

byte[] input = new byte[4096]; 

    int bytesRead; 
    while ((bytesRead = inFile.read(input, 0, 4096)) != -1) 
       { 
        byte[] output = cipher.update(input, 0, bytesRead); 
        if (output != null) outFile.write(output); 
       } 
+0

哇,我覺得很愚蠢,這是一個非常好的點(我應該看到一個很明顯的點)。那麼這裏我最好的選擇是什麼?感謝您的幫助。 – Austin 2011-05-31 03:20:57

+0

請記住,Android應用程序的堆大小(通常爲16或24MB)是有限的,因此即使您擁有數百MB的內存,任何大於此值的文件都會成爲問題。 – sargas 2011-05-31 03:31:35

+0

非常感謝,@sargas這應該修復,即使他們只有16或24mb的堆大小的權利?由於您只加載4096字節。 – Austin 2011-05-31 03:53:22