4

I'm trying to Intent to install APK on android N. I use the following code:

File toInstall = new File(appDirectory, appName + ".apk");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    Uri apkUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", toInstall);
    Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
    intent.setData(apkUri);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    activity.startActivity(intent)
} else {
    Uri apkUri = Uri.fromFile(toInstall);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    activity.startActivity(intent);
}

but I face the following error:

    FATAL EXCEPTION: Thread-5
Process: com.myapp.myapp, PID: 16557
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:583)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)
at ir.myapp.picwriter.service.MyService$1.run(MyService.java:57)
at java.lang.Thread.run(Thread.java:761)

Which the error is in this line:

Uri apkUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", toInstall);
sajad abbasi
  • 1,988
  • 2
  • 22
  • 43
  • 1
    IIRC, that error means that Android cannot find your `FileProvider` in the manifest with your specified authority string. – CommonsWare Dec 24 '17 at 22:11
  • @CommonsWare so what should I do? – sajad abbasi Dec 24 '17 at 22:13
  • Since I cannot see your manifest, or your `FileProvider` metadata XML resource, I cannot really give you more advice. If you edit your question and add those to it, perhaps we can point out what to change. – CommonsWare Dec 24 '17 at 22:28
  • @CommonsWare I have no `FileProvider` or any `FileProvider` in my manifest I just copied the code from https://stackoverflow.com/a/40131196/7297151 and searched for error but I couldn't reach any answer so I asked a question. – sajad abbasi Dec 24 '17 at 22:42

2 Answers2

11

I had many problems with it finally I found the answer. here is the answer hope its useful for you.

    File toInstall = new File(url);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        File newFile = new File(new File(Environment.getExternalStorageDirectory(), "apps"), getImageNameByUrl(appUrl));
        Uri apkUri = getUriForFile(context, BuildConfig.APPLICATION_ID+".provider", newFile);
        Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        intent.setData(apkUri);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(intent);
    } else {
        Uri apkUri = Uri.fromFile(toInstall);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

manifest:

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

and file_paths:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="." path="." />
</paths>

and permission for API>25

<uses-permissionandroid:name="android.permission.REQUEST_INSTALL_PACKAGES" />
sajad abbasi
  • 1,988
  • 2
  • 22
  • 43
  • 4
    Also, remember to add permission `android.permission.REQUEST_INSTALL_PACKAGES` if target API > 25. Otherwise nothing will happen. – Jack May 29 '19 at 21:54
3

If the File Provider is not being found in the manifest file you have to do something like this to define it:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.mydomain.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    ...
</provider>

Here is a link to the documentation on setting up FileProviders. https://developer.android.com/reference/android/support/v4/content/FileProvider.html

beastlyCoder
  • 2,349
  • 4
  • 25
  • 52