亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android N 7.0中報(bào)錯(cuò):android.os.FileUriExposedException的解決方法

 更新時(shí)間:2018年05月16日 11:39:49   作者:loading  
這篇文章主要給大家介紹了關(guān)于在Android N 7.0中報(bào)錯(cuò):android.os.FileUriExposedException的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧

發(fā)現(xiàn)問題

最近在Android N 上 安裝Apk時(shí)報(bào)錯(cuò):android.os.FileUriExposedException: file:///storage/emulated/0/Download/appName-2.3.0.apk exposed beyond app through Intent.getData(),通過查找相關(guān)的資料終于找到了解決的方法,下面分享給大家,話不多說了,來一起看看詳細(xì)的介紹吧。

解決方法

1、在AndroidManifest.xml中添加如下代碼

<provider android:name="android.support.v4.content.FileProvider"
  android:authorities="app的包名.fileProvider"
  android:grantUriPermissions="true"
  android:exported="false">
  <meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/file_paths" />
</provider>

注意:

  • authorities:app的包名.fileProvider
  • grantUriPermissions:必須是true,表示授予 URI 臨時(shí)訪問權(quán)限
  • exported:必須是false
  • resource:中的@xml/file_paths是我們接下來要添加的文件

2、在res目錄下新建一個(gè)xml文件夾,并且新建一個(gè)file_paths的xml文件(如下圖)

3、打開file_paths.xml文件添加如下內(nèi)容

<?xml version="1.0" encoding="utf-8"?>
<paths>
  <external-path path="Android/data/app的包名/" name="files_root" />
  <external-path path="." name="external_storage_root" />
</paths>
  • path:需要臨時(shí)授權(quán)訪問的路徑(.代表所有路徑)
  • name:就是你給這個(gè)訪問路徑起個(gè)名字

4、修改代碼適配Android N

Intent intent = new Intent(Intent.ACTION_VIEW);
//判斷是否是AndroidN以及更高的版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", apkFile);
  intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
  intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
startActivity(intent);

1、首先我們對Android N及以上做判斷;

2、然后添加flags,表明我們要被授予什么樣的臨時(shí)權(quán)限

3、以前我們直接 Uri.fromFile(apkFile)構(gòu)建出一個(gè)Uri,現(xiàn)在我們使用FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", apkFile);

4、BuildConfig.APPLICATION_ID直接是應(yīng)用的包名

參考地址

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論