Forum General

Announcement:

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?

thank

Answers

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @RenatoLima,

    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
                try
                {
                    Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
                    Uri contentUri = Uri.FromFile(App._file);
                    mediaScanIntent.SetData(contentUri);
                    SendBroadcast(mediaScanIntent);
    
                    // 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)
                    {
                        imageView.SetImageBitmap(App.bitmap);
                        App.bitmap = null;
    
    
                    }
    
                    // Dispose of the Java side bitmap.
                    GC.Collect();
                }
                catch
                {
    
    
                }
                try
                {
                    if (resultCode == Result.Ok)
                    {
                        var imageView =
                            FindViewById<ImageView>(Resource.Id.imageView1);
                        imageView.SetImageURI(data.Data);
    
    
                    }
                }
                catch {
    
                }
    
            }
    
Sign In or Register to comment.