天天看點

将Blob圖檔放進資料庫中

public long addUser(User user) {

db = dbhelper.getWritableDatabase();

ContentValues values = new ContentValues();

// 存儲一張圖檔進BLOB容器

ByteArrayOutputStream os = new ByteArrayOutputStream();

// user.getUser_head()這裡為一張圖檔,你可以自定義一張圖檔R.drawable.你的圖檔

BitmapDrawable newhead = (BitmapDrawable) user.getUser_head();

//CompressFormat.PNG圖檔的壓縮格式為.png,品質為100,os為前面聲明的流

newhead.getBitmap().compress(CompressFormat.PNG, 100, os);//

values.put(DBInfo.table.USER_HEAD, os.toByteArray());

long rowid = db.insert("表的名稱", DBInfo.table.USER_NAME,

values);

db.close();

return rowid;

}