如何将URL中的图像存储在Room数据库中(Java)

时间:2019-12-26 12:08:14

标签: android android-room

我要对Room数据库进行整个API响应以进行脱机访问。 我已经设法存储了所有字符串值,但是我也想将图像(在响应中获取图像url)也存储到数据库中。 由于我没有使用Room数据库的经验,因此我对如何将图像存储到Room db列中感到困惑 下面是API响应

{
"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"
    }]
}

2 个答案:

答案 0 :(得分:0)

您应该使用BLOB数据类型存储图像。只需使用@ColumnInfo(typeAffinity = ColumnInfo.BLOB)注释Room模型类中的图像字段。观看此answer,了解更多信息。

答案 1 :(得分:0)

您可以将图像作为Blob类型存储在房间DB中。 您应该将图像转换为字节数组并保存。 示例:

@ColumnInfo(name = "image", typeAffinity = ColumnInfo.BLOB)
private byte[] image;