使用相机Intent的几种方法区别

呼叫相机,有时候需要返回图片,有时候不需要返回图片。

情况1,在app中呼叫相机,并将拍摄的图片保存到指定目录下,返回到app中:

  1. public void  startIntentForResult() {  
  2.         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  3.         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));  
  4. startActivityForResult(intent,1);
  5.     }  
在这个Intent中,需要设置action =  MediaStore.ACTION_IMAGE_CAPTURE,并且设置参数MediaStore.EXTRA_OUTPUT来存储拍摄的图片,这个参数设置了图片的存储路径,否则将找不到图片。因此关于这个图片的存储路径最好是全局变量,以便在onActivityResult()中取出。

注:在三星的galaxy 设备中,保存的图片的uri需要用intent.getData()来获取,不是将图片保存在事先设定的路径中的!!!!!


情况2,在app中呼叫相机,但并不指定目录,但需要返回Bitmap


  1. public void startIntent() {  
  2. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
    startActivity(intent);
  3.     }  


uri是空的,因为把图片封装到bundle中传递回来

需要需要在bundle中取得图片

  1. Uri uri = data.getData();  
  2. if (uri != null) {  
  3.  photo = BitmapFactory.decodeFile(uri.getPath());  
  4. }  
  5. if (photo == null) {  
  6.  Bundle bundle = data.getExtras();  
  7.  if (bundle != null) {  
  8.   photo = (Bitmap) bundle.get("data");  
  9.  } else {  
  10.   Toast.makeText(DefectManagerActivity.this,  
  11.     getString(R.string.common_msg_get_photo_failure),  
  12.     Toast.LENGTH_LONG).show();  
  13.   return;  
  14.  }  
  15. }  
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,

  1. uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bmp, null,null));  
uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bmp, null,null));



情况3,只是打开相机,不做任何处理,且将停留在相机app里,拍完一张照片后不回原来的app,且照片保存到系统的默认的照片保存路径:


  1. Intent i = new Intent();  
  2.         i.setAction(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); //  停留在Carema中  
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

呼叫相机,有时候需要返回图片,有时候不需要返回图片。

情况1,在app中呼叫相机,并将拍摄的图片保存到指定目录下,返回到app中:

  1. public void  startIntentForResult() {  
  2.         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  3.         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));  
  4. startActivityForResult(intent,1);
  5.     }  
在这个Intent中,需要设置action =  MediaStore.ACTION_IMAGE_CAPTURE,并且设置参数MediaStore.EXTRA_OUTPUT来存储拍摄的图片,这个参数设置了图片的存储路径,否则将找不到图片。因此关于这个图片的存储路径最好是全局变量,以便在onActivityResult()中取出。

注:在三星的galaxy 设备中,保存的图片的uri需要用intent.getData()来获取,不是将图片保存在事先设定的路径中的!!!!!


情况2,在app中呼叫相机,但并不指定目录,但需要返回Bitmap


  1. public void startIntent() {  
  2. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
    startActivity(intent);
  3.     }  


uri是空的,因为把图片封装到bundle中传递回来

需要需要在bundle中取得图片

  1. Uri uri = data.getData();  
  2. if (uri != null) {  
  3.  photo = BitmapFactory.decodeFile(uri.getPath());  
  4. }  
  5. if (photo == null) {  
  6.  Bundle bundle = data.getExtras();  
  7.  if (bundle != null) {  
  8.   photo = (Bitmap) bundle.get("data");  
  9.  } else {  
  10.   Toast.makeText(DefectManagerActivity.this,  
  11.     getString(R.string.common_msg_get_photo_failure),  
  12.     Toast.LENGTH_LONG).show();  
  13.   return;  
  14.  }  
  15. }  
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,

  1. uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bmp, null,null));  
uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bmp, null,null));



情况3,只是打开相机,不做任何处理,且将停留在相机app里,拍完一张照片后不回原来的app,且照片保存到系统的默认的照片保存路径:


  1. Intent i = new Intent();  
  2.         i.setAction(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); //  停留在Carema中  
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

猜你喜欢

转载自blog.csdn.net/fanleiym/article/details/50010301