2010-02-22 74 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