解决Android7.0系统 调用系统相机、系统播放器播放视频、切图兼容问题,报异常android.os.FileUriExposedException

原文地址:https://blog.csdn.net/msn465780/article/details/59058088?locationNum=8&fps=1


Android7.0以前获取本地文件uri用的Uri.fromFile(new File(filePath)); 后会得到一个file://,这种方式呢7.0及以后的系统版本就用不了,且会报一个异常:

android.os.FileUriExposedException
file:///storage/emulated/0/Android/data/com.alex.demo/cache/.tmp/show.mp4 exposed beyond app through Intent.getData()

举个例子:

[html]  view plain  copy
  1. String filePath = "/storage/emulated/0/Android/data/com.alex.demo/cache/.temp.jpg";  
  2.   
  3. Uri uri = Uri.fromFile(new File(filePath));  
  4.   
  5. 这个uri打印出来就是"file:///storage/emulated/0/Android/data/com.alex.demo/cache/.temp.jpg"   

注意:文件路径和uri路径是不一样的,注意区分,文件路径时没有"file://"前缀的哦


Android7.0及以上系统版本由于共享文件权限的限制,方法不一样了,需要用FileProvider.getUriForFile()

举个例子:

[html]  view plain  copy
  1. String filePath = "/storage/emulated/0/Android/data/com.alex.demo/cache/.temp.jpg";  
  2.   
  3. Uri uri = FileProvider.getUriForFile(BaseApplication.getApplication(), BaseApplication.getApplication().getPackageName() + ".FileProvider", new File(filePath));  
  4.   
  5. 这个uri打印出来是"content://com.alex.demo.FileProvider/external_storage_root/cache/.temp.jpg";  

下面介绍下我们常用的几个功能的具体代码,保证目前的主流版本都可以兼容使用。

首先声明:com.alex.demo为项目的包名,以下需要包名的地方替换即可

第一步、

 在AndroidManifest.xml中加上摄像头、读写磁盘的权限,如下

[html]  view plain  copy
  1. <uses-permission android:name="android.permission.CAMERA" />  
  2. <uses-permission android:name="android.permission.RECORD_AUDIO" />  
  3. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />  
  4. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  


第二步、

 在AndroidManifest.xml中加上自定义权限的ContentProvider,如下

[html]  view plain  copy
  1. <provider  
  2.             android:name="android.support.v4.content.FileProvider"  
  3.             android:authorities="com.alex.demo.FileProvider"  
  4.             android:exported="false"  
  5.             android:grantUriPermissions="true">  
  6.             <meta-data  
  7.                 android:name="android.support.FILE_PROVIDER_PATHS"  
  8.                 android:resource="@xml/file_paths" />  
  9.         </provider>  

[html]  view plain  copy
  1. android:authorities="com.alex.demo.FileProvider" 自定义的权限  
[html]  view plain  copy
  1. android:exported="false" 是否设置为独立进程  
[html]  view plain  copy
  1. android:grantUriPermissions="true" 是否拥有共享文件的临时权限  
[html]  view plain  copy
  1. android:resource="@xml/external_storage_root" 共享文件的文件根目录,名字可以自定义  

第三步、

在项目res目录下创建一个xml文件夹,里面创建一个file_paths.xml文件,上一步定义的什么名称,这里就什么名称,如图:


[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <paths>  
  3.     <external-path  
  4.         name="external_storage_root"  
  5.         path="." />  
  6. </paths>  

[html]  view plain  copy
  1. name="external_storage_root" 这个是根目录名称,可以自定义  

好了,基本工作准备好,下面开始具体的使用吧


1、调用系统相机

[html]  view plain  copy
  1. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  2.             intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);  
  3.             String filePath = ShowConfig.getCacheFolderPath() + File.separator + ".temp.jpg";  
  4.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {  
  5.                 mOriginUri = FileProvider.getUriForFile(BaseApplication.getApplication(), BaseApplication.getApplication().getPackageName() + ".FileProvider",  
  6.                         new File(filePath));  
  7.             } else {  
  8.                 mOriginUri = Uri.fromFile(new File(filePath));  
  9.             }  
  10.             intent.putExtra(MediaStore.EXTRA_OUTPUT, mOriginUri);  
  11.   
  12.             if(mAttachedFragment == null)  
  13.                 mAttachedActivity.startActivityForResult(intent, REQUEST_CROP_CAM_IMG_CODE);  
  14.             else  
  15.                 mAttachedFragment.startActivityForResult(intent, REQUEST_CROP_CAM_IMG_CODE);  

2、调用系统播放器播放视频

[html]  view plain  copy
  1. Intent intent = new Intent();  
  2.             intent.setAction(android.content.Intent.ACTION_VIEW);  
  3.             File file = FileUtils.createFile(mSelectedVideoPath);  
  4.             Uri uri;  
  5.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {  
  6.                 intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);  
  7.                 Uri contentUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".FileProvider", file);  
  8.                 intent.setDataAndType(contentUri, "video/*");  
  9.             } else {  
  10.                 uri = Uri.fromFile(file);  
  11.                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  12.                 intent.setDataAndType(uri, "video/*");  
  13.             }  
  14.   
  15.             startActivity(intent);  

3、切图

[html]  view plain  copy
  1. Intent intent = new Intent("com.android.camera.action.CROP");  
  2.                 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);  
  3.                 intent.setDataAndType(uri, "image/*");  
  4.                 Uri customUri = null;  
  5.                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {  
  6.                     customUri = FileProvider.getUriForFile(BaseApplication.getApplication(), BaseApplication.getApplication().getPackageName() + ".FileProvider",  
  7.                             new File(mLocalAvatarImagePath));  
  8.                 } else {  
  9.                     customUri = Uri.fromFile(new File(mLocalAvatarImagePath));  
  10.                 }  
  11.                 intent.putExtra("output", customUri);  
  12.                 intent.putExtra("crop", "true");  
  13.                 intent.putExtra("aspectX", 1);  // 裁剪框比例  
  14.                 intent.putExtra("aspectY", 0.75);  
  15. //            intent.putExtra("outputX", mOutputWidth);  // 输出图片大小  
  16. //            intent.putExtra("outputY", mOutputHeight);  
  17.                 intent.putExtra("scale", true);  // 去黑边  
  18.                 intent.putExtra("scaleUpIfNeeded", true);  // 去黑边  
  19.                 if(mAttachedFragment == null)  
  20.                     mAttachedActivity.startActivityForResult(intent, REQUEST_UPLOAD_IMG_CODE);  
  21.                 else  
  22.                     mAttachedFragment.startActivityForResult(intent, REQUEST_UPLOAD_IMG_CODE);  


4、用系统安装器安装APK

[html]  view plain  copy
  1. Uri fileUri = null;  
  2.                             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {  
  3.                                 fileUri = FileProvider.getUriForFile(BaseApplication.getApplication(), BaseApplication.getApplication().getPackageName() + ".FileProvider",  
  4.                                         new File(filePath));  
  5.                             } else {  
  6.                                 fileUri = Uri.fromFile(new File(filePath));  
  7.                             }  
  8.                             Intent installIntent = new Intent(Intent.ACTION_VIEW);  
  9.                             installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  10.                             installIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);  
  11.                             installIntent.setAction(Intent.ACTION_VIEW);  
  12.                             installIntent.setDataAndType(fileUri,  
  13.                                     "application/vnd.android.package-archive");  
  14.                             context.startActivity(installIntent);  


注意:通过content类型的uri和file类型的uri来获取文件的时候要注意方法的不同

猜你喜欢

转载自blog.csdn.net/dodod2012/article/details/80570163
今日推荐