如何从Camera中实现捕获图像像从android中的库中获取图像?

时间:2011-12-03 05:01:25

标签: android android-layout android-emulator camera

在我的应用程序中,我有实现从Gallery中获取图像。 在该库中单击按钮时打开,选择图像后,该活动即将关闭,所选图像将显示在上一个活动ImageView中。

就像那样,我想想象一下从相机活动中捕获图像的相机预览,并在拍摄完图像后,我带着拍摄的图像回到原来的活动。

那么如何实现这种技术? 或者是否有任何演示从Android摄像头拍摄捕获的图像并返回原始活动与捕获的图像???

感谢。

1 个答案:

答案 0 :(得分:3)

这是你想要的吗? (我理解你的问题)

在Manafiest.xml中

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

在您的活动中

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;
    }
  }
}