app安裝報錯FileUriExposedException

原因

從Android 7.0開始,不再允許在app中把file:// Uri暴露給其他app,否則應用會拋出FileUriExposedException。原因在於,Google認爲使用file:// Uri存在一定的風險。比如,文件是私有的,其他app無法訪問該文件,或者其他app沒有申請READ_EXTERNAL_STORAGE運行時權限。解決方案是,使用FileProvider生成content:// Uri來替代file:// Uri。

解決方案

  1. 在manifests.xml清單文件中添加:
<application>
...
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.xxxxx.FileProvider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_path"/>
</provider>
</application>

authorities屬性可自定義,exported必須設置成 false,grantUriPermissions用來控制共享文件的訪問權限。

  1. 編寫file_path.xml文件,放在res/xml目錄下
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <!--<external-path name="name" path="tienal" />-->
    <external-path
        name="tienal_external_path"
        path="." />
</paths>

path中的可配置選項

<files-path name="name" path="path" /> //相當 Context.getFilesDir() + path, name是分享url的一部分

<cache-path name="name" path="path" /> //getCacheDir()

<external-path name="name" path="path" /> //Environment.getExternalStorageDirectory()

<external-files-path name="name" path="path" />//getExternalFilesDir(String) Context.getExternalFilesDir(null)

<external-cache-path name="name" path="path" /> //Context.getExternalCacheDir()    
  1. 使用
private void installApk(String filename) {
//        Intent intent = new Intent();
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        intent.setAction(Intent.ACTION_VIEW);
//        String type = "application/vnd.android.package-archive";
//        intent.setDataAndType(Uri.fromFile(file), type);
//        startActivity(intent);

        File file = new File(filename);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri data;
        // 判斷版本大於等於7.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // "com.tienal.FileProvider"即是在清單文件中配置的authorities
            data = FileProvider.getUriForFile(mActivity, "com.tienal.FileProvider", file);
            // 給目標應用一個臨時授權
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            data = Uri.fromFile(file);
        }
        intent.setDataAndType(data, "application/vnd.android.package-archive");
        startActivity(intent);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章