我正在尝试使用 Room 将 base64 值保存到我的 sqlite 数据库中,但由于某种原因它没有保存。好吧,我假设它没有保存,因为当我尝试读取具有base64列的表时,它会返回除base64列之外的所有其他列的值。我做错了什么?我的实体:@Entity(tableName = "healthCareWorkerInformation", foreignKeys = @ForeignKey( entity = HealthCareWorker.class, parentColumns = {"id"}, childColumns = {"hcwId"}, onDelete = ForeignKey.CASCADE), indices = @Index( value = {"hcwId"}))public class HealthCareWorkersInformation { @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "hcwInfoId") private long id; private long hcwId; //@ColumnInfo(typeAffinity = ColumnInfo.BLOB) private String base64Image; private String updatedAt = new SimpleDateFormat("dd MM yyyy, HH:mm", Locale.getDefault()).format(Calendar.getInstance().getTime()); public HealthCareWorkersInformation() { } @Ignore public HealthCareWorkersInformation(long hcwId) { this.hcwId = hcwId; } public long getId() { return id; } public void setId(long id) { this.id = id; } public long getHcwId() { return hcwId; } public void setHcwId(long hcwId) { this.hcwId = hcwId; } public String getBase64Image() { return base64Image; } public void setBase64Image(String base64Image) { this.base64Image = base64Image; } public String getUpdatedAt() { return updatedAt; } public void setUpdatedAt(String updatedAt) { this.updatedAt = updatedAt; }}我的道:@Insertvoid insertHealthCareWorkerInformation(HealthCareWorkersInformation healthCareWorkersInformation);@Query("SELECT * FROM HEALTHCAREWORKERINFORMATION")LiveData<List<HealthCareWorkerInformation>> getHCWInfo();我发送的示例数据:除 base64 列外,其他所有内容都将保存。请协助。
1 回答
翻阅古今
TA贡献1780条经验 获得超5个赞
问题是分号与双引号的组合。data:image/png;base64,"
最简单的方法是保存没有该前缀的字符串,这会破坏语法,然后假设它是所有图像(或添加另一个字段,指示每个图像的编码)。base64PNG
此问题不特定于 ,而是特定于 with ,因为该分号终止语句(将生成)。在 中,只能对基元数据类型使用单引号,而复杂数据类型只能使用双引号。绕过此限制的唯一方法是不要尝试保存包含 .RoomJavaSQLiteRoomJava'charString"String;
举个例子来说明我的意思:
private String base64String = null;
private String base64Type = "png";
public String getBase64Image() {
if(this.base64String != null) {
return "data:image/" + this.base64Type + ";base64," + this.base64String;
} else {
return null;
}
}
添加回答
举报
0/150
提交
取消