Android在7.0以后传递Uri直接和低版本一样操作会报FileUriExposedException,涉及到了数据共享问题,具体实现:  
首先在AndroidManifest中添加如下:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | </application>...
 
 <provider
 android:name="android.support.v4.content.FileProvider"
 android:authorities="包名.fileprovider"
 android:exported="false"
 android:grantUriPermissions="true">
 <meta-data
 android:name="android.support.FILE_PROVIDER_PATHS"
 android:resource="@xml/file_paths"/>
 </provider>
 </application>
 
 | 
在属性android:authorities中值填写为包名+fileprovider,然后在meta-data中的resource中对应相关的xml文件,创建该文件并填写内容:  
| 12
 3
 4
 5
 6
 
 | <?xml version="1.0" encoding="utf-8"?><path>
 <external-path
 name="my_images"
 path=""/>
 </path>
 
 | 
其中
-  //代表的根目录: Context.getFilesDir()
-  //代表的根目录: Environment.getExternalStorageDirectory()
-  //代表的根目录: getCacheDir()
name 可以随便填写,path为共享空间,不填为整个根目录,进入代码模块:  
定义常量:  
| 12
 3
 4
 5
 6
 7
 8
 
 | private final int TAKE_PHOTO_CODE = 1000;private final int SELECT_PHOTO_CODE = 1001;
 private final int CUT_PICTURE_CODE = 1002;
 
 private File file;
 private String filePath = Constant.TAKE_PHOTO_PATH;
 private File cropFile;
 private String cropPath = Constant.CUT_PHOTO_PATH;
 
 | 
然后初始化文件,如果已经存在要删除后在创建:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | file = new File(filePath);cropFile = new File(cropPath);
 
 try {
 if (file.exists()) {
 file.delete();
 }
 file.createNewFile();
 
 if (cropFile.exists()) {
 cropFile.delete();
 }
 cropFile.createNewFile();
 } catch (IOException e) {
 e.printStackTrace();
 }
 
 | 
开始拍照,在拍照之前请检查权限,确认拥有相机权限之后调用如下方法:  
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);Uri temp;
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
 temp = FileProvider.getUriForFile(ContextHandler.currentActivity(), "包名.fileprovider", file);
 } else {
 temp = Uri.fromFile(file);
 }
 
 intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
 intent.putExtra(MediaStore.EXTRA_OUTPUT, temp);
 startActivityForResult(intent, TAKE_PHOTO_CODE);
 
 | 
调用图库就简单多了:  
| 12
 3
 
 | Intent intent = new Intent(Intent.ACTION_PICK, null);intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
 startActivityForResult(intent, SELECT_PHOTO_CODE);
 
 | 
然后是裁剪的方法:  
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 
 | public void startPhotoZoom(Uri uri) {Intent intent = new Intent("com.android.camera.action.CROP");
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 }
 intent.setDataAndType(uri, "image/*");
 
 intent.putExtra("crop", "true");
 intent.putExtra("scale", true);
 
 intent.putExtra("aspectX", 1);
 intent.putExtra("aspectY", 1);
 
 intent.putExtra("outputX", 300);
 intent.putExtra("outputY", 300);
 
 intent.putExtra("return-data", false);
 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cropFile));
 intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
 intent.putExtra("noFaceDetection", true);
 startActivityForResult(intent, CUT_PICTURE_CODE);
 }
 
 | 
拍照、选图、裁图返回来的处理:  
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 
 | @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 if (resultCode == Activity.RESULT_OK && requestCode == TAKE_PHOTO_CODE) {
 Uri mUri;
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
 mUri = FileProvider.getUriForFile(ContextHandler.currentActivity(), "com.nuhtech.cmedicine.fileprovider", file);
 }else{
 mUri = Uri.fromFile(file);
 }
 startPhotoZoom(mUri);
 } else if (resultCode == Activity.RESULT_OK && requestCode == SELECT_PHOTO_CODE) {
 if (data != null) {
 startPhotoZoom(data.getData());
 }
 } else if (resultCode == Activity.RESULT_OK && requestCode == CUT_PICTURE_CODE) {
 
 
 
 
 file.delete();
 cropFile.delete();
 }
 }
 
 |