Xamarin - Android: Is there a file picker which allow to filter out the format

2.9k views Asked by At

There are good file picker I have found on the internet but don't see a way to customise which file extension of files can be displayed.

I am trying out these two packages Compass File Picker and FilePicker-Plugin-for-Xamarin-and-Windows which is a cross platform. None of this I can find to filter out the file and only show certain type of files.

3

There are 3 answers

3
Elvis Xia - MSFT On

I have found on the internet but don't see a way to customise which file extension of files can be displayed.

I have checked FilePicker-Plugin-for-Xamarin-and-Windows's source codes the file type of this plugin is hardcoded by intent.SetType("*/*");. So it is not possible to use this plugin to filter a file type.

But you can create your own File Picker for filtering out the folder and file extension you wanted:

private void PickFile(string folder, string extension)
{
    Intent intent = new Intent(Intent.ActionGetContent);
    intent.SetType(folder+"/"+extension);

    intent.AddCategory(Intent.CategoryOpenable);
    try
    {
        StartActivityForResult(Intent.CreateChooser(intent, "Select a file"),
                  0);
    }
    catch (System.Exception exAct)
    {
        System.Diagnostics.Debug.Write(exAct);
    }
}

And by overriding the OnActivityResult, you will get the result of the file picker.

0
vividos On

There are some forks of the FilePicker Xamarin plugin, and there's only one that also supports specifying file types in a platform independent way: https://github.com/jfversluis/FilePicker-Plugin-for-Xamarin-and-Windows (note: I'm one of the contributors to the project).

The README.md explains that on Android you can pass MIME types of what you want to select. The actual file picker grays out files that don't match the MIME types specified. You can check the sample Forms project on how to specify the file types on other platforms. The project recently got an update, so try it out.

0
Jabir Ishaq On
private void Button1_Click(object sender, System.EventArgs e)
    {
        Intent = new Intent();
        Intent.SetType("image/*");
        Intent.SetType("pdf/*");
        Intent.SetAction(Intent.ActionGetContent);
        StartActivityForResult(Intent.CreateChooser(Intent, "Select a File"),PickImageId);
        //CreateChooser(Intent, "Select Picture"), PickImageId);
    }

This is for picking an image from dvice's gallery, and the way of setting the extension. And keep in mind if the API level of device is lower than 20, you may need to install any file explorer too.