I want to install the APK file from the storage directory using FileProvider in VS2019 - .Net Standard2.1 and Target Framework API28 for Android. But I got the error "The name 'FileProvider' does not exist in the current context".
Even am try to change .Net 2.0 same error.
Java.IO.File file = new Java.IO.File(filepath);
if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
{
Android.Net.Uri URIAPK = FileProvider.GetUriForFile(Android.App.Application.Context, Android.App.Application.Context.ApplicationContext.PackageName ".provider", filepath);
Intent intS = new Intent(Intent.ActionInstallPackage);
intS.SetData(URIAPK);
intS.SetFlags(ActivityFlags.GrantReadUriPermission);
Android.App.Application.Context.StartActivity(intS);
}
else
{
Android.Net.Uri URIAPK = Android.Net.Uri.FromFile(filepath);
Intent intS = new Intent(Intent.ActionView);
intS.SetDataAndType(URIAPK, "application/vnd.android.package-archive");
intS.SetFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intS);
}
I try to use namespace "Using Android.v4.content.FileProvider;"
still errors because missing .dll so, when am going to install Xamarin.Android.Support.Compat Nuget got the Suppression Error
NU1202 Package Xamarin.Android.Support.Compat 28.0.0.3 is not compatible with netstandard2.1 (.NETStandard,Version=v2.1). Package Xamarin.Android.Support.Compat 28.0.0.3 supports:
- monoandroid60 (MonoAndroid,Version=v6.0)
- monoandroid70 (MonoAndroid,Version=v7.0)
- monoandroid71 (MonoAndroid,Version=v7.1)
- monoandroid80 (MonoAndroid,Version=v8.0)
- monoandroid81 (MonoAndroid,Version=v8.1)
- monoandroid90 (MonoAndroid,Version=v9.0)
Give me the solution or any other methodology for Install APK file.
CodePudding user response:
But I got the error "The name 'FileProvider' does not exist in the current context"
For this error, please refer to the correct package. In common,we suggest to use the latest nuget Xamarin.AndroidX.AppCompat
In code, we should import this nuget as follows:
using AndroidX.AppCompat.App;
using AndroidX.Core.Content;
The total code of FileProvider
should be :
Android.Net.Uri URIAPK = AndroidX.Core.Content.FileProvider.GetUriForFile(Android.App.Application.Context, Android.App.Application.Context.ApplicationContext.PackageName ".provider", filepath);
In this condition,the error "The name 'FileProvider' does not exist in the current context" will disapear.
And if you want to use the latest nuget Xamarin.AndroidX.AppCompat
, you should Migrate your app to AndroidX, for more, you can check document: https://docs.microsoft.com/en-us/xamarin/android/platform/androidx#migration-tooling .
In addition,remember to add the correct additional configuration files to adhere to the new strict mode:
(AndroidX) Add the following to your AndroidManifest.xml inside the <application>
tags:
<provider android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
For more ,check:https://github.com/jamesmontemagno/MediaPlugin#android-file-provider-setup.
Note:
We strongly recommend that you upgrade to the latest Visual Studio and use the newer Nugets by default to avoid migrate and compatibility problems.