Xamarin Essentials

Knowing the File Picker in Xamarin Forms

Howdy! 💁‍♀️ Happy new year!! 🎄Welcome to my first post of the year! This time we will be learning about a very interesting topic that was recently included in Xamarin Essentials, this is the File Picker!! 😮


First of all… What do I need?

Let’s add some platform settings. To implement it, follow the instructions added below:

La imagen tiene un atributo ALT vacío; su nombre de archivo es if_android_313085.png

We have more than one way to do it, let’s see:

1⃣  Right click in your Android project ➖ Options ➖Android Application ➖ Required permissions ➖  ReadExternalStorage

La imagen tiene un atributo ALT vacío; su nombre de archivo es FilePicker_AndroidSettings.gif

2⃣ Or in your  AssemblyInfo.cs file under the Properties folder and add the following code:

🚧 For IOS and UWP you don’t need an additional configuration.


Let’s start!

What is FilePicker.PickAsync()?

Is the method that allows the user to pick a file from the device. This method receives as parameter a PickOptions, which is responsible to start the picker for selecting a single file.

The PickOptions class contains two properties, which are the following:

La imagen tiene un atributo ALT vacío; su nombre de archivo es Screen-Shot-2021-01-07-at-10.59.49-PM-1024x648.png

⭐ Having seen the graphic explanation. We’ll be implementing the code, which allows us to open the File Picker. In this example we are filtering only for  .Jpeg files.

And finally, let’s add a block of code that allows us to show the image obtained in our UI. (Please add it inside the previous code added above, just where say: "// Here add the code that is being explained in the next step")


And if I want to show files other than those allowed by Default? 🧐

You can customize the types of files that you want to be filtered in your File Picker and something very important is that you can customize it by platform!  Let’s see the example!

Once finished, look for the star icon (⭐) in this post which is included above. In that code block, you just have to replace the FilePickerFileType.Jpeg with the customFileType and done!


But.. What if I want to Pick more than one file? 🧐

It’s too easy! You just have to replace the PickAsync() with the PickMultipleAsync() method. Both have the same structure, receiving PickOptions as parameter. The unique difference is that instead of a single FileResult an IEnumerable<FileResult> is returned.


Tagged , , ,

4 thoughts on “Knowing the File Picker in Xamarin Forms

  1. Hello! This is my first visit to your blog! We are a collection of volunteers and starting a new project
    in a community in the same niche. Your blog provided us useful information to work on.
    You have done a wonderful job!

  2. Buena tarde Leomaris, cuando utilicé esto funcionó bien, pero no sé en que punto ya no me funciona, ni en emulador ni en cel físico (android 9 ) ¿ me puedes indicar que debo agregar?

    gracias

    1. Hola Guillermo!

      Lo primero es tratar de identificar exactamente en que punto dejó de funcionar para poder saber como podemos solucionarlo, porfa dale un vistazo y me dejas saber y vemos que tal!

Leave a Reply

Your email address will not be published. Required fields are marked *