Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Android 7+ install APK programmatically

I am trying to install an .apk I have downloaded to the downloads folder in Android 7.

I have tried the way recommended in a number of StackOverflow posts and here https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en by using a FileProvider:

File file = new File(fileUri);
//using Android.Support.V4.Content;
var downloadUri = FileProvider.GetUriForFile(context,context.ApplicationContext.PackageName + ".com.package.name.provider", file);
Intent install = new Intent(Intent.ActionInstallPackage);
install.AddFlags(ActivityFlags.GrantReadUriPermission);
install.AddFlags(ActivityFlags.GrantWriteUriPermission);
install.AddFlags(ActivityFlags.GrantPersistableUriPermission);
install.SetDataAndType(downloadUri, "application/vnd.android.package-archive");
context.StartActivity(install);

AndroidManifest.xml

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<application android:label="Settings" android:icon="@drawable/Icon" android:theme="@style/myTheme">
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.com.package.name.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
</application>

provider_paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="." /> 
</paths>

The "downloadUri" looks like: "content://com.package.name.com.package.name.provider/external_files/Download/Sensors%2520Multitool_1.3.0_apk-dl.com.apk"

The error when the installation window pops up is: "There was a problem parsing the package".

I have installed this package by clicking on it in the downloads folder and it installs fine, I have also tried other .apk's with the same issue.

like image 427
Aaron Thompson Avatar asked Jul 10 '17 14:07

Aaron Thompson


People also ask

Does Xamarin support Android 7?

We’re excited to announce that we have published Xamarin.Android support for Android 7.0 for both Xamarin Studio and Visual Studio. This release of Xamarin.Android is currently available in our Beta channel, alongside our Xamarin.iOS 10 preview support.

Can I use Xamarin on Visual Studio for Mac?

By the end of this article, you will have a working Xamarin.Android installation integrated into Visual Studio for Mac, and you'll be ready to start building your first Xamarin.Android application. Visual Studio includes an Android SDK Manager that replaces Google's standalone Android SDK Manager.

What can I do with Xamarin forms?

So, there is nothing to do with Xamarin Forms. All must be done in Xamarin.Android and Xamarin.iOS. Well, question still the same - good, working, platform specific code example (or link to...) that could be used in Xamarin Forms project? :) You could download the file with a platform-agnostic web request.

What is the Android SDK manager in Visual Studio?

Visual Studio includes an Android SDK Manager that replaces Google's standalone Android SDK Manager. This article explains how to use the SDK Manager to download Android SDK tools, platforms, and other components that you need for developing Xamarin.Android apps.


Video Answer


1 Answers

File file = new File(fileUri);
if(Build.VERSION.SdkInt >= Build.VERSION_CODES.N) {
    Uri apkUri = FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".provider", toInstall);
    Intent intentS = new Intent(Intent.ActionInstallPackage);
    intentS.SetData(apkUri);
    intentS.SetFlags(ActivityFlags.GrantReadUriPermission);
    context.StartActivity(intentS);
} else {
    Uri apkUri = Uri.FromFile(toInstall);
    Intent intentS = new Intent(Intent.ActionView);
    intentS.SetDataAndType(apkUri, "application/vnd.android.package-archive");
    intentS.SetFlags(ActivityFlags.NewTask);
    context.StartActivity(intentS);
}
like image 84
h.ghanbari Avatar answered Nov 15 '22 05:11

h.ghanbari