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

MAUI在Android设备无法选择图片:文件变灰无法访问问题

问题

我按照微软官方文档实现了MAUI文件选择器,但在Android 13系统的Pixel 5设备上打开文件管理器时,所有jpg、png图片文件均显示为灰色,无法选取。以下是应用的关键配置与代码:

AndroidManifest配置

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

核心代码

private static PickOptions GetPickOptions()
{
   var customFileType = new FilePickerFileType(
       new Dictionary<DevicePlatform, IEnumerable<string>>
       {
           { DevicePlatform.iOS, new[] { ".jpg", ".jpeg", ".png" } },
           { DevicePlatform.Android, new[] { ".jpg", ".jpeg", ".png" } },
           { DevicePlatform.WinUI, new[] { ".jpg", ".jpeg", ".png" } },
           { DevicePlatform.macOS, new[] { ".jpg", ".jpeg", ".png" } }
       });

   PickOptions options = new()
   {
       PickerTitle = AppResources.Bitte_waehlen_Sie_ein_Bild,
       FileTypes = customFileType
   };

   return options;
}

public async Task<FileResult?> PickAndShow(PickOptions options)
{
   try
   {
       var result = await FilePicker.Default.PickAsync(options);

       if (result == null)
       {
           return result;
       }

       if (!result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) &&
           !result.FileName.EndsWith("jpeg", StringComparison.OrdinalIgnoreCase) &&
           !result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
       {
           ImageInformation = AppResources.Das_Dateiformat_wird_nicht_unterstuetzt;

           return result;
       }

       Image = result.FullPath;
       var length = new FileInfo(result.FullPath).Length;

       if (length > MaxFileSize)
       {
           ImageInformation = AppResources.Die_Datei_ueberschreitet_die_erlaubt_Groesse;

           return null;
       }

       ImageInformation = AppResources.Vielen_Dank_fuer_Ihr_Bild;

       return result;
   }
   catch (Exception)
   {
       ImageInformation = AppResources.Es_tut_uns_leid_beim_Upload_ist_ein_Fehler_passiert;
   }

   return null;
}
原因分析

问题出在Android平台的文件类型定义上:MAUI的FilePicker在Android端要求传入MIME类型而非文件扩展名,你当前配置的.jpg、.png属于扩展名格式,无法被Android文件选择器正确解析,导致符合条件的图片被标记为不可选。

解决方案

修改GetPickOptions方法中Android平台的文件类型配置,替换为对应的MIME类型:

private static PickOptions GetPickOptions()
{
   var customFileType = new FilePickerFileType(
       new Dictionary<DevicePlatform, IEnumerable<string>>
       {
           { DevicePlatform.iOS, new[] { ".jpg", ".jpeg", ".png" } },
           { DevicePlatform.Android, new[] { "image/jpeg", "image/png" } }, // 替换为MIME类型
           { DevicePlatform.WinUI, new[] { ".jpg", ".jpeg", ".png" } },
           { DevicePlatform.macOS, new[] { ".jpg", ".jpeg", ".png" } }
       });

   PickOptions options = new()
   {
       PickerTitle = AppResources.Bitte_waehlen_Sie_ein_Bild,
       FileTypes = customFileType
   };

   return options;
}

补充说明

  • Android系统的文件选择器依赖MIME类型进行文件过滤,image/jpeg对应jpg/jpeg格式,image/png对应png格式,配置后即可正常选取图片。
  • 你的权限配置无需调整:Android 13及以上版本使用READ_MEDIA_IMAGES权限,低于13的版本使用READ_EXTERNAL_STORAGE,符合系统权限要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 20:17:25