You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin.Forms中FileProvider报错:'context'当前上下文未定义求解决

Fixing the "context does not exist" Error with FileProvider in Xamarin.Forms

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 static Instance property in your MainActivity—this is a perfect, scoped context to use.
  • Use the global Application Context: Android.App.Application.Context gives 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.Instance to access a valid Context object
  • Replaced Java's getApplicationContext().getPackageName() with C#'s context.ApplicationContext.PackageName property
  • Fixed duplicate SetType calls (only the last one takes effect, so we set it to message/rfc822 once)
  • Added Intent.CreateChooser to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:46:12