3

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);
}
AADProgramming
  • 6,077
  • 11
  • 38
  • 58
DevAnuragGarg
  • 1,592
  • 2
  • 14
  • 18
  • 1
    Please provide a [mcve] demonstrating what you tried, and explain **in detail** what problems you encountered. – CommonsWare Jan 14 '17 at 14:14
  • @CommonsWare Done Sir, please check my comments – DevAnuragGarg Jan 14 '17 at 14:27
  • how are you calling your java code? – AADProgramming Jan 14 '17 at 14:32
  • Get rid of the `for` loop. Replace `intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);` with `intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_GRANT_READ_URI_PERMISSION);`. As it stands, you are granting the wrong access (write vs. read) to the wrong apps (everything that handles any form of `ACTION_VIEW` rather than just the app that will handle your request). – CommonsWare Jan 14 '17 at 14:32

0 Answers0