将画布图像保存到 SQLite?

发布于 2024-10-21 10:31:07 字数 682 浏览 1 评论 0原文

我正在尝试将画布上绘制的图像作为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷默言语 2024-10-28 10:31:07

用于

ByteBuffer buffer = ByteBuffer.allocate(
    bitmap.getRowBytes() * bitmap.getHeight()
);

确保缓冲区大小正确。

如果您要重用缓冲区,请确保

buffer.clear()

在之前调用

bitmap.copyPixelsToBuffer(buffer)

Use

ByteBuffer buffer = ByteBuffer.allocate(
    bitmap.getRowBytes() * bitmap.getHeight()
);

to make sure the buffer is the correct size.

If you are reusing the buffer, make sure to call

buffer.clear()

before

bitmap.copyPixelsToBuffer(buffer)
轻拂→两袖风尘 2024-10-28 10:31:07

您没有为位图分配足够大的缓冲区。请记住,位图每个像素可以有多个字节,具体取决于压缩和颜色深度。

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.

断念 2024-10-28 10:31:07

此代码将从 url 获取图像并将其转换为字节数组,其工作

byte[] logoImage = getLogoImage(IMAGEURL);

private byte[] getLogoImage(String url){
  try {
         URL imageUrl = new URL(url);
         URLConnection ucon = imageUrl.openConnection();

         InputStream is = ucon.getInputStream();
         BufferedInputStream bis = new BufferedInputStream(is);

         ByteArrayBuffer baf = new ByteArrayBuffer(500);
         int current = 0;
         while ((current = bis.read()) != -1) {
                 baf.append((byte) current);
          }

         return baf.toByteArray();
    } catch (Exception e) {
         Log.d("ImageManager", "Error: " + e.toString());
    }
 return null;
}

将图像保存到我使用此代码的数据库。

public void insertUser(){
SQLiteDatabase db               =   dbHelper.getWritableDatabase();

String delSql                       =   "DELETE FROM ACCOUNTS";
SQLiteStatement delStmt         =   db.compileStatement(delSql);
delStmt.execute();

String sql                      =
"INSERT INTO ACCOUNTS   
(account_id,account_name,account_image)  VALUES(?,?,?)";
SQLiteStatement insertStmt      =   db.compileStatement(sql);
insertStmt.clearBindings();
insertStmt.bindString(1, Integer.toString(this.accId));
insertStmt.bindString(2,this.accName);
insertStmt.bindBlob(3, this.accImage);
insertStmt.executeInsert();
db.close();
}

为了检索图像,这是我使用的代码。

public Account getCurrentAccount() {
SQLiteDatabase db       =   dbHelper.getWritableDatabase();
String sql              =   "SELECT * FROM ACCOUNTS";
Cursor cursor           =   db.rawQuery(sql, new String[] {});

if(cursor.moveToFirst()){
    this.accId             = cursor.getInt(0);
    this.accName           = cursor.getString(1);
    this.accImage          = cursor.getBlob(2);
    }
if (cursor != null && !cursor.isClosed()) {
    cursor.close();
    }
db.close();
if(cursor.getCount() == 0){
    return null;
    } else {
    return this;
    }
}

最后将此图像加载到图像视图中

logoImage.setImageBitmap(
BitmapFactory.decodeByteArray(   currentAccount.accImage, 
    0,currentAccount.accImage.length));

This code will take a image from url and convert is to a byte array its work

byte[] logoImage = getLogoImage(IMAGEURL);

private byte[] getLogoImage(String url){
  try {
         URL imageUrl = new URL(url);
         URLConnection ucon = imageUrl.openConnection();

         InputStream is = ucon.getInputStream();
         BufferedInputStream bis = new BufferedInputStream(is);

         ByteArrayBuffer baf = new ByteArrayBuffer(500);
         int current = 0;
         while ((current = bis.read()) != -1) {
                 baf.append((byte) current);
          }

         return baf.toByteArray();
    } catch (Exception e) {
         Log.d("ImageManager", "Error: " + e.toString());
    }
 return null;
}

save the image to db i used this code.

public void insertUser(){
SQLiteDatabase db               =   dbHelper.getWritableDatabase();

String delSql                       =   "DELETE FROM ACCOUNTS";
SQLiteStatement delStmt         =   db.compileStatement(delSql);
delStmt.execute();

String sql                      =
"INSERT INTO ACCOUNTS   
(account_id,account_name,account_image)  VALUES(?,?,?)";
SQLiteStatement insertStmt      =   db.compileStatement(sql);
insertStmt.clearBindings();
insertStmt.bindString(1, Integer.toString(this.accId));
insertStmt.bindString(2,this.accName);
insertStmt.bindBlob(3, this.accImage);
insertStmt.executeInsert();
db.close();
}

To retrieve the image back this is code i used.

public Account getCurrentAccount() {
SQLiteDatabase db       =   dbHelper.getWritableDatabase();
String sql              =   "SELECT * FROM ACCOUNTS";
Cursor cursor           =   db.rawQuery(sql, new String[] {});

if(cursor.moveToFirst()){
    this.accId             = cursor.getInt(0);
    this.accName           = cursor.getString(1);
    this.accImage          = cursor.getBlob(2);
    }
if (cursor != null && !cursor.isClosed()) {
    cursor.close();
    }
db.close();
if(cursor.getCount() == 0){
    return null;
    } else {
    return this;
    }
}

Finally to load this image to a imageview

logoImage.setImageBitmap(
BitmapFactory.decodeByteArray(   currentAccount.accImage, 
    0,currentAccount.accImage.length));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文