Giorge Caique
Giorge Caique

Reputation: 319

Save image from uri to gallery on Xamarin Forms

I need to download an image from an Uri and save it to gallery on Xamarin Forms.

How can I achieve this?

Upvotes: 1

Views: 1900

Answers (1)

Martin Zikmund
Martin Zikmund

Reputation: 39102

You will need to use HttpClient to download the image from your given Uri. Most likely the GetAsync method will help. See documentation for more info.

Once you have the file loaded in memory, you will have to store the image into storage. If you want to have the image publicly available (not just internally to your application), you will need to do this in a platform specific manner, because each platform has different approach to storing images into photos library.

You have to look into dependency service concept for Xamarin.Forms, which allows you to create a shared, platform-agnostic interface and then provide platform specific implementations. You would then for example pass the in-memory image bytes to this interface and it would manage the platform-specific saving of the image.

For Android you will need to request internal storage access permission. See more details on in Android documentation.

For UWP you will need to declare the PicturesLibrary capability to be able to access user's pictures. You then access the location using Windows.Storage.KnownFolders.PicturesLibrary and then store the image there. See more info in documentation.

For iOS see Xamarin recipe to see how to save images into user albums. You will utilize UIImage.SaveToPhotosAlbum method. You will also need to request additional permission with NSPhotoLibraryUsageDescription. See permissions info here.

Upvotes: 1

Related Questions