1. 程式人生 > >app安裝報錯FileUriExposedException

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);
    }