It is very common nowadays to select images from the photo gallery in a mobile app. Right now there is a plugin on Xamarin Forms called Media Plugin https://github.com/jamesmontemagno/MediaPlugin where you can select an image from gallery or camera. Unfortunately, if you want to select multiple images there aren’t a lot of open source options to do so.
Here I will guide you through an implementation I did to Select Multiple images from the photo gallery in Xamarin Forms, using a dependency service. In the case of iOS it was easier because right now there is a component to do it https://components.xamarin.com/view/elcimagepicker so, I just had to do an implementation to use it in Xamarin Forms.
Here is my code:
I started by creating an interface for the dependency service to open the gallery on a PCL Xamarin Forms project:
After we implement it on iOS and Android will be able to use it this way:
Then in the Android project, I created a service to handle the photo gallery opening, also I’m using the Permission plugin for check and request permissions in Android > 6
Is important to remember adding the permissions in the Android Manifest:
The images selected are sent to the OnActivityResult method on the MainActivity, I also added an extra implementation for rotating the images, because depending on the orientation in which it was taken, will get it in that orientation, but I want to show them in portrait mode.
We have to install this component https://components.xamarin.com/view/elcimagepicker
For sending the images to the Forms projects I’m using a Messenging Center.
Also in iOS we have to add the permission for Gallery.
Xamarin Forms Implementation:
For show the images in XForms I’m using a package called FlowListView
And for caching the images and avoid memory issues:
An important detail is to clear the paths after using them, so we don’t have that local reference in our devices.
NOTE: This code is just compatible with Android > 4.3
You can download the full code here:
Happy Coding! 🙂