Need to install the apk which is downloaded by download manager. Installation means, showing up the installation page. This I want to achieve in Android 24 API, Nougat. I created the provider_path named file in xml folder of res. And added the provider in Android manifest. Whenever I try to pass the URI either it shows the installer is crashed or the apk is not fine. Please help.
My Code: Provider path xml provider_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>
Android Manifest
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="$$$$$.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
Run for the installer pop up:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = null;
if (apkPath.substring(0, 7).matches("file://")) {
apkPath = apkPath.substring(7);
}
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", new File(apkPath));
List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}