Forum Xamarin.Android

How do i save an image from gallery to AppData?

Hello,

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.

Thanks!

Best Answer

  • ArturMalendowiczArturMalendowicz USMember ✭✭
    edited August 2015 Accepted Answer

    @PedroNeves.7715 What about such code:

    using (var fos = new FileOutputStream(photoPath))
    {
           using (var ms = new MemoryStream())
           {
                  resizedImage.Compress(Bitmap.CompressFormat.Jpeg, 100, ms);
                  fos.Write(ms.ToArray());
           }
    }
    

Answers

  • ArturMalendowiczArturMalendowicz USMember ✭✭

    ImageSource ? That is Xamarin.Forms then. What do you mean by AppData?

  • PedroNeves.7715PedroNeves.7715 USMember ✭✭

    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.

  • ArturMalendowiczArturMalendowicz USMember ✭✭
    edited August 2015

    Then you can use MemoryStream to save your image in System.Environment.GetFolderPath(folder); where folder is one of these:

    System.Environment.SpecialFolder.Personal
    System.Environment.SpecialFolder.ApplicationData
    System.Environment.SpecialFolder.LocalApplicationData

  • PedroNeves.7715PedroNeves.7715 USMember ✭✭
    edited August 2015

    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);
                            stream.Close();
                            return path;
                        }
                    }
                }
    

    And later, i try to get it:

            public ImageSource GetImage(string path)
            {
                return ImageSource.FromFile(path);
            }
    

    And i got it binded to the layout (i named the property ImageSource and i know it is set):

    ...
                    <Image Grid.Row="1"
                           HorizontalOptions="StartAndExpand"
                           Source="{Binding ImageSource}"
                           VerticalOptions="Start">
    ...
    

    But still no image appears... So my doubt is if i am not saving the image, not loading it or not showing it correctly.

    Any thoughts?

    Thanks for your help

    btw, i tried System.IO.File.Exists and it is false before and true after the SaveImage method.

  • PedroNeves.7715PedroNeves.7715 USMember ✭✭

    I used Directory.EnumerateFiles and the files are there...

  • PedroNeves.7715PedroNeves.7715 USMember ✭✭

    I used a rooted phone to open the save image It is not valid. So I'm saving it wrong....

  • PedroNeves.7715PedroNeves.7715 USMember ✭✭

    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.

  • ArturMalendowiczArturMalendowicz USMember ✭✭
    edited August 2015 Accepted Answer

    @PedroNeves.7715 What about such code:

    using (var fos = new FileOutputStream(photoPath))
    {
           using (var ms = new MemoryStream())
           {
                  resizedImage.Compress(Bitmap.CompressFormat.Jpeg, 100, ms);
                  fos.Write(ms.ToArray());
           }
    }
    
  • PedroNeves.7715PedroNeves.7715 USMember ✭✭

    Finally, perfect!! Thank you!!

  • ServinOsmanovServinOsmanov USMember ✭✭

    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+

Sign In or Register to comment.