android 7.0 因爲Uri.fromFile引起的FileUriExposedException異常

最近又碰到因爲android 7.0 引起的兼容問題了。

android.os.FileUriExposedException: 		
file:///storage/emulated/0/DCIM/IMG_20170125_144112.jpg exposed beyond app through ClipData.Item.getUri()
    at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
    at android.net.Uri.checkFileUriExposed(Uri.java:2346)
    at android.content.ClipData.prepareToLeaveProcess(ClipData.java:832)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:8909)

原因

Android不再允許在app中把file://Uri暴露給其他app,包括但不侷限於通過Intent或ClipData 等方法。

原因在於使用file://Uri會有一些風險,比如:

  • 文件是私有的,接收file://Uri的app無法訪問該文件。
  • 在Android6.0之後引入運行時權限,如果接收file://Uri的app沒有申請READ_EXTERNAL_STORAGE權限,在讀取文件時會引發崩潰。

因此,google提供了FileProvider,使用它可以生成content://Uri來替代file://Uri


解決方案
首先聲明:com.hct.demo爲項目的包名,以下需要包名的地方替換即可

第一步.

 在AndroidManifest.xml中加上自定義權限的ContentProvider,如下

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

[html] view plain copy
  1. android:authorities="com.hct.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" 這個是根目錄名稱,可以自定義 
void shareVideoPlayer(Context context, Uri uri) {
    mUri = uri;
    if (mUri != null && mUri.toString().startsWith("file:///")) {
        mUri = FileProvider.getUriForFile(context, context.getPackageName()+ ".fileProvider", new File(mUri.getPath()));
    }
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND).setType("video/*");
    intent.putExtra(Intent.EXTRA_STREAM, mUri);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    String title = context.getResources().getString(R.string.share);
    context.startActivity(Intent.createChooser(intent, title));
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章