Android 6.0
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(install);
Android 7.0
- 打开AndroidManifest.xml
<application>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.ven.temp.fileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
</application>
androidx.core.content.FileProvider 如果报错 这个字符串删掉。输入File基本就会有提示出来了,再不行把name也删了,重新打name ,确认。选择有关FileProvider的选项
com.ven.temp 这里的字符串替换成你们自己的包名
@xml/filepaths 这里会报错 ,alt + enter ,确认,确认, 这个就新建了这个文件
- 打开filepaths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external" path="." />
//Environment.getExternalStorageDirectory() 下的所有路径
</paths>
<files-path path="path"> ======== getFilesDir() + "/path"
<cache-path path="path"> ======== getCacheDir() + "/path"
<external-files-path path="path"> ======== getExternalFilesDir("path")
- 最后和6.0几乎一样
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri contentUri = FileProvider.getUriForFile(mContext, "com.ven.temp.fileProvider", file);
install.setDataAndType(contentUri, "application/vnd.android.package-archive");
startActivity(install);
这里的com.ven.temp.fileProvider 一定要和 AndroidManifest.xml 文件中的 android:authorities= 一模一样
over over