有问题的安装:
之前可以直接这样安装apk:
/**
* @param file
* @return
* @Description 安装apk
*/
public void installApk(File file) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
但是在androud N上会报错,
android.os.FileUriExposedException
Android 7.0强制启用了被称作 StrictMode的策略,带来的影响就是你的App对外无法暴露file://类型的URI了。
如果你使用Intent携带这样的URI去打开外部App(比如:打开系统相机拍照),那么会抛出FileUriExposedException异常。
进入正题:开始解决异常
1、定义FileProvider
在Androidmanifest.xml文件中声明:
<manifest>
...
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.shawpoo.app.fileprovider" //自定义名字 为避免重复建议设为:包名.fileprovider
android:exported="false"
android:grantUriPermissions="true">
...
</provider>
...
</application>
</manifest>
2、指定可用的文件路径
在项目的res目录下,创建xml文件夹,并新建一个file_paths.xml文件。通过这个文件来指定文件路径:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="my_images" path="images/" />
...
</paths>
有多种指定路径,在标签内应至少包含一种,或者多种。
a、表示应用程序内部存储区中的文件/子目录中的文件
<files-path name="name" path="image" />
等同于Context.getFileDir() : /data/data/com.xxx.app/files/image
b、表示应用程序内部存储区缓存子目录中的文件
<cache-path name="name" path="image" />
等同于Context.getCacheDir() : /data/data/com.xxx.app/cache/image
c、表示外部存储区根目录中的文件
<external-path name="name" path="image" />
等同于Environment.getExternalStorageDirectory() : /storage/emulated/0/image
d、表示应用程序外部存储区根目录中的文件
<external-files-path name="name" path="image" />
等同于Context.getExternalFilesDir(String) / Context.getExternalFilesDir(null) : /storage/emulated/0/Android/data/com.xxx.app/files/image
e、表示应用程序外部缓存区根目录中的文件
<external-cache-path name="name" path="image" />
等同于Context.getExternalCacheDir() : /storage/emulated/0/Android/data/com.xxx.app/cache/image
3、引用指定的路径
在刚才Androidmanifest.xml中声明的provider进行关联:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.shawpoo.app.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
4、生成文件的Uri
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mUri = FileProvider.getUriForFile(MainActivity.this, "com.crg.installtest.fileprovider", mApkFile);
} else {
mUri = Uri.fromFile(mApkFile);
}
5、给Uri授予临时权限
installApkIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
所以最终安装apk的方法可以这么写了,就可以了:
mApkFile = new File(this.getExternalFilesDir("aloe"), "com.tencent.mobileqq.1708161007.apk");
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent installApkIntent = new Intent(Intent.ACTION_VIEW);
installApkIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
installApkIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mUri = FileProvider.getUriForFile(MainActivity.this, "com.crg.installtest.fileprovider", mApkFile);
} else {
mUri = Uri.fromFile(mApkFile);
}
installApkIntent.setDataAndType(mUri, "application/vnd.android.package-archive");
startActivity(installApkIntent);
}
});