随着Android 版本升级 Google在保护用户隐私和安全方面增加了力度 像Android 6.0 细分了普通权限和高危权限,涉及到高危权限则需要及时申请用户必须感知。Android7.0 增加了 "StrictMode API" 政策即私有目录限制访问。
下载完后安装方法,其中BuildConfig.APPLICATION_ID是app的包名
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(context.getApplicationContext(),
BuildConfig.APPLICATION_ID+".fileProvider", apkFile);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
context.startActivity(intent);
}
Android 7.0以上就不可以这么简单了,
首先是要在AndroidManifest.xml里进行注册一个组件(四大组件之一provider)
如果用的是 andriodx的库请自行替换
android:name="androidx.core.content.FileProvider"
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.yourappname.zhishan.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
然后在res下创建xml文件
provider_paths.xml里面的内容,其中com.yourappname.zhishan为包名你可以替换成你自己的包名
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="files_root"
path="Android/data/com.yourappname.zhishan/"/>
<external-path
name="external_storage_root"
path="."/>
</paths>
到这就完成了