最初我是在卡片 View 中添加信息(字符串)和照片(来自可绘制对象)。我让它从列表中工作,并使用适配器等将它添加到回收器 View 中的卡片中。现在我正在尝试迁移到使用 Room Persistence Library 来保存此信息,而不是在代码中添加虚拟信息,我打算让它来自用户输入,因为我正在尝试实现这一点,我发现保存图像去Room DB也不是太容易。字符串现在工作正常,我只需要一种方法来保存从相机拍摄的图像。
我无法使用 Image、Bitmap、URI、Drawable 类型将图像存储在 Room DB 中。
@Entity(tableName = "machines_table")
public class Machines {
@PrimaryKey(autoGenerate = true)
private int id;
private Drawable photoId;
private String name;
private String location;
public Machines(String name, String location, Drawable photoId) {
this.name = name;
this.location = location;
this.photoId = photoId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public String getLocation() {
return location;
}
public Drawable getPhotoId() {
return photoId;
}
}
我想我希望能够更轻松地保存图像,但是当使用我上面列出的任何类型时情况并非如此,我会收到此错误。
“错误:无法确定如何将此字段保存到数据库中。您可以考虑为其添加类型转换器。”
最佳答案
您可能可以将它们保存为 byte[]
,但是通常认为将位图保存到数据库中是不明智的。将它们存储到文件系统并将文件名(例如基于 UUID 的文件名)保存到数据库。然后在需要时检索文件。
如果它自己存储图像,数据库将变得庞大而缓慢。
关于java - 如何将图像保存到 Room Persistence Library,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57117262/