Android 房间类型转换器(列表<;ByteArray>;)不';行不通

Android 房间类型转换器(列表<;ByteArray>;)不';行不通,android,android-room,typeconverter,Android,Android Room,Typeconverter,错误日志为: 错误:无法确定如何将此字段保存到数据库中。你 可以考虑为它添加一个类型转换器。 private final java.util.Listbyte[]>photo=null 我在stackoverflow中做了所有的回答,但仍然不起作用 备忘类 @Entity data class Memo ( @PrimaryKey(autoGenerate = true) val id: Int?, @ColumnInfo(name = "title") val title: S

错误日志为:

错误:无法确定如何将此字段保存到数据库中。你 可以考虑为它添加一个类型转换器。 private final java.util.Listbyte[]>photo=null

我在stackoverflow中做了所有的回答,但仍然不起作用

备忘类

@Entity
data class Memo (
    @PrimaryKey(autoGenerate = true) val id: Int?,
    @ColumnInfo(name = "title") val title: String?,
    @ColumnInfo(name = "content") val content: String?,
    @ColumnInfo(name = "photo", typeAffinity = ColumnInfo.BLOB) val photo: List<ByteArray>?
)
高级船员

class Converters {

        @TypeConverter
        fun fromString(value: String): List<ByteArray> {
            val type = object : TypeToken<List<ByteArray>>() {}.type
            return Gson().fromJson(value, type)
        }

        @TypeConverter
        fun fromList(list: List<ByteArray>): String {
            val type = object : TypeToken<List<ByteArray>>() {}.type
            return Gson().toJson(list, type)
        }
}
类转换器{
@类型转换器
趣味fromString(值:String):列表{
val type=object:TypeToken(){}.type
返回Gson().fromJson(值,类型)
}
@类型转换器
趣味fromList(list:list):字符串{
val type=object:TypeToken(){}.type
返回Gson().toJson(列表,类型)
}
}

给定
typeAffinity=ColumnInfo.BLOB
,您可能需要将
List
转换为
ByteArray
。或者,如果您愿意像现在这样使用Gson,可以删除
typeAffinity=ColumnInfo.BLOB
,房间应该使用
字符串
表示。请注意,将
ByteArray
扩展为JSON文本可能会显著增加大小。@Commonware感谢您的回答@如果您不介意的话,可以告诉我Listto和ByteArray的详细代码吗?我大概有30年没有做过类似的事情了,所以我现在没有任何代码与您共享。而且,老实说,除非这些数组非常小且数量很少,否则我不会将它们存储在数据库中。
class Converters {

        @TypeConverter
        fun fromString(value: String): List<ByteArray> {
            val type = object : TypeToken<List<ByteArray>>() {}.type
            return Gson().fromJson(value, type)
        }

        @TypeConverter
        fun fromList(list: List<ByteArray>): String {
            val type = object : TypeToken<List<ByteArray>>() {}.type
            return Gson().toJson(list, type)
        }
}