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 , , ,

Leave a Reply

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