天天看点

将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;

}