How to store image in SQLite database from gallery

前端 未结 3 1492
半阙折子戏
半阙折子戏 2021-01-01 07:40

i have tried this code to upload an image from gallery to sqllite database in my application but when my application tries to open gallery it gives FORCE TO CLOSE Error and

3条回答
  •  灰色年华
    2021-01-01 08:11

    db.execSQL("CREATE TABLE images (" 
        + "_id INTEGER PRIMARY KEY AUTOINCREMENT," 
        + "data BLOB," 
        + "hash BLOB UNIQUE" 
        + ");"); 
    

    To convert the image to a BLOB, use the following code: (Note that we're compressing the image here)

     private byte[] getBitmapAsByteArray(Bitmap bitmap)
     { 
       ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
       // Middle parameter is quality, but since PNG is lossless, it doesn't matter 
       bitmap.compress(CompressFormat.PNG, 0, outputStream); 
       return outputStream.toByteArray(); 
    } 
    

提交回复
热议问题