How can i achieve this? I already got the image in an ImageSource object taken from the gallery, and i want to save it as app data and use it afterwards.
@PedroNeves.7715 What about such code:
using (var fos = new FileOutputStream(photoPath))
using (var ms = new MemoryStream())
resizedImage.Compress(Bitmap.CompressFormat.Jpeg, 100, ms);
ImageSource ? That is Xamarin.Forms then. What do you mean by AppData?
I mean the data folder associated with the app. Where we can save files.
Yes i have ImageSource because i don't know how to solve this anyway. I'll change for whatever i need.
Then you can use MemoryStream to save your image in System.Environment.GetFolderPath(folder); where folder is one of these:
I don't know if it worked.... I'll show you my code.
I save the image (using Forms and DependencyService, i wrote this for Android):
public async Task<string> SaveImage(ImageSource imgSrc, int Id)
var renderer = new StreamImagesourceHandler();
var photo = await renderer.LoadImageAsync(imgSrc, Forms.Context);
string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = System.IO.Path.Combine(documentsPath, Id + ".png");
using (var stream = new MemoryStream())
using (FileStream file = new FileStream(path, FileMode.Create, System.IO.FileAccess.Write))
byte bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
file.Write(bytes, 0, bytes.Length);
And later, i try to get it:
public ImageSource GetImage(string path)
And i got it binded to the layout (i named the property ImageSource and i know it is set):
But still no image appears... So my doubt is if i am not saving the image, not loading it or not showing it correctly.
Thanks for your help
btw, i tried System.IO.File.Exists and it is false before and true after the SaveImage method.
I used Directory.EnumerateFiles and the files are there...
I used a rooted phone to open the save image It is not valid. So I'm saving it wrong....
I also tried:
using (var os = new FileStream(path, FileMode.CreateNew))
photo.Compress(Bitmap.CompressFormat.Jpeg, 95, os);
But still my image was not valid.
Finally, perfect!! Thank you!!
what about GIF. can you save gif animation to gallery on android 11?
I can save it on older android versions but not in 11+
Xamarin Inc., as a wholly-owned Microsoft subsidiary acting as a separate legal entity, adheres to the Microsoft Privacy Statement: Privacy & cookies