安卓App报错:android.os.FileUriExposedException
生活随笔
收集整理的這篇文章主要介紹了
安卓App报错:android.os.FileUriExposedException
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
安卓7.0開始,不再允許在App中把 file://Uri 暴露給其他App,因此在代碼中需要做下版本判斷,在7.0版本及以上需要使用 FileProvider 生成 content://Uri 來代替 file://Uri。同時安卓工程需要做以下調整:
1、在 AndroidManifest.xml 的 application 標簽頁下增加 provider 聲明
<application......<providerandroid:name="android.support.v4.content.FileProvider"android:authorities="com.smartphone.wifikey.fileProvider"android:grantUriPermissions="true"android:exported="false"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/filepath" /></provider></application>以上內容需注意 android:authorites 必須填寫為實際訪問的 App 包名稱。
2、在 res 目錄下創建 xml 文件夾,新建 filepath.xml 并填寫以下內容
<?xml version="1.0" encoding="utf-8"?> <paths><external-path name="external_files" path="."/> </paths>3、代碼中增加對7.0版本的判斷與處理
private void installApk(File apkFile) {Intent intent = new Intent(Intent.ACTION_VIEW);//判斷是否是Android N以及更高的版本if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);Uri contentUri = FileProvider.getUriForFile(mContext, "com.smartphone.wifikey" + ".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);}以上代碼必須注意,getUriForFile 方法的第二個參數必須為”實際訪問的App包名.fileProvider”。另外在測試時,如果出現其他異常,留心檢查下應用權限。
有問題給我郵件或者評論哦,覺得對你有幫助就點贊吧~:-)
總結
以上是生活随笔為你收集整理的安卓App报错:android.os.FileUriExposedException的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: coreldraw快速撤回_CorelD
- 下一篇: 常用sed四个功能