c# - 如何将图片保存到sqlite数据库

标签 c# xamarin xamarin.android xamarin.forms

在我的类(class)中,我有一个方法可以在照片库中搜索图像,还可以接收从手机摄像头拍摄的图像,我现在需要将此图像保存在 sqlite 数据库中。我正在使用像 BLOB 这样的数据库字段,但不像在 bity [] 中序列化图像或在 decode64 中转换以写入数据库。

我想知道是否有人有任何例子可以传递 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 {

            }

        }

最佳答案

不是将图像(byte[])存储和检索为 blob 从 sqlite db 将图像转换为 base64string 并将图像作为字符串存储在 sqlite 中,同时检索将接收到的 base64string 从 sqlite 转换为图像(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);
}

关于c# - 如何将图片保存到sqlite数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43614282/

相关文章:

xamarin - 从 Visual Studio 2017 15.5.1 开始,如何创建 Xamarin.Forms 项目以使用 PCL 来定位最低 Android KitKat (API 19)?

c# - 如何为面板中的项目添加自定义行为?

ios - Xamarin - 取消选择 ListView 项目在 iOS 中无法正常工作

c# - 在 Xamarin 中获取 OutOfMemoryException

c# - 在 Xamarin.Android/Xamarin.iOS 中使用 Akavache

c# - 为什么 Visual Studio 在选择设备时尝试部署到模拟器

c# - 在 ASP.NET Core 的中间件中注入(inject)服务

c# - 净核心 ILogger 值不能为空

c# - 配置 .NET Core 通用主机时如何应用 HostOptions.ShutdownTimeout?

c# - Xamarin Forms 主详细信息页面主页隐藏导航栏