How to specify FilePickerFileTypes for Xamarin.Essentials.PickOptions?

Shantimohan Elchuri 721 Reputation points
2021-08-09T17:50:03.677+00:00

I was trying the following code which greys .txt files. What's wrong with my specifying the file types?

        FilePickerFileType customFileType =
            new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
            {
                { DevicePlatform.iOS, new[] { ".txt", ".tab", ".csv" } },
                { DevicePlatform.Android, new[] { ".txt", ".tab", ".csv" } },
                { DevicePlatform.UWP, new[] { ".txt", ".tab", ".csv" } },
            });
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,366 questions
0 comments No comments
{count} votes

Accepted answer
  1. Shantimohan Elchuri 176 Reputation points
    2021-08-16T15:35:50.223+00:00

    The following is the code that is working for Android & iOS:

            FilePickerFileType customFileType =
                new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
                {
                    { DevicePlatform.iOS, new[] { "public.text" } },
                    { DevicePlatform.Android, new[] { "text/plain" } },
                });
    

    For iOS, first I tried the following:

    { DevicePlatform.iOS, new[] { (string)MobileCoreServices.UTType.Text } },
    

    Then checked the value set to customFileType. That is "public.text". Also "Text" covers all text files including .tab and .csv though separate enumerations are available for those types.

    Still I have not yet tried for UWP project. That will take some more time for me.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Kyle Wang 5,531 Reputation points Microsoft Vendor
    2021-08-10T03:05:05.9+00:00

    Hi ShantimohanElchuri-8757,

    Welcome to our Microsoft Q&A platform!

    According to the document: PickOptions.FileTypes Property, we can know that

    every platform has its own way to specify the file types.

    On Android you can specify one or more MIME types, e.g.
    "image/png"; also wild card characters can be used, e.g. "image/*".

    On iOS you can specify UTType constants, e.g. UTType.Image.

    On UWP, specify a list of extensions, like this: ".jpg", ".png".

    So to filter the txt file, you can set the customFileType as follows.

    FilePickerFileType customFileType =  
        new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>  
        {  
            { DevicePlatform.iOS, new[] { "UTType.Text" } },  
            { DevicePlatform.Android, new[] { "text/plain" } },  
            { DevicePlatform.UWP, new[] { ".txt"} }  
        });  
    

    Regards,
    Kyle


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.