1. 报错
报错: android.os.FileUriExposedException: file:///storage/emulated/0/channelWe exposed beyond app through Intent.getData()
2. 出现操作
还原场景:在版本更新的时候,下载apk,但是安装失败。
3. 显示错误
Android Studio 控制台显示错误:
FATAL EXCEPTION: main
Process: com.hsy.update, PID: 29369
android.os.FileUriExposedException: file:///storage/emulated/0/channelWe exposed beyond app through Intent.getData()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1816)
at android.net.Uri.checkFileUriExposed(Uri.java:2350)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9076)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9037)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1530)
at android.app.Activity.startActivityForResult(Activity.java:4391)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivity(Activity.java:4665)
at com.hsy.update.MainActivity.update(MainActivity.java:432)
at com.hsy.update.MainActivity.access$200(MainActivity.java:43)
at com.hsy.update.MainActivity$DownloadTask.onPostExecute(MainActivity.java:330)
at com.hsy.update.MainActivity$DownloadTask.onPostExecute(MainActivity.java:191)
at android.os.AsyncTask.finish(AsyncTask.java:688)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:705)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6524)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
......
4. 分析错误
根据日志的错误,找到如上代码:
private void update() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), DOWNLOAD_NAME)),
"application/vnd.android.package-archive");
startActivity(intent);
}
最后一行 startActivity(intent),问题出在启动安装阶段。查看开发文档Android 7.0 行为变更,原来是这个问题。
5. 解决错误
- AndroidManifest.xml
<application>
....
<provider
android:authorities="项目包名.fileprovider"
android:name="android.support.v4.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
- 在 res/xml 目录下创建文件 file_paths.xml 内容如下
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="Android/data/项目的包名/" name="files_root"/>
<external-path path="." name="external_storage_root"/>
</paths>
<external-cache-path> 表示应用程序内部存储目录下的 cache/ 目录,完整路径为 Android/data/com.xxx.xxx/cache/。
path 属性用于指定子目录。
name 属性告诉 FileProvider 为 Android/data/com.xxx.xxx/cache/app/ 创建一个名为 apk 的路径字段。
标签 | 路径 |
---|---|
<files-path name="name" path="path" /> | Context.getFilesDir() |
<cache-path name="name" path="path" /> | getCacheDir() |
<external-path name="name" path="path" /> | Environment.getExternalStorageDirectory() |
<external-files-path name="name" path="path" /> | Context.getExternalFilesDir() |
<external-cache-path name="name" path="path" /> | Context.getExternalCacheDir() |
- 完成之后,修改代码
private void update() {
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(context, "com.yuneec.android.saleelfin.fileprovider", new File(filePath));
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
context.startActivity(intent);
}
最后就可以正常下载安装了。