//用于回显的Uri
private Uri cameraUri;
//在onActivityResult 中判断 回显 cameraUri
private int REQUEST_TAKE_PHOTO=23;
//拍照
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {catch (IOException ex) {
// Error occurred while creating the File
Log.e("TAG", "dispatchTakePictureIntent: " + ex);
}
if (photoFile != null) {
cameraUri = FileProvider.getUriForFile(this,
AppUtils.getAppPackageName() + ".fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
/**
* 将Uri 解析出文件绝对路径
*
* @param uri
* @return
*/
public static String parseOwnUri(Context context, Uri uri) {
if (uri == null) {
return null;
}
String path;
if (TextUtils.equals(uri.getAuthority(), getFileProviderName(context))) {
//camera_photos/ 要与<provider><provider/> 对应
path = new File(uri.getPath().replace("camera_photos/", "")).getAbsolutePath();
} else {
path = uri.getPath();
}
return path;
}