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

Xamarin Forms自动生成AndroidManifest.xml时,如何添加<provider>修复相机问题?

Fixing Camera File Provider Issue in Xamarin Forms After Android Upgrade

Hey there, let's tackle this camera problem you're facing after upgrading your Android system. I've dealt with this exact scenario before, so here's a clear, step-by-step solution to get your camera working again:

1. Create the File Provider Path Configuration File

First, we need to define which file paths our app will share via File Provider.

  • In your Xamarin Android project, navigate to the Resources folder. If there's no xml subfolder, create one.
  • Inside the xml folder, add a new XML file named file_paths.xml with this content:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Use this if your images are stored in the app's private external Pictures directory -->
    <external-files-path name="my_images" path="Pictures" />
    
    <!-- Uncomment below if you need to share from the root external storage (less secure) -->
    <!-- <external-path name="external_files" path="." /> -->
</paths>

Note: Choose the path that matches where you're saving your camera images. external-files-path is recommended because it's app-specific and doesn't require extra storage permissions.

2. Add the Provider to Your AndroidManifest.xml

You mentioned your AndroidManifest is auto-generated, but don't worry—you can edit the editable manifest file in your project (found under the Properties folder, not the auto-generated one in the obj directory).

Open Properties/AndroidManifest.xml and add the <provider> node inside the <application> tag:

<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" />
</provider>

Important: If your project still uses the old Android Support Libraries (not recommended), replace androidx.core.content.FileProvider with android.support.v4.content.FileProvider. But I strongly suggest migrating to AndroidX if you haven't already—it's the modern replacement for Support Libraries.

3. Update Your Camera Intent Code

Your current code uses Uri.FromFile() which creates a file:// URI—this is blocked on Android 7.0+. Replace it with FileProvider to generate a secure content:// URI:

// Get your app's authority (matches what you set in the manifest)
var authority = PackageName + ".fileprovider";

// Generate the content URI using FileProvider
var contentUri = FileProvider.GetUriForFile(this, authority, AppClass._file);

// Update the intent with the new URI
var intent = new Intent(MediaStore.ActionImageCapture);
intent.PutExtra(MediaStore.ExtraOutput, contentUri);

// Grant temporary permissions to the camera app to access this URI
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
intent.AddFlags(ActivityFlags.GrantWriteUriPermission);

// Launch the camera
StartActivityForResult(intent, YOUR_REQUEST_CODE);

Make sure AppClass._file is saved in the same path you defined in file_paths.xml (e.g., GetExternalFilesDir(Android.OS.Environment.DirectoryPictures)).

Quick Checks to Avoid Headaches

  • Double-check that the authority value in your code matches exactly what's in the manifest (${applicationId}.fileprovider maps to your app's package name plus ".fileprovider").
  • Ensure android:exported="false" is set on the provider—this is a security requirement, as FileProvider shouldn't be exported to other apps.

内容的提问来源于stack exchange,提问作者AKrist

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:27:24