如何将图像保存到sqlite数据库 [英] How to save images to sqlite database

查看:66
本文介绍了如何将图像保存到sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的课程中,我有一个方法可以在图库中搜索图像并接收从手机相机拍摄的图像,我现在需要将此图像保存在 sqlite 数据库中.我正在使用像 BLOB 这样的数据库字段,但不喜欢在 bity [] 中序列化图像或在 decode64 中转换以写入数据库.

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. 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.

我想知道是否有人有任何示例可以传递 xaramin android 我正在发布接收图像的方法

I would like to know if anyone has any examples to pass on xaramin android I am posting the method that receives the image

 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 {

            }

        }

推荐答案

代替存储 &从 sqlite db 中检索图像(字节 [])作为 blob 将图像转换为 base64string 并将图像作为字符串存储在 sqlite 中,同时检索将接收到的 base64string 从 sqlite 转换为图像(字节 [])

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[])

Base64 到位图:

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

位图到 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);
}

这篇关于如何将图像保存到sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆