您的位置:首页 > 移动开发 > Android开发

android 7.0的android.os.FileUriExposedException: 问题解决

2017-10-14 00:07 645 查看
1,先在manifest里面添加红色部分
<application
android:name="xxx.xxx.xxx.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/icona"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:icon">

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="这边填写项目包名.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
2,在res文件夹下新建xml文件夹,在xml文件夹下新建provider_paths.xml然后在里面写上
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
3,在写代码的地方添加如下代码,红色部分是添加的代码
private void install(File file) {
if (file.exists() && file.length() > 0) {
Intent intent = new Intent();
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//7.0需要的
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri uriForFile = FileProvider.getUriForFile(this, "项目包名.fileprovider", file);
intent = intent.setDataAndType(uriForFile, "application/vnd.android.package-archive");
startActivity(intent);
} else {//7.0之下需要的
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);
// finish();
}
}
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐