Android 相機拍照獲取圖片並儲存到指定位置。
阿新 • • 發佈:2019-02-05
public void takePhone(View view) {
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission .WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CALL_PHONE2);
} else {
takePhoto();
}
}
拍照的方法
private void takePhoto() {
destoryBimap();
String state = Environment.getExternalStorageState();
if (state.equals (Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, PHOTO_WITH_CAMERA);
} else {
Toast.makeText(getActivity(), "沒有SD卡", Toast.LENGTH _LONG).show();
}
}
返回的方法
public void onActivityResult(int req, int res, Intent data) {
switch (req) {
/**
* 拍照的請求標誌
*/
case PHOTO_WITH_CAMERA:
if (res == RESULT_OK) {
Uri uri = data.getData();
if (uri != null) {
this.photo = BitmapFactory.decodeFile(uri.getPath());
}
if (this.photo == null) {
Bundle bundle = data.getExtras();
if (bundle != null) {
this.photo = (Bitmap) bundle.get("data");
Intent intent = new Intent(getActivity(), CollectActivity.class);
intent.putExtra("bm", this.photo);
startActivity(intent);
} else {
Toast.makeText(getActivity(), "拍照失敗", Toast.LENGTH_LONG).show();
return;
}
}
FileOutputStream fileOutputStream = null;
try {
// 獲取 SD 卡根目錄
String saveDir = Environment.getExternalStorageDirectory() + "/photos";
// 新建目錄
File dir = new File(saveDir);
if (! dir.exists())
dir.mkdir();
// 生成檔名
SimpleDateFormat t = new SimpleDateFormat("yyyy_MM_ddssSSS");
String filename = "P" + (t.format(new Date())) + ".jpg";
// 新建檔案
File file = new File(saveDir, filename);
// 開啟檔案輸出流
fileOutputStream = new FileOutputStream(file);
// 生成圖片檔案
this.photo.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
// 相片的完整路徑
this.picPath = file.getPath();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
try {
MediaStore.Images.Media.insertImage(getActivity().getContentResolver(),
this.picPath, "photos", null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 最後通知相簿更新
getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
} else {
Log.i("tag", "失敗");
}
break;
default:
break;
}
}
許可權 onRequestPermissionsResult方法
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
takePhoto();
} else {
Toast.makeText(getActivity(), "許可權不夠", Toast.LENGTH_SHORT).show();
}
}
if (requestCode == 3) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
choosePhoto();
} else {
Toast.makeText(getActivity(), "許可權不夠", Toast.LENGTH_SHORT).show();
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
完