Xamarin.Forms中FileProvider报错:'context'当前上下文未定义求解决
Hey there, let's get that FileProvider working for you! The error you're seeing is because the code references a context variable that hasn't been defined in your current scope. In Xamarin.Forms, we need to use a valid Android Context instance—here's how to fix it step by step:
1. Get a Valid Context Instance
In your Xamarin.Forms Android project, you have two reliable ways to grab a working Context:
- Use your MainActivity instance: Since you're already using
MainActivity.Instance.StartActivity(email), it looks like you've set up a staticInstanceproperty in yourMainActivity—this is a perfect, scoped context to use. - Use the global Application Context:
Android.App.Application.Contextgives you the app-wide context, which works for most FileProvider use cases too.
2. Fix Your Code
Your original code has a mix of Java-style syntax (like getApplicationContext()) that won't work in C#. Here's the corrected version with proper context handling:
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); // Define your context using MainActivity's instance (or Android.App.Application.Context) var context = MainActivity.Instance; Uri path = FileProvider.GetUriForFile( context, context.ApplicationContext.PackageName + ".provider", file ); file.SetReadable(true, false); file.SetWritable(true, false); // Note: SetType should only be called once—we'll set it to message/rfc822 at the end 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"); // Add a chooser to let the user pick their email app (optional but user-friendly) MainActivity.Instance.StartActivity(Intent.CreateChooser(email, "Send email..."));
Key Fixes:
- Defined
var context = MainActivity.Instanceto access a valid Context object - Replaced Java's
getApplicationContext().getPackageName()with C#'scontext.ApplicationContext.PackageNameproperty - Fixed duplicate
SetTypecalls (only the last one takes effect, so we set it tomessage/rfc822once) - Added
Intent.CreateChooserto make the email app picker appear for better UX
3. Double-Check Your FileProvider Configuration
Just to rule out other potential issues, make sure your AndroidManifest.xml has the FileProvider registered correctly:
<application ...> <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> </application>
And ensure you have a Resources/xml/file_paths.xml file that grants access to the Downloads folder:
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="downloads" path="Download/" /> </paths>
That should resolve the context error and get your FileProvider working to attach files in emails!
内容的提问来源于stack exchange,提问作者Tony Christie

