Take Picture in Android with MediaStore.ACTION_IMAGE_CAPTURE

Previously in Android when you want to capture an Image, you have to use SurfaceView and SurfaceHolder to preview the camera. MediaStore.ACTION_IMAGE_CAPTURE was introduced in Cupcake, Android 1.5, and has been the default way to capture images captured in Android. 

What Do I Need
In Manafiest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

In your activity
private static final int TAKE_PHOTO_CODE = 1;

private void takePhoto(){
  final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) ); 
  startActivityForResult(intent, TAKE_PHOTO_CODE);
}

private File getTempFile(Context context){
  //it will return /sdcard/image.tmp
  final File path = new File( Environment.getExternalStorageDirectory(), context.getPackageName() );
  if(!path.exists()){
    path.mkdir();
  }
  return new File(path, "image.tmp");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == RESULT_OK) {
    switch(requestCode){
      case TAKE_PHOTO_CODE:
        final File file = getTempFile(this);
        try {
          Bitmap captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
          // do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc)
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      break;
    }
  }
}


Ads from Amazon: 
Explanation
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile(this) );
startActivityForResult(intent, TAKE_PHOTO_CODE);

This is the core of the code, here we call an intent from the MediaStore which would open up the camera app, then we pass the output path of the captured image to a temporary location (Always use a safe location to store the image). Here we use the Environment.getExternalStorageDirectory() which is our SDCard, again for safety reason check if the SDCard is present or not. Then we start the activity, expecting a result with code TAKE_PHOTO_CODE

final File file = getTempFile(this);
try {
Bitmap captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
}...{}

Knowing where the output of the file will be, we would open that file and place it on a bitmap where we would do our magic

Conclusion

ACTION_IMAGE_CAPTURE has done a great job so that we wont implement our own implementation of the camera app, so use it. 


原始链接:http://www.tutorialforandroid.com/2010/10/take-picture-in-android-with.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值