Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 534
Johny19 Avatar asked Sep 14 '16 11:09

Johny19


2 Answers

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);
}
like image 119
Qadir Hussain Avatar answered Oct 24 '22 15:10

Qadir Hussain


Try to add read uri permission to your intent:

installIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

Check this answer https://stackoverflow.com/a/40131196/1912947

like image 31
Kamil Svoboda Avatar answered Oct 24 '22 15:10

Kamil Svoboda