Android 7 open APK with ACTION_VIEW not working (Package installer has stopped)
Asked Answered
D

2

11

My app has an auto update feature which download an APK and then uses a Intent.ACTION_VIEW to open the package installer.

Up to 7 it worked perfectly (by feeding the Intent with a normal file://)

With Android 7 I had to change to use a FileProvider. The only difference in the code is:

Intent installIntent = new Intent(Intent.ACTION_VIEW);
          if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
            installIntent.setDataAndType(uri,
                manager.getMimeTypeForDownloadedFile(downloadId));
          } else {

            Uri apkUri = FileProvider.getUriForFile(AutoUpdate.this,
                BuildConfig.APPLICATION_ID, file);
              installIntent.setDataAndType(apkUri,manager.getMimeTypeForDownloadedFile(downloadId));
          }
          activity.startActivity(installIntent);

Once the startActivity is called I get this every single time

enter image description here

Is this a bug with Android 7 ? Or something/permission is missing my side ?

EDIT AndroidManifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.READ_LOGS" />

 <application ...>
 ...
   <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.myapp"
            android:exported="false"
            android:enabled="true"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
  </application>

The path xmlfile

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="myfolder" path="."/>
</paths>
Delay answered 14/9, 2016 at 11:16 Comment(4)
Can you post your manifest?Gynecocracy
I have added it. Note that this happens on the emulator as well as on real handsetsDelay
@Delay hey i have same problem...how you fixed it...,please guid meGeometric
I have same issue. did you found the solutions?Monahon
M
9

try like below, it helped me and its working in Android N7.0.

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);
}
Monahon answered 15/11, 2017 at 13:39 Comment(2)
Thanks a lot.. should be the accpeted answerPyromorphite
Glad to hear it help :) happy codingMonahon
F
2

Try to add read uri permission to your intent:

installIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

Check this answer https://mcmap.net/q/49305/-android-install-apk-with-intent-view_action-not-working-with-file-provider

Fitzger answered 2/11, 2016 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.