2012-07-30 61 views
1

我想捕捉使用Android相機意圖的圖像。相機意圖返回字節數組,當我將字節數組保存爲位圖時,我得到一個非常小的圖像,而不是基於當前相機設置(在Android手機相機中設置的1024像素)獲取圖像。保存圖像從Android相機返回的字節意圖被保存爲小圖像

通常我會從相機意圖獲取文件路徑,但不知何故,我沒有從這個設備,所以我從相機意圖返回的字節創建位圖。

有人知道這是爲什麼,以及如何解決這個問題。謝謝。

以下是我正在使用的java代碼塊。

private Intent cameraIntent = null;

public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 

cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 

} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
           super.onActivityResult(requestCode, resultCode, data); 

if (requestCode == CAMERA_PIC_REQUEST) { 
    if (resultCode == RESULT_OK) { 
     if (data != null) 
     { 
      Bitmap myImage = null; 
      Bitmap imageBitmap = (Bitmap) data.getExtras().get("data"); 
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100,stream); 
      byte[] byteArray = stream.toByteArray(); 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      myImage = BitmapFactory.decodeByteArray(byteArray, 0,byteArray.length, options); 
      fileOutputStream = new FileOutputStream(sPath); 
      BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream); 
      myImage.compress(CompressFormat.JPEG, 100, bos); 
      bos.flush(); 
      bos.close(); 
     } 
    } 
} 
} 

回答

0

data.getExtras("data")只返回縮略圖圖像。要獲得全尺寸的圖像,您需要傳遞相機意圖的文件,在其中存儲該圖像並稍後檢索。一個粗略的例子如下。

啓動意圖:

File dir = new File(Environment.getExternalStorageDirectory() + "/dcim/myappname"); 
File mFile = File.createTempFile("myImage", ".png", dir); 

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile)); 
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 

onActivityResult

if (resultCode == RESULT_OK) { 
    Bitmap bm = BitmapFactory.decodeFile(mFile.getAbsolutePath()); 
} 
// do whatever you need with the Bitmap 

記住MFILE必須是全球性的,或在某種程度上依然存在,以便它可以在必要時調用。