|
Android SQLite存取图像的简单方法如下:
//Bitmap to byte[]
public byte[] bmpToByteArray(Bitmap bmp){
//Default size is 32 bytes
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return bos.toByteArray();
}
//Cursor to bitmap
Bitmap cursorToBmp(Cursor c, int columnIndex) {
byte[] data = c.getBlob(columnIndex);
try {
return BitmapFactory.decodeByteArray(data, 0, data.length);
} catch (Exception e) {
return null;
}
}
图像存储调用:
ContentValues values = new ContentValues();
values.put("img", bmpToByteArray(bmp);
图像读取调用:
Cursor c = db.rawQuery("select * from info", null);
c.moveToLast();
Bitmap bmp = cursorToBmp(c, c.getColumnIndex("img"));
|
|
|