Convert Drawable to BLOB Datatype sqlite problem

后端 未结 1 1666
无人共我
无人共我 2021-01-17 02:02

Here I am attaching code in which store image in Drawable and pass to Database with BLOB Datatype.

I try this, it store properly but not display image.

相关标签:
1条回答
  • 2021-01-17 02:53

    To get the image from the sqlite database i used the following:

     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     byte[] data=cur.getBlob(cur.getColumnIndex("img"));
     baos.write(data);
    

    Then to display the image i used to do the following:

     InputStream is = database.getImageStream(someId);
     Bitmap img = BitmapFactory.decodeStream(is);
    

    The code below shows how to save the image into a BLOB field

    ByteArrayOutputStream bos = new ByteArrayOutputStream();      
    image.compress(CompressFormat.PNG, 100, bos); 
    byte[] bytes = bos.toByteArray(); 
    

    the bytes can be added to sqlite using

     initialValues.put("<fieldname>", bytes); //ofcourse <fieldname> is of type BLOB
    
    0 讨论(0)
提交回复
热议问题