Search code examples
mauifilepicker

MAUI Can't load images from android device


I implemented the file picker as described here: learn.microsoft.com/...

When I open the android explorer, all files (jpg + png) are greyed-out / not accessible.

The important parts of my app you'll find below.

Tested on Android Pixel 5, Android 13 device.

Why I can't pick a image file from Android device?

AndroidManifest:

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

Code:

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;
}

Solution

  • Thank you very much Jason!

    The solution is to adjust the Mime types for Android.

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

    In the end I got in trouble loading images using iOS. Therefor I changed from FilePicker to MediaPicker. It is much easier and works on Windows, Android and iOS.