I have a requirement where I want to download the image from a URL so that the user can see the image in the Gallery folder. Can we do this using MAUI?
Loading
I have a requirement where I want to download the image from a URL so that the user can see the image in the Gallery folder. Can we do this using MAUI?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 2, 2024, 7:50 AM EDT
Yes, you can download an image from a URL and save it to the device’s gallery using .NET MAUI. Here’s a basic example of how you can do this:
First, you need to download the image. You can use the
HttpClientclass to do this:HttpClient client = new HttpClient(); Stream stream = await client.GetStreamAsync("IMAGE_URL"); // Replace with your image URLNext, you need to save the image to the device’s gallery. This can be a bit tricky because the process is different for each platform (iOS, Android, etc.). You’ll need to use the Dependency Service to call platform-specific code from your shared code.
Here’s an example of what the interface in your shared code might look like:
public interface IPhotoLibrary { Task SavePhotoAsync(Stream stream, string folder, string filename); }You would then implement this interface in each platform project. The implementation will use platform-specific APIs to save the photo to the gallery.
For example, here’s what the implementation might look like on Android:
[assembly: Dependency(typeof(PhotoLibrary))] namespace YourNamespace.Droid { public class PhotoLibrary : IPhotoLibrary { public async Task SavePhotoAsync(Stream stream, string folder, string filename) { var file = await CrossMedia.Current.SavePhotoAsync(stream, folder, filename); // Add additional code here to save the file to the Android gallery } } }Please note that this is a simplified example and the actual code may be more complex. You’ll need to add error checking, handle permissions, etc1234.
Also, remember to replace
"IMAGE_URL"with the actual URL of the image you want to download. And replace"YourNamespace.Droid"with your actual namespace.This code also uses the
Xamarin.EssentialsandXamarin.Formsnamespaces, so make sure to install those NuGet packages in your project1234.Please note that you should comply with all terms of service of the image source. Also, be aware that the image source may have usage limits1234.
Thanks