gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:54 25 4
gpt4 key购买 nike

在我的类(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com