2012-08-16 75 views
0

我试图从图库中加密图像并将其存储在SD卡上。加密图像后无法将图像从图库移动到SD卡

当我试图将图片从图库移动到SD卡时,我得到“强制关闭”错误,logcat不显示异常。

我得到一张吐司图片“图片成功加密”,但没有将图片从图库移动到SD卡。

我在做什么错了?

 private void encript(byte[] data, byte[] clear) { 

     byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 
     0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }; 
     SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); 
     Cipher cipher = null; 

     try { 

     cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC"); 

     } catch (NoSuchAlgorithmException e) { 

     e.printStackTrace(); 
     } catch (NoSuchProviderException e) { 

     e.printStackTrace(); 

     } catch (NoSuchPaddingException e) { 

     e.printStackTrace(); 
     } 
     try { 
     cipher.init(Cipher.ENCRYPT_MODE, key); 

     } catch (InvalidKeyException e) { 

     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 

     byte[] cipherText = new byte[cipher.getOutputSize(data.length)]; 

     // int ctLength = cipher.update(data, 0, data.length, cipherText, 0); 

     byte[] input = null; 

     try { 
     input = cipher.doFinal(cipherText); 
     } catch (IllegalBlockSizeException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } catch (BadPaddingException e) { 
     // TODO Auto-generated catch block 


     e.printStackTrace(); 
     } 
     try{ 
     if(input.length>0){ 
     Toast.makeText(getApplicationContext(), "Image successfully encripted", 3000).show(); 
     } 
     ByteArrayInputStream bis = new ByteArrayInputStream(input); 

     ObjectInputStream ois = new ObjectInputStream(bis); 

    File encriptedfile = (File) ois.readObject();//This is the file which i want to Move to App folder 

      bis.close(); 
     ois.close(); 
     } catch (StreamCorruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
    return; 
    } 
    //Code for copy image into app folder...... 
    File outputFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + ".map"); 
     try { 
      FileUtils.copyFile (encriptedfile, outputFolder); 
      } 
      catch (IOException e) { 
      Log.e("photomover", e.toString()); 
      } 
} 

08-17 11:42:00.905: WARN/System.err(8453): java.io.StreamCorruptedException 
08-17 11:42:00.905: WARN/System.err(8453):  at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2392) 
08-17 11:42:00.905: WARN/System.err(8453):  at java.io.ObjectInputStream.<init>(ObjectInputStream.java:445) 
08-17 11:42:00.905: WARN/System.err(8453):  at com.cw.GalleryShareimage1Activity.encript(GalleryShareimage1Activity.java:177) 
08-17 11:42:00.905: WARN/System.err(8453):  at com.cw.GalleryShareimage1Activity.onCreate(GalleryShareimage1Activity.java:76) 
08-17 11:42:00.915: WARN/System.err(8453):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
08-17 11:42:00.915: WARN/System.err(8453):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633) 
08-17 11:42:00.915: WARN/System.err(8453):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685) 
08-17 11:42:00.915: WARN/System.err(8453):  at android.app.ActivityThread.access$2300(ActivityThread.java:126) 
08-17 11:42:00.915: WARN/System.err(8453):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038) 
08-17 11:42:00.915: WARN/System.err(8453):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-17 11:42:00.915: WARN/System.err(8453):  at android.os.Looper.loop(Looper.java:123) 
08-17 11:42:00.915: WARN/System.err(8453):  at android.app.ActivityThread.main(ActivityThread.java:4633) 
08-17 11:42:00.915: WARN/System.err(8453):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-17 11:42:00.915: WARN/System.err(8453):  at java.lang.reflect.Method.invoke(Method.java:521) 
08-17 11:42:00.915: WARN/System.err(8453):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
08-17 11:42:00.915: WARN/System.err(8453):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
08-17 11:42:00.915: WARN/System.err(8453):  at dalvik.system.NativeStart.main(Native Method) 
08-17 11:42:00.925: WARN/dalvikvm(8453): threadid=1: thread exiting with uncaught exception (group=0x400207d8) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453): FATAL EXCEPTION: main 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cw/com.cw.GalleryShareimage1Activity}: java.lang.NullPointerException: Source must not be null 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at android.app.ActivityThread.access$2300(ActivityThread.java:126) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at android.os.Looper.loop(Looper.java:123) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at android.app.ActivityThread.main(ActivityThread.java:4633) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at java.lang.reflect.Method.invoke(Method.java:521) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at dalvik.system.NativeStart.main(Native Method) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453): Caused by: java.lang.NullPointerException: Source must not be null 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1068) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1038) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at com.cw.GalleryShareimage1Activity.onCreate(GalleryShareimage1Activity.java:96) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633) 
08-17 11:42:00.935: ERROR/AndroidRuntime(8453):  ... 11 more 
+0

单步执行代码,看看发生了什么。看看它失败的地方。发布你的logcat也是有帮助的。 (即使你说它没有显示异常) – prolink007 2012-08-16 13:19:19

+0

通过获取文件名称,能够将图像复制到应用程序文件夹中,但无法将标记的文件放到应用程序文件夹中.... – user1310175 2012-08-16 13:27:41

+0

感谢您给予回复... ..之后有一段时间我得到了“java.io.StreamCorruptedException”...我怎么能避免这个请....给我任何建议.... – user1310175 2012-08-17 06:20:52

回答

0

你的代码的几个缺陷:

首先,当你对数据进行加密,你不处理数据被正确加密。你应该尝试一下这种方法:

byte[] cipherText = cipher.doFinal(data); 

其次您使用的是ObjectInputStream和它的readObject()方法。 ObjectInputStream仅对Java序列化创建的数据有用。您无法以这种方式处理图像。将它投射到File也没有任何意义。

当您在RAM中执行所有操作时(因为Android上的RAM限制,它将不能用于大文件),您只需将加密的内存数据保存到文件即可。不需要移动:

File outputFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),".map"); 
File outputFile = new File(outputFolder, "outputfilename.bin"); 
FileOutputStream out = new FileOutputStream(outputFile); 
out.write(cipherText); 
out.close; 
+0

感谢您给予回复.....其工作正常...... – user1310175 2012-08-17 10:19:20

+0

File outputFile = new File(outputFolder,imagename);当我动态给文件名是“FilenotFound异常”“outputfilename.bin”...... wt是问题......在这里...... – user1310175 2012-08-17 10:57:49

+0

请给我任何建议.... – user1310175 2012-08-17 12:17:22