如何在SQLite数据库中存储和检索字节数组(图像数据)?

时间:2010-11-16 04:06:45

标签: android sqlite

如何在Android中的SQLite数据库中存储和检索字节数组(图像数据)?

1 个答案:

答案 0 :(得分:12)

字节数组可以存储为BLOB数据类型。除了特别注意分块之外,这个过程并没有什么特别之处,例如:

InputStream is = context.getResources().open(R.drawable.MyImageFile);
try {
    byte[] buffer = new byte[CHUNK_SIZE];
    int size = CHUNK_SIZE;
    while(size == CHUNK_SIZE) {
        size = is.read(buffer);  //read chunks from file
        if (size == -1) break;

        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
    }
} catch (Exception e) {
    Log.e(TAG, "Error saving raw image to: "+rawId, e);
}