Social Icons

2010年10月15日 星期五

將Bitmap儲存到Sqlite的欄位

在寫程式的過程中,遇到需要將Bitmap儲存在sqlite的binary欄位中。
Google了一下發現下面的方法:

1. 首先先生成一個ByteArrayOutput
2. 然後將Bitmap壓縮成PNG(or其他格式),並輸出到ByteArrayOutput裡
3. 最後在放到binary的value中。

部分的程式片段如下:
ByteArrayOutputStream out = new ByteArrayOutputStream();
thumb.compress(Bitmap.CompressFormat.PNG, 100, out);
values.put(FIELD_THUMB, out.toByteArray());

取出Bitmap並重新建立的方法如下:
byte[] buf;
buf = cur.getBlob(FIELD_THUMB_IDX);
Bitmap bmp = BitmapFactory.decodeByteArray(buf, 0, buf.length);

查了很久,不過知道了之後還覺得蠻簡單的說。

沒有留言:

張貼留言