0

我在将对象插入 SQLite 数据库时遇到问题。我的课,Case看起来像这样:

public class Case {

String caseNumber; 
int status; 
String date;
Bitmap rightIndexFinger;
Bitmap leftIndexFinger; 


public Case(String caseNumber, int status, String date, Bitmap rightIndexFinger, Bitmap leftIndexFinger) {

    this.caseNumber = caseNumber;
    this.status = status;
    this.date = date;
    this.rightIndexFinger = rightIndexFinger;
    this.leftIndexFinger = leftIndexFinger; 

}

public Case(String caseNumber, int status, String date) {

    this.caseNumber = caseNumber;
    this.status = status;
    this.date = date;
}


public Bitmap getRightIndexFinger() {
    return rightIndexFinger;
}


public void setRightIndexFinger(Bitmap rightIndexFinger) {
    this.rightIndexFinger = rightIndexFinger;
}


public Bitmap getLeftIndexFinger() {
    return leftIndexFinger;
}


public void setLeftIndexFinger(Bitmap leftIndexFinger) {
    this.leftIndexFinger = leftIndexFinger;
}


public String getCaseNumber() {
    return caseNumber;
}


public void setCaseNumber(String caseNumber) {
    this.caseNumber = caseNumber;
}


public int getStatus() {
    return status;
}


public void setStatus(int status) {
    this.status = status;
}


public String getDate() {
    return date.toString();
}


public void setDate(String date) {
    this.date = date;
}
}

我需要将此类的对象插入 SQLite 数据库。我尝试的是:

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CASES + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + DATE + " TEXT,"
                + RIGHTFINGER + " TEXT," + LEFTFINGER +" TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);

case如果类中的所有变量都是 ,这是有道理的String,但是如何为位图做到这一点?

4

2 回答 2

1

sqlite 中的位图映射到 Blob,要插入图像,您将其作为字节 [] 插入。
检查https://stackoverflow.com/a/4508531/1434631
https://stackoverflow.com/a/7331698/ 1434631

于 2012-07-30T08:56:51.900 回答
1

将位图转换为字节数组,然后将此字节数组以 Blob 数据类型保存到数据库中。通过以下代码:

要将位图转换为字节数组,请使用以下代码:

Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.thumbnail);

ByteArrayOutputStream out = new ByteArrayOutputStream();

image.compress(Bitmap.CompressFormat.PNG, 100, out);

byte[] buffer=out.toByteArray();

要将字节数组保存为 blob 类型,请使用以下代码:

ContentValues cv=new ContentValues();
   cv.put(CHUNK, buffer);       //CHUNK blob type field of your table
   long rawId=database.insert(TABLE, null, cv); //TABLE table name

通过此链接阅读有关 Blob 的更多信息

于 2012-07-30T08:58:58.270 回答