1. 程式人生 > >Android呼叫系統相機拍照 獲取原圖

Android呼叫系統相機拍照 獲取原圖

拍照時候在onActivityResult中獲得相機拍照後點擊確定後的照片。
Android中用Intent提取縮圖和原始影象
可以接受照片的縮圖

Bundle bundle =data.getExtras();
photo = (Bitmap)bundle.get("data");

或者

photo=data.getParcelableExtra("data");

也可以接受原圖片的uri地址

在Intent呼叫相機時存一個uri

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra
(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, PHOTO_WITH_CAMERA);

然後在onActivityResult中取

 imageUri = Uri.parse(MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), f.getAbsolutePath(), null, null));

其中f 就是拍照時的指定的圖片檔案路徑

File f=new File(Environment.getExternalStorageDirectory
() +"/"+localTempImgDir+"/"+localTempImgFileName); try { imageUri = Uri.parse(MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), f.getAbsolutePath(), null, null)); //通知相簿更新 getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path))); } catch (FileNotFoundException e) { e.printStackTrace
(); }
 FileOutputStream fileOutputStream = null;
try {
// 獲取 SD 卡根目錄
String saveDir = Environment.getExternalStorageDirectory() + "/hylife_photos";
// 新建目錄
File dir = new File(saveDir);
if (!dir.exists())
dir.mkdir();
// 生成檔名
SimpleDateFormat t = new SimpleDateFormat("yyyy_MMddssSSS");
String filename = "hy" + (t.format(new Date())) + ".jpg";
// 新建檔案
File file = new File(saveDir, filename);
// 開啟檔案輸出流
fileOutputStream = new FileOutputStream(file);
// 生成圖片檔案                  photo.compress(Bitmap.CompressFormat.JPEG,100, fileOutputStream);
// 相片的完整路徑
picPath = file.getPath();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
// photo.recycle();
} catch (Exception e) {
 e.printStackTrace();
}
}
}
try {                  MediaStore.Images.Media.insertImage(getActivity().getContentResolver(),
picPath, "photos", null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 最後通知相簿更新
getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));