将画布图像保存到 SQLite?
我正在尝试将画布上绘制的图像作为 Blob 保存到 SQLite 数据库。这是代码的一部分。
//Bitmap is already initialized/drawn
ByteBuffer buffer = ByteBuffer.allocate (bmp.getHeight() * bmp.getWidth());
bmp.copyPixelsToBuffer(buffer);
byte[] bdata = buffer.array();
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DBHelper.graphIMG, bdata);
db.insert(DBHelper.graphTable, null, cv);
然而,我得到了
“java.lang.RuntimeException:缓冲区 像素不够大”
错误。我错过了什么?是否有更好/更简单的方法将画布作为图像保存到 SQLite 数据库中?而且我不太确定如何检索图像。应该是可以使用光标和适配器,对吧?谢谢。
I am trying to save a image drawn on the canvas to SQLite db as Blob. Here's part of the code.
//Bitmap is already initialized/drawn
ByteBuffer buffer = ByteBuffer.allocate (bmp.getHeight() * bmp.getWidth());
bmp.copyPixelsToBuffer(buffer);
byte[] bdata = buffer.array();
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DBHelper.graphIMG, bdata);
db.insert(DBHelper.graphTable, null, cv);
Howevere, I am getting a
"java.lang.RuntimeException: Buffer
not large enough for pixels"
error with this code. What am I missing? Is there a better/easier way to save a canvas as an image into SQLite db? Also I am not too sure how to retrieve the images back. It should be possible using a cursor and adapter, right? Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
用于
确保缓冲区大小正确。
如果您要重用缓冲区,请确保
在之前调用
Use
to make sure the buffer is the correct size.
If you are reusing the buffer, make sure to call
before
您没有为位图分配足够大的缓冲区。请记住,位图每个像素可以有多个字节,具体取决于压缩和颜色深度。
You're not allocating a large enough buffer for the bitmap. Remember that a bitmap can have more than one byte per pixel depending on the compression and colour depth.
此代码将从 url 获取图像并将其转换为字节数组,其工作
将图像保存到我使用此代码的数据库。
为了检索图像,这是我使用的代码。
最后将此图像加载到图像视图中
This code will take a image from url and convert is to a byte array its work
save the image to db i used this code.
To retrieve the image back this is code i used.
Finally to load this image to a imageview