简体   繁体   English

如何将图像保存到sqlite数据库

[英]How to save images to sqlite database

In my classe I have a method that searches the image in the photo gallery and also receives the image taken from the camera of the mobile phone, I need now to save this image in the sqlite database. 在我的课堂上,我有一种方法可以搜索照片库中的图像,还可以接收从手机的摄像头拍摄的图像,现在我需要将此图像保存在sqlite数据库中。 I am using a database field like BLOB, but not like serializing the image in bity [] or transforming in decode64 to write to the database. 我正在使用像BLOB这样的数据库字段,但是不喜欢在bity []中序列化图像或在decode64中进行转换以写入数据库。

I would like to know if anyone has any examples to pass on xaramin android I am posting the method that receives the image 我想知道是否有人通过xaramin android传递任何示例,我正在发布接收图像的方法

 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 {

            }

        }

Instead of storing & retrieving image(byte[]) as blob from sqlite db convert image to base64string and store image as string in sqlite and while retrieving convert received base64string from sqlite to image(byte[]) 而不是从sqlite db以blob的形式存储和检索image(byte []),而是将图像转换为base64string并将图像以字符串形式存储在sqlite中,而在检索的同时,将接收到的base64string从sqlite转换为image(byte [])

Base64 to Bitmap : Base64到位图:

public Bitmap Base64ToBitmap(String base64String)
{
    byte[] imageAsBytes = Base64.Decode(base64String, Base64Flags.Default);
    return BitmapFactory.DecodeByteArray(imageAsBytes, 0, imageAsBytes.Length);
}

Bitmap to Base64 : 位图到Base64:

public String BitmapToBase64(Bitmap bitmap)
{
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.Compress(Bitmap.CompressFormat.Png, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.ToByteArray();
    return Base64.EncodeToString(byteArray, Base64Flags.Default);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM