0

I try programmatically to install the APK file. I put it into the "download" folder of the device. I make everything according to the instructions from this link Installing APK

i.e. I put the xml file in Resources/xml/provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="."/>
</paths>

I add the manifest inside application tags

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

And then I try to install APK in code

public void OpenApk()
{
    var path =
        Android.OS.Environment.GetExternalStoragePublicDirectory(
        Android.OS.Environment.DirectoryDownloads);

    Java.IO.File file = new Java.IO.File(path + "/", "test.apk");

    Intent install = new Intent(Intent.ActionView);

    Android.Net.Uri apkURI = FileProvider.GetUriForFile(Forms.Context, Forms.Context.ApplicationContext.PackageName + ".provider", file);
    install.SetDataAndType(apkURI, "application/vnd.android.package-archive");
    install.AddFlags(ActivityFlags.NewTask);
    install.AddFlags(ActivityFlags.GrantReadUriPermission);

    Forms.Context.StartActivity(install);
}

But the installation fails with "There was a problem parsing the package" What can be wrong?

user2273044
  • 133
  • 2
  • 16

1 Answers1

0

I had done a simple and reproduced your problem. The cause of it is the apk file in the media Download folder.There are two solutions for you.

  1. Move the apk file to your app's own folder, such as: var path = GetExternalFilesDir(null).ToString();

  2. Grant your app the permission about accessing all the files.

    Add the permission into the AndroidManifest.xml:

    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    

    Use the following code to request the permission before you install the apk:

    if (!Android.OS.Environment.IsExternalStorageManager)
            {
                Intent intent = new Intent();
                intent.SetAction(Android.Provider.Settings.ActionManageAppAllFilesAccessPermission);
                Android.Net.Uri uri = Android.Net.Uri.FromParts("package", this.PackageName, null);
                intent.SetData(uri);
                StartActivity(intent);
            }
    

Update:

This is the OnCreate method of my MainActivity and it worked well:

  protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
        if (!Android.OS.Environment.IsExternalStorageManager)
        {
            Intent intent = new Intent();
            intent.SetAction(Android.Provider.Settings.ActionManageAppAllFilesAccessPermission);
            Android.Net.Uri uri = Android.Net.Uri.FromParts("package", this.PackageName, null);
            intent.SetData(uri);
            StartActivity(intent);
        }
    }
Liyun Zhang - MSFT
  • 1,212
  • 1
  • 1
  • 6