I've got problem about intent in the Xamarin.Forms. I want to install downloaded APK file,which stored in the Internal Storage root. I have written code and it works perfectly on the Android 6 but it doesn't work on the Android 8 and 9. I used FileProvider class,I know that it's a way for Android 7+ in this case,but it still doesn't work.
no any error,it's opening packageInstaller and closing immediately,but when I tried absolutely same code in the new project,it works perfectly.
I checked,when file name is incorrect it's showing parsing error. also I checked that File.EXists method returns true with this APK file
I have no ideas,why happens it
tried update Xamarin.Forms NuGet
******MainActivity******
private void DetailPage_openit()
{
CrossPermissions.Current.RequestPermissionsAsync(Plugin.Permissions.Abstractions.Permission.Storage);
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(FileProvider.FileProvider.GetUriForFile(this, "aaaa", new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + "/gcam14.apk")), "application/vnd.android.package-archive");
intent.SetFlags(ActivityFlags.NewTask);
intent.SetFlags(ActivityFlags.GrantReadUriPermission);
StartActivity(intent);
}
******AndroidManifest.xml******
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:targetSandboxVersion="1" android:versionCode="1" android:versionName="1.0" package="com.granturismo.gcamator" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<application android:label="Gcamator" android:usesCleartextTraffic="true" android:icon="@drawable/logo" android:allowBackup="false">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="aaaa"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
<uses-library android:name="org.apache.http.legacy" android:required="false" />
</application>
<uses-permission android:name="android:permission.REQUEST_INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
******provider_paths.xml******
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
Before Android8.0 (android-o) : can be installed, or need to open the system Settings "install unknown application" permission, or there will be a popup to give the user a hint. ; After Android8.0, Google has further strengthened the permission management. The permanent switch of "install unknown application" permission is removed. Every time when users install the App from the location, they need to independently authorize and manually confirm the permission of the software. When we install Apk on Android8.0, we will not jump to the system App installation interface without setting or code control, which will result in the App cannot be installed.
try this :
if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) == Permission.Granted && ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) == Permission.Granted)
{
Intent intent = new Intent(Intent.ActionView);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
intent.AddFlags(ActivityFlags.NewTask);
intent.SetDataAndType(FileProvider.GetUriForFile(this, "aaaa", new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads) + "/gcam14.apk")), "application/vnd.android.package-archive");
if (Build.VERSION.SdkInt >= Build.VERSION_CODES.O)
{
bool hasInstallPermission = PackageManager.CanRequestPackageInstalls();
if (!hasInstallPermission)
{
//Request permission to install unknown application source
ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.RequestInstallPackages }, 1);
}
}
StartActivity(intent);
}
else
{
ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.WriteExternalStorage, Manifest.Permission.ReadExternalStorage }, 1);
}