2

I am whoring whole API response to Room database for offline access. I have managed to store all string values but I want to store image (getting image url in response)into database as well. Since I have no experience in using Room database, I m confused about how to store image int Room db column Below is API response

{
"beast": [{
        "id": "1",
        "name": "The Griffin of Edward III",
        "description": "The English Beast has always been a lion",
        "image": "www.google.com/uploads/beast-images/2.png"
    },
    {
        "id": "2",
        "name": "The Lion of England",
        "description": "The English Beast has always been a lion",
        "image": "www.google.com/uploads/beast-images/2.png"
    }]
}
Hussain
  • 1,243
  • 12
  • 21
  • 1
    Does this answer your question? [Saving Image path from gallery to Room Database and display it in Recycler list](https://stackoverflow.com/questions/50609378/saving-image-path-from-gallery-to-room-database-and-display-it-in-recycler-list) – Manthan Tilva Dec 26 '19 at 12:16
  • @ManthanTilva I m getting image url in response and I want to store it as image in room db – Vinod Patil Dec 26 '19 at 12:21
  • save the image as blob/ byte array or as base64 string in room database – Manoj Perumarath Dec 26 '19 at 14:10

2 Answers2

0

You should use BLOB data type to store image. Just annotate image field in Room model class with @ColumnInfo(typeAffinity = ColumnInfo.BLOB). Watch this answer for more information.

art
  • 1,222
  • 9
  • 19
  • getting com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 2104 path $.beastData[0].image error – Vinod Patil Dec 30 '19 at 05:27
  • @VinodPatil add some code, which causes this exception – art Dec 30 '19 at 10:05
0

You can store the image as a blob type in room DB. you should convert your image to byte array and save it. Example:

@ColumnInfo(name = "image", typeAffinity = ColumnInfo.BLOB)
private byte[] image;
  • getting JsonSyntaxException, Expected BEGIN_ARRAY but was STRING at line 1 column 2104 path $.beastData[0].image – Vinod Patil Dec 30 '19 at 05:30