Tony Christie
Tony Christie

Reputation: 1

Xamarin.forms FileProvider.GetUriForFile - The name 'context' does not exist in the current context

im having a problem with my code, i have used some direction in using FileProvider and have got so far however i am unsure how to define 'context', please could someone point me in the right direction so i can get FileProvider Working. Below is my code:

        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 protected]" });
        email.PutExtra(Intent.ExtraSubject, Subject);
        email.PutExtra(Intent.ExtraText, EmailBodytxt);
        email.PutExtra(Intent.ExtraStream, path);
        email.SetType("message/rfc822");
        MainActivity.Instance.StartActivity(email);

Upvotes: 0

Views: 717

Answers (3)

Leo Zhu
Leo Zhu

Reputation: 14956

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

this line seems to be written in Java.

you could try to change like :

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

by the way i see you have a static variable MainActivity.Instance.you could also use it as the context sometimes.

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

Upvotes: 1

deczaloth
deczaloth

Reputation: 7445

To correctly set the Context you should do as Dave's Tech Blog suggest:

You should create a MainApplication class and make it derive from 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)
        {

        }
    }
}

Quoting the Dave:

The CurrentContext property is updated with a reference to the current Activity, whenever an Activity is created, started, or resumed. Therefore this property will always have a reference to the current Activity, which can then be used as the local context.

Using this, you can simply change your context references like

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

Note that i also defined FileProviderAuthority in MainApplication!

I hope this helps you to go on.

Upvotes: 0

Guilherme Nimer
Guilherme Nimer

Reputation: 555

Try Context with capital C, if possible please show the entire procedure in the code.

Upvotes: 1

Related Questions