2011-03-12 165 views
1

我想将画布上绘制的图像保存为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); 

Howevere,我得到一个

“了java.lang.RuntimeException:缓冲区 不是像素足够大”

错误与此代码。我错过了什么?是否有更好/更简单的方式将画布保存为SQLite数据库?此外,我不太确定如何恢复图像。应该可以使用游标和适配器,对吧?谢谢。

回答

0

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

+0

好。可能是这个问题。但我不知道该怎么做。任何链接/示例代码都会有帮助。它甚至会因设备而异?它如何发现?谢谢。 – redGREENblue 2011-03-12 12:54:43

+0

http://www.coderanch.com/t/449045/Android/Mobile/byte-image上有一个链接,它使用流而不是缓冲区来获取字节数组。这可能是最简单的方法。 – 2011-03-12 13:12:45

+0

谢谢。将通过这一点。 – redGREENblue 2011-03-12 13:17:23

3

使用

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

以确保缓冲区的大小正确。

如果您正在重用的缓冲区,请确保调用

buffer.clear() 

bitmap.copyPixelsToBuffer(buffer) 
0

该代码会从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; 
    } 
} 

最后到该图像加载到ImageView的

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