开发者

Saving canvas image to 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.


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)


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.


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));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜