How to save bitmap in database?

前端 未结 4 1374
抹茶落季
抹茶落季 2021-02-02 02:46

I want to know how to save a bitmap in database. I created table in db as:

@Override
public void onCreate(SQLiteDatabase db)
{
    db.execSQL(\"CREATE TABLE \" +         


        
4条回答
  •  长发绾君心
    2021-02-02 03:14

    If you have Bitmap image then you can do following.

    Bitmap photo = 
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
    byte[] bArray = bos.toByteArray();
    

    Then you can save data in table using following way.

    db = YourDBHelper.getInstance(ctx).getWritableDatabase();    
    ContentValues values = new ContentValues();         
    values.put("image", bArray);            
    db.insert(TABLE_NAME , null, values);
    

提交回复
热议问题