呼叫相機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中取得圖片

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章