Xamarin Android 7+ install APK programatically


Xamarin Android 7+ install APK programatically



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.





Not a guaranteed solution, but maybe you are downloading an apk build for Android O, hence resulting in an parsing exception. Do you have any possibilities to check this?
– Iulian Popescu
Jul 10 '17 at 14:34





I have checked that the apk does install, by just clicking on it in the downloads folder and it does install and run correctly.
– Aaron Thompson
Jul 10 '17 at 14:36





@IulianPopescu You may be onto something, I have tried yet another .apk "es file explorer" just to test if that would work and it did, do you know why apps built for different android versions would not work?
– Aaron Thompson
Jul 10 '17 at 14:50




2 Answers
2


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);
}





Thank you for the response, but at the moment I am only targeting android 7+ devices. Once converted into Xamarin I believe this code is the same as mine.
– Aaron Thompson
Jul 10 '17 at 15:18



It appears that the issue in parsing the package was due to the space in the package name "Sensors%2520Multitool_1.3.0_apk-dl.com.apk".



As soon as the space was removed the package installed correctly.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages