在Android7.0系统上,Android 框架强制执行了 StrictMode API 政策禁止向你的应用外公开 file:// URI。 如果一项包含文件 file:// URI类型 的 Intent 离开你的应用,应用失败,并出现 FileUriExposedException 异常,如调用系统相机拍照,或裁切照片。
<!--android7.0拍照 需要注册FileProvider-->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="包名.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
写完这个直接复制下面代码 ,即实现拍照及回传资源
public static final int REQUEST_CAPTURE = 10;
private Uri ImageUri;
private void initCapture(){
File outputImage=new File(getExternalCacheDir(),"remarkImage.jpg");
try {
if (outputImage.exists()){
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
if (Build.VERSION.SDK_INT>=24){
//xxxxx 你的包名
ImageUri= FileProvider.getUriForFile(FieldCardingEditActivity.this,
"com.xxxxxx.xxx.fileprovider",outputImage);
}else {
ImageUri=Uri.fromFile(outputImage);
}
//启动相机程序
Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,ImageUri);
startActivityForResult(intent,REQUEST_CAPTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent dataIntent) {
super.onActivityResult(requestCode, resultCode, dataIntent);
if(resultCode == RESULT_OK){
if(requestCode == REQUEST_CAPTURE){
try {
Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(ImageUri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
直接复制,直接用 ,别浪费时间