呼叫相机,有时候需要返回图片,有时候不需要返回图片。
情况1,在app中呼叫相机,并将拍摄的图片保存到指定目录下,返回到app中:
- public void startIntentForResult() {
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));
- startActivityForResult(intent,1);
- }
注:在三星的galaxy 设备中,保存的图片的uri需要用intent.getData()来获取,不是将图片保存在事先设定的路径中的!!!!!
情况2,在app中呼叫相机,但并不指定目录,但需要返回Bitmap:
- public void startIntent() {
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent); - }
uri是空的,因为把图片封装到bundle中传递回来
需要需要在bundle中取得图片
Uri uri = data.getData();
if (uri != null) {
photo = BitmapFactory.decodeFile(uri.getPath());
}
if (photo == null) {
Bundle bundle = data.getExtras();
if (bundle != null) {
photo = (Bitmap) bundle.get("data");
} else {
Toast.makeText(DefectManagerActivity.this,
getString(R.string.common_msg_get_photo_failure),
Toast.LENGTH_LONG).show();
return;
}
}
此时要将bitmap转换成uri,
uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bmp, null,null));
情况3,只是打开相机,不做任何处理,且将停留在相机app里,拍完一张照片后不回原来的app,且照片保存到系统的默认的照片保存路径:
Intent i = new Intent();
i.setAction(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); // 停留在Carema中
这里主要用了MediaStore的另一个参数,让相机正常的拍摄模式自动保存图片到系统默认的文件夹下。
注: 录制视频也会出现如上的两种情况,可参考以上的intent的设置
关于类MediaStore:http://developer.android.com/reference/android/provider/MediaStore.html