I want to install a 3rd party app from the filesystem from my xamarin android app. The code I used successfully before Android 10 was pretty straightforward and easy.
Intent intent = new Intent(Intent.ActionView);
Uri data = Uri.FromFile(file);
intent.SetDataAndType(data, "application/vnd.android.package-archive");
context.StartActivity(intent);
This code does not work on Android 10 because of ACTION_VIEW and ACTION_INSTALL_PACKAGE were deprecated in Android 10. Looks like we now need to use the PackageInstaller API.
I tried to write a method using the PackageInstaller API. Unfortunately it doesn't work.
Code with PackageInstaller API
public static void InstallPackageAndroidQAndAbove(Context context, string filePath, string packageName)
{
var packageInstaller = context.PackageManager.PackageInstaller;
var sessionParams = new PackageInstaller.SessionParams(PackageInstallMode.FullInstall);
sessionParams.SetAppPackageName(packageName);
int sessionId = packageInstaller.CreateSession(sessionParams);
var session = packageInstaller.OpenSession(sessionId);
var input = new FileStream(filePath, FileMode.Open, FileAccess.Read);
var output = session.OpenWrite(packageName, 0, -1);
input.CopyTo(output);
output.Close();
input.Close();
input.Dispose();
session.Fsync(output);
var pendingIntent = PendingIntent.GetBroadcast(context, sessionId, new Intent(Intent.ActionInstallPackage), 0);
session.Commit(pendingIntent.IntentSender);
}
An exception "Unrecognized stream" occurs during the call.
I hope someone can help me.
Thank you very much in advance.