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:
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
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:
⭐ 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.
And done! Our File Picker is added to our App!
Thanks for reading
Spanish post: https://es.askxammy.com/conociendo-el-file-picker-en-xamarin-forms/
Reference: https://docs.microsoft.com/en-us/xamarin/essentials/file-picker?tabs=android?WT.mc_id=DT-MVP-5003353
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!
Hello Celeste 🙈 It’s a pleasure for me to know it! 😍😍
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
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!