呼叫相機,有時候需要返回圖片,有時候不需要返回圖片。
情況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