简体   繁体   English

从 Xamarin Forms 安装 APK

[英]Install APK from Xamarin Forms

I want to install the APK file from the storage directory using FileProvider in VS2019 -.Net Standard2.1 and Target Framework API28 for Android.我想使用VS2019 -.Net Standard2.1 中的 FileProvider 和用于 Android 的 Target Framework API28 从存储目录安装 APK 文件。 But I got the error "The name 'FileProvider' does not exist in the current context".但我收到错误"The name 'FileProvider' does not exist in the current context". Even am try to change.Net 2.0 same error.甚至我尝试更改.Net 2.0 相同的错误。

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;"我尝试使用命名空间"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仍然出错,因为缺少。dll 所以,当我要安装 Xamarin.Android.Support.Compat ZE0A9608C9CC2A525001

NU1202 Package Xamarin.Android.Support.Compat 28.0.0.3 is not compatible with netstandard2.1 (.NETStandard,Version=v2.1). NU1202 Package Xamarin.Android.Support.Compat 28.0.0.3 不兼容,与netstandard2.1.1(.NETStandv)不兼容。 Package Xamarin.Android.Support.Compat 28.0.0.3 supports: Package Xamarin.Android.Support.Compat 28.0.0.3 支持:

  • monoandroid60 (MonoAndroid,Version=v6.0) monoandroid60 (MonoAndroid,版本=v6.0)
  • monoandroid70 (MonoAndroid,Version=v7.0) monoandroid70 (MonoAndroid,版本=v7.0)
  • monoandroid71 (MonoAndroid,Version=v7.1) monoandroid71 (MonoAndroid,版本=v7.1)
  • monoandroid80 (MonoAndroid,Version=v8.0) monoandroid80 (MonoAndroid,版本=v8.0)
  • monoandroid81 (MonoAndroid,Version=v8.1) monoandroid81 (MonoAndroid,版本=v8.1)
  • monoandroid90 (MonoAndroid,Version=v9.0) monoandroid90 (MonoAndroid,版本=v9.0)

Give me the solution or any other methodology for Install APK file.给我安装 APK 文件的解决方案或任何其他方法。

But I got the error "The name 'FileProvider' does not exist in the current context"但我收到错误“当前上下文中不存在名称‘FileProvider’”

For this error, please refer to the correct package.对于这个错误,请参考正确的package。 In common,we suggest to use the latest nuget Xamarin.AndroidX.AppCompat通常,我们建议使用最新的 nuget Xamarin.AndroidX.AppCompat

In code, we should import this nuget as follows:在代码中,我们应该如下导入这个 nuget:

using AndroidX.AppCompat.App;
using AndroidX.Core.Content; 

The total code of FileProvider should be: FileProvider的总代码应该是:

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.在这种情况下,错误“当前上下文中不存在名称'FileProvider'”将消失。

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 . 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: (AndroidX) 将以下内容添加到<application>标记内的 AndroidManifest.xml 中:

   <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 .有关更多信息,请查看: 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.我们强烈建议您升级到最新的 Visual Studio 并默认使用较新的 Nugets 以避免迁移和兼容性问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM