Forum General


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to save images to sqlite database

RenatoLimaRenatoLima BRMember ✭✭

Can anyone tell me how I can this by writing an image in the sqlite database, this image I am buncando from the photos folder.

I know this has slowed down my application, but since I am doing data transfer with another server, I see no other way than to save the image.

Would anyone give me an example of how to shrink the image and write to the database?



  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai


    You can serialize the image to a byte[] and then save that in the SQLite table as a "blob". Then, deserialize the data once you retrieve it back from the database.

    I would recommend serializing to a byte[] instead of encoding to a base64 string.

  • RenatoLimaRenatoLima BRMember ✭✭
    edited April 2017

    Hello @JohnMiller! Thanks for answering

    Would you have any examples in xamarin for to pass?

    In my class I have the method to search the photo, I must pass to byte[] only at the time of recording or in this method OnActivityResult I can already pass

     protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
                base.OnActivityResult(requestCode, resultCode, data);
                // Make it available in the gallery
                    Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
                    Uri contentUri = Uri.FromFile(App._file);
                    // Display in ImageView. We will resize the bitmap to fit the display.
                    // Loading the full sized image will consume to much memory
                    // and cause the application to crash.
                    int height = Resources.DisplayMetrics.HeightPixels;
                    int width = imageView.Height;
                    App.bitmap = App._file.Path.LoadAndResizeBitmap(width, height);
                    if (App.bitmap != null)
                        App.bitmap = null;
                    // Dispose of the Java side bitmap.
                    if (resultCode == Result.Ok)
                        var imageView =
                catch {
Sign In or Register to comment.