1

When I try to select an image using the "File Picker" in Xamarin.Essentials, I can't select it only in the iOS simulator.

enter image description here

Xamarin.Essentials: File Picker
https://learn.microsoft.com/en-us/xamarin/essentials/file-picker?context=xamarin%2Fandroid&tabs=ios

var options = new PickOptions
{
  PickerTitle = "Please select files",
  FileTypes = FilePickerFileType.Images,
};

IEnumerable<FileResult> results = await FilePicker.PickMultipleAsync(options);

FilePickerFileType.Jpeg setting did not work for me. In addition, I could not select a PDF file with FilePickerFileType.Pdf settings.

However, when I launched the Android simulator, I was able to select the image successfully; only the iOS simulator was unable to select it.

Since PickerTitle is also not reflected, it seems that options itself is not enabled.

Also, even if I modified the customFileType area as pointed out in the answer below, it still did not allow me to select files.

var customFileType =
  new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
  {
      {DevicePlatform.iOS, new[] { "com.adobe.pdf" , "public.jpeg" } },
  });

var options = new PickOptions
  {
      PickerTitle = "Please select files",
      //FileTypes = FilePickerFileType.Images,
      FileTypes = customFileType,
  };

IEnumerable<FileResult> results = await FilePicker.PickMultipleAsync(options);

Do I need to set anything else?

  • Apple M1(macOS Monterey)
  • Visual Studio for Mac 8.10.17
  • Xamarin.Essentials 1.7.0
  • Xamarin.Forms 5.0.0.2291
hibara
  • 137
  • 11

1 Answers1

0

Since your FileTypes property is set to FilePickerFileType.Images, pdf files are not allowed to select. So when setting FileTypes, you need to add it.

Here is the background code:

private void Button_Clicked(object sender, EventArgs e)
{
    Task<FileResult> results =  PickAndShow();
}
async Task<FileResult> PickAndShow()
{
    var customFileType =
        new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
        {
            {DevicePlatform.iOS, new[] { "com.adobe.pdf" , "public.image" } },
        });
    try
    {
        var result = await FilePicker.PickAsync(new PickOptions
        {
            PickerTitle = "Please select files",
            FileTypes = customFileType,  
        }) ;
        if (result != null)
        {
            Text = $"File Name: {result.FileName}";
            if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
                result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
            {
                var stream = await result.OpenReadAsync();
                Image = ImageSource.FromStream(() => stream);
            }
        }
        return result;
    }
    catch (Exception ex)
    {
        // The user canceled or something went wrong
    }
    return null;
}

The main difference is that I create a customFileType and use it when setting FileTypes.

Wen xu Li
  • 1,698
  • 1
  • 4
  • 7
  • I created a `customFileType` as advised and invoked the File Picker, but I still could not select the image file. PDF files cannot be selected as well. I have added this to the question above. Is there anything else you can think of? – hibara Jan 26 '22 at 10:25
  • 1
    I copied your updated code completely, and the result can still be selected as pdf. It is recommended that you test it with a real machine. – Wen xu Li Jan 27 '22 at 01:49
  • It worked on the real machine! So it looks like there is a problem with the simulator in my environment. Thank you very much. – hibara Jan 27 '22 at 05:38