Intent to install APK file - Android Nougat

3.8k views Asked by At

I searched extensively and did not find the problem. When you try to install an APK file using an Intent in Android Nougat, it simply does not install and displays the following warning: "There was a problem parsing the package".

It works perfectly to open PDF files, for example, with settings to open this type of file (.PDF). However to install .apk files does not work.

LogCat does not show any errors and I can not reach any solution.

What could be wrong?

The following code:

Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="br.com.xxxxxx.xxxxxx.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths"/>
    </provider>

xml/filepaths:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="storage/emulated/0" path="."/>

Activity:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        try {
            Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
            File file = new File(getContext().getFilesDir(), "app-debug.apk");
            Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file);

            intent.setDataAndType(uri, "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(intent);
            finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }else{
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            File file = new File(Environment.getExternalStorageDirectory() + "/app-debug.apk");

            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(intent);
            finish();
        } catch (Exception e) {
            e.getMessage();
        }
    }

Please, what could be wrong with this code? Can someone help me?

1

There are 1 answers

0
koocbor On

I had to change the intent for N (and higher) and remove the type designation. Once I did that the install worked as expected.

So for N:

Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
File file = new File(getContext().getFilesDir(), "app-debug.apk");
Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file);
intent.setData(uri)
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
finish();