繁体   English   中英

从 Xamarin Forms 安装 APK

[英]Install APK from Xamarin Forms

我想使用VS2019 -.Net Standard2.1 中的 FileProvider 和用于 Android 的 Target Framework API28 从存储目录安装 APK 文件。 但我收到错误"The name 'FileProvider' does not exist in the current context". 甚至我尝试更改.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);
                    }

我尝试使用命名空间"Using Android.v4.content.FileProvider;" 仍然出错,因为缺少。dll 所以,当我要安装 Xamarin.Android.Support.Compat ZE0A9608C9CC2A525001

NU1202 Package Xamarin.Android.Support.Compat 28.0.0.3 不兼容,与netstandard2.1.1(.NETStandv)不兼容。 Package Xamarin.Android.Support.Compat 28.0.0.3 支持:

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

给我安装 APK 文件的解决方案或任何其他方法。

但我收到错误“当前上下文中不存在名称‘FileProvider’”

对于这个错误,请参考正确的package。 通常,我们建议使用最新的 nuget Xamarin.AndroidX.AppCompat

在代码中,我们应该如下导入这个 nuget:

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

FileProvider的总代码应该是:

Android.Net.Uri URIAPK = AndroidX.Core.Content.FileProvider.GetUriForFile(Android.App.Application.Context, Android.App.Application.Context.ApplicationContext.PackageName + ".provider", filepath);

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

另外,记得添加正确的附加配置文件以遵守新的严格模式:

(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>

有关更多信息,请查看: https://github.com/jamesmontemagno/MediaPlugin#android-file-provider-setup

笔记:

我们强烈建议您升级到最新的 Visual Studio 并默认使用较新的 Nugets 以避免迁移和兼容性问题。

暂无
暂无

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

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