第一:在AndroidManifest.xml檔案下
android:name="android.support.v4.content.FileProvider"
android:authorities="com.tvbox.yoostore.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
第二: 在res目錄下建立想xml檔案夾及其xml檔案夾下建立file_paths檔案
file_paths檔案内容如下:
最後相容個版本的apk安裝方法:
public static voidinstallApp(File file) {
Intent intent =newIntent(Intent.ACTION_VIEW);
//判斷是否是AndroidN以及更高的版本
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(App.app.getApplicationContext(),"com.tvbox.yoostore"+".fileprovider",file);
intent.setDataAndType(contentUri,"application/vnd.android.package-archive");
}else{
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
App.app.getApplicationContext().startActivity(intent);
}