我正在嘗試使用 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
提交
取消