Xamarin.forms FileProvider.GetUriForFile-当前上下文中不存在名称“ context”

时间:2019-11-29 15:40:32

标签: xamarin.forms android-context android-fileprovider

我的代码有问题,我在使用FileProvider时使用了一些指导,到目前为止,我不确定如何定义“上下文”,请有人指出正确的方向,以便我使FileProvider正常工作。 下面是我的代码:

        Java.IO.File dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
        Java.IO.File file = new Java.IO.File(dir, "Write.txt");
        Intent email = new Intent(Intent.ActionSend);

        Uri path = FileProvider.GetUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", file);

        file.SetReadable(true, false);
        file.SetWritable(true, false);


        email.SetType("text/plain");
        email.PutExtra(Intent.ExtraEmail, Recipients);
        email.PutExtra(Intent.ExtraCc, new string[] { "email@example.com" });
        email.PutExtra(Intent.ExtraSubject, Subject);
        email.PutExtra(Intent.ExtraText, EmailBodytxt);
        email.PutExtra(Intent.ExtraStream, path);
        email.SetType("message/rfc822");
        MainActivity.Instance.StartActivity(email);

3 个答案:

答案 0 :(得分:0)

尝试使用大写C的上下文,请尽可能在代码中显示整个过程。

答案 1 :(得分:0)

要正确设置上下文,您应该按照Dave's Tech Blog suggest进行操作:

您应该创建一个MainApplication class并使其源自IActivityLifecycleCallbacks

using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;

namespace MyApp.Droid
{
    [Application]
    public partial class MainApplication : Application, Application.IActivityLifecycleCallbacks
    {
        internal static Context CurrentContext { get; private set; }

        internal static String FileProviderAuthority
        {
            get => MainApplication.CurrentContext.ApplicationContext.PackageName + ".fileprovider";
        }

        public MainApplication(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)
        {
        }

        public override void OnCreate()
        {
            base.OnCreate();
            RegisterActivityLifecycleCallbacks(this);
        }

        public override void OnTerminate()
        {
            base.OnTerminate();
            UnregisterActivityLifecycleCallbacks(this);
        }

        public void OnActivityCreated(Activity activity, Bundle savedInstanceState)
        {
            CurrentContext = activity;
        }

        public void OnActivityDestroyed(Activity activity)
        {

        }

        public void OnActivityPaused(Activity activity)
        {

        }

        public void OnActivityResumed(Activity activity)
        {
            CurrentContext = activity;
        }

        public void OnActivitySaveInstanceState(Activity activity, Bundle outState)
        {

        }

        public void OnActivityStarted(Activity activity)
        {
            CurrentContext = activity;
        }

        public void OnActivityStopped(Activity activity)
        {

        }
    }
}

引用戴夫:

  

每当创建,启动或恢复活动时,都会使用对当前活动的引用来更新CurrentContext属性。因此,此属性将始终具有对当前Activity的引用,然后可以将其用作本地上下文。

使用此方法,您可以简单地更改上下文引用,例如

Uri path = FileProvider.GetUriForFile(MainApplication.CurrentContext, MainApplication.FileProviderAuthority, file);

请注意,我也在FileProviderAuthority中定义了MainApplication

希望这对您有所帮助。

答案 2 :(得分:0)

Uri path = FileProvider.GetUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", file);

此行似乎是用Java编写的。

您可以尝试像这样进行更改:

Context context = Android.App.Application.Context;
Android.Net.Uri path = FileProvider.GetUriForFile(context, context.PackageName+".provider", file);// make sure ".provider" is the same as "android:authorities" you defined in your AndroidManifest provider

我认为您有一个静态变量MainActivity.Instance。有时您也可以将其用作上下文。

Android.Net.Uri path = FileProvider.GetUriForFile(MainActivity.Instance, MainActivity.Instance.PackageName+ ".provider", file);