【发布时间】:2019-07-31 17:59:30
【问题描述】:
我将图像作为存储在数据库中的字节数组检索,并将其转换为位图并在 Imageview 中显示。我希望能够从 Imageview 中检索该图像并将其存储回数据库。我的数据库检索代码是:
TheService myService = new TheService.DataInterface();
DataSet MyPhoto = myService.GetPhoto(id);
byte[] imageBytes = (byte[])MyPhoto.Tables[0].Rows[0][0];
Bitmap bitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
imageview.SetImageBitmap(bitmap);
在某些时候图像发生了变化,我需要将其存储回数据库中。我如何将图像从图像视图中取出?到目前为止,我所看到的所有内容都处理附加的可绘制对象,在这种情况下没有可绘制对象。
好像没有这样的方法:
Bitmap photo = imageview.GetCurrentImage();
我们将不胜感激。
**** 已更新 ****
获得图像后,我需要将其转换为字节数组以将其保存到数据库中。我尝试了几种不同的方法都没有成功,最新的是:
using Java.Nio;
public static byte[] ImageToByte(Bitmap bitmap)
{
var bytes = new Byte[30000];
try
{
var byteBuffer = ByteBuffer.Allocate(bitmap.ByteCount);
bitmap.CopyPixelsToBuffer(byteBuffer);
bytes = byteBuffer.ToArray<byte>();
return bytes;
}
catch (Exception ex)
{
var message = ex.Message;
return bytes;
}
}
这会生成一个异常“无法从 'java/nio/HeapByteBuffer' 转换为 '[B'”
【问题讨论】:
标签: c# android xamarin.android visual-studio-2017