2010-02-22 77 views
35

如果要使用使用本機Android相機的內置相機活動,只需執行以下操作。在Android中使用相機活動

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     this.startActivityForResult(camera, PICTURE_RESULT); 

你想從你顯示的漂亮的相機回來的圖像 - 但如何?

+1

那麼,我的問題是:什麼PICTURE_RESULT? – 2010-11-11 10:31:40

+2

PICTURE_RESULT是一個自定義常量。您傳遞給startActivityResult()作爲requestCode的值將與您的Intent完成時傳遞給onActivityResult()的值相同,以便您知道Intent將返回結果。 – jcmcbeth 2010-11-17 15:20:28

回答

23

如果你想讓圖像重獲光彩,請將uri傳遞給EXTRA_OUTPUT內的Intent。如果你對一個小小的位圖沒有問題(你應該是這樣),那麼就像平常那樣調用intent。

現在你有兩個選擇,處理是在EXTRA_OUTPUT返回額外的,還是做圖像的URI在onActivityResult方法如下:

if (requestCode == PICTURE_RESULT) // 
      if (resultCode == Activity.RESULT_OK) { 
       // Display image received on the view 
       Bundle b = data.getExtras(); // Kept as a Bundle to check for other things in my actual code 
       Bitmap pic = (Bitmap) b.get("data"); 

       if (pic != null) { // Display your image in an ImageView in your layout (if you want to test it) 
        pictureHolder = (ImageView) this.findViewById(R.id.IMAGE); 
        pictureHolder.setImageBitmap(pic); 
        pictureHolder.invalidate(); 
       } 
      } 
      else if (resultCode == Activity.RESULT_CANCELED) {...} 
    } 

而且你去那裏!

+2

謝謝slrobert這幫了我很多。很多其他教程解釋了從頭開始實現相機功能,而不是簡單地提高「ActivityForResult」並使默認相機活動處理事物! – wired00 2011-07-10 00:17:54