2013-02-05 97 views
0

當我用我的Android相機拍照時,我的屏幕上有一張圖像(imageview3),我想用我的照片保存。Android:用照片上的圖像保存照片

這裏是我的onPictureTaken方法

public void onPictureTaken(byte[] data, Camera camera) { 
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/Ker"); 
    imagesFolder.mkdirs(); 
    String fileName = "Ker_.jpg"; 
    output = new File(imagesFolder, fileName); 
    ImageView view = (ImageView) gameactivity.findViewById(R.id.imageView3); 
    view.setDrawingCacheEnabled(true); 
    Bitmap b = view.getDrawingCache(); 
    FileOutputStream fos = null; 
    try { 
     fos = new FileOutputStream(output); 
    } catch (FileNotFoundException e1) { 
     e1.printStackTrace(); 
    } 
    b.compress(CompressFormat.JPEG, 95, fos); 
    try { 
     fos.write(data); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
     catch (IOException e) { 
     e.printStackTrace(); 
    } 
    camera.stopPreview(); 
} 

當我打開文件夾,保存的圖片只是imageview3黑色背景。爲什麼真正的攝像頭視圖沒有保存?

編輯 我試圖用帆布太多的東西:

output = new File(imagesFolder, fileName); 
      ImageView view = (ImageView) gameactivity.findViewById(R.id.imageView3); 
      view.setDrawingCacheEnabled(true); 
      Bitmap b = view.getDrawingCache(); 
      FileOutputStream fos = null; 
      try { 
       fos = new FileOutputStream(output); 
       fos.write(data); 
       fos.close(); 
      } catch (FileNotFoundException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
       catch (IOException e) { 
        e.printStackTrace(); 
      } 
      FileOutputStream fos2 = null; 
      b.compress(CompressFormat.JPEG, 95, fos2); 

      try { 
       Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fos.getFD()); 
       Bitmap bitmap2 = BitmapFactory.decodeFileDescriptor(fos2.getFD()); 
       Canvas canvas = new Canvas(bitmap); 
       canvas.drawBitmap(bitmap2, null, null); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

是正確的嗎?如何將畫布保存到我的SD卡上的文件(即從文件輸出流中的畫布上寫入數據)

回答

0

您正在將來自相機的imageview和JPEG的JPEG附加到單個文件中。

爲每個文件創建一個單獨的文件輸出流,並將來自Bitmap.compress()和onPictureTaken數據數組的數據寫入它們自己的流中。

如果您想要將兩個圖像合併爲一個圖像,則需要將數據數組解碼爲位圖,然後使用Canvas將圖像位圖和相機捕獲的位圖繪製到畫布上在你想要的安排中,然後把它保存爲一個單獨的jpeg。

您不能簡單地連接兩個壓縮的JPEG比特流;該文件格式不起作用。

+0

你知道如何將Canvas保存到我的SD卡中的JPEG中,即如何從文件輸出流的畫布寫入數據? – morg

+0

好了,謝謝http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas 重要的是我們必須保存「cs」而不是畫布(請參閱鏈接) – morg