调用系统相机,我们需要用到Intent,而获取拍照后的图片数据,可以使用两种方式,下面贴代码:
第一种方法:
Intent mIntent = new Intent(); mIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); mIntent.putExtra(MediaStore.Images.Media.ORIENTATION, 0); startActivityForResult(mIntent, 200);然后在onActivityResult中处理返回的数据:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode == Activity.RESULT_OK){ switch(requestCode){ case 200: //从data的Bundle中取数据 imageView.setImageBitmap((Bitmap) data.getExtras().get("data")); break; } } }
第二种方法:
File pictureFile = new File(Environment.getExternalStorageDirectory(), "picture" + System.currentTimeMillis()/1000 + ".jpg"); Intent mIntent = new Intent(); mIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); mIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(pictureFile)); mIntent.putExtra(MediaStore.Images.Media.ORIENTATION, 0); startActivityForResult(mIntent, 200);在onActivityResult中显示拍照的图片:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode == Activity.RESULT_OK){ switch(requestCode){ case REQUEST_CODE_TAKE_PICTURE: imageView.setImageBitmap(BitmapFactory.decodeFile(pictureFile.getAbsolutePath())); break; } } }