android - 如何将图像从服务器(api)存储到房间数据库
问题描述
我想将来自 api 的图像存储到房间 db 中。我从 api 接收数据和图像。当我处于在线模式时,图像是使用 api 提供的 url 加载的,但是当我处于离线状态时,应该在离线模式下从数据库中存储和检索图像。
我像这样加载图像(它加载图像):----
if (!data.data.images.isNullOrEmpty()) {
val base = BuildConfig.SERVER_URL.replace("api/", "")
data.data.images.forEachIndexed { pos, it ->
Glide.with(this)
.asBitmap()
.load(base + it?.imagePath)
.into(object : CustomTarget<Bitmap>() {
override fun onResourceReady(
resource: Bitmap,
transition: Transition<in Bitmap>?
) {
imageClick = pos + 1
val file = File(
getExternalFilesDir(Environment.DIRECTORY_PICTURES),
"${System.currentTimeMillis()}${Constants.PROFILE_PIC_FILE_EXTENSION}"
)
imagePath = file.absolutePath
Utils.saveBitmapToFile(resource, imagePath)
Utils.compressImage(imagePath)
onCropImageResult(resource)
}
override fun onLoadCleared(placeholder: Drawable?) {
}
})
}
}
我的 JSON 是这样的:--------
"data" : {
.
.
.
"images" : [ {
"assetImageId" : 4113,
"transactionTypeId" : 2,
"transactionId" : 88341,
"imageURL" : "",
"imagePath" : "UserData/AssetImages/INS-0000017673_1616058387618.jpg",
"createdBy" : 0,
"createdOn" : "0001-01-01T00:00:00",
"updatedBy" : null,
"updatedOn" : null,
"isActive" : 0,
"isNew" : false,
"isDeleted" : false,
"isChanged" : false,
"timestamp" : null
}, {
"assetImageId" : 4114,
"transactionTypeId" : 2,
"transactionId" : 88341,
"imageURL" : "",
"imagePath" : "UserData/AssetImages/INS-0000017673_1616058402212.jpg",
"createdBy" : 0,
"createdOn" : "0001-01-01T00:00:00",
"updatedBy" : null,
"updatedOn" : null,
"isActive" : 0,
"isNew" : false,
"isDeleted" : false,
"isChanged" : false,
"timestamp" : null
} ],
}
我的实体表:---
@Entity(tableName = DatabaseConstants.MYTABLE, indices = [Index(value = ["requestId"], unique = true)])
data class AllocationDetailsByIdEntity(
var requestId: String? = null,
var subLocation: String? = null,
var amsTagId: String? = null,
var status: String? = null,
var installedBy: String? = null,
var installedDate: String? = null,
val assetTree: String? = null,
var images: List<ImagesssItem?>? =null,
@PrimaryKey(autoGenerate = true) var id: Int? = null
): Serializable
@Entity
data class ImagesssItem (
var assetImageId :String?= null,
var transactionTypeId: Int? = null,
var transactionId: Int? = null,
var imageURL: String? = null,
var imagePath: String? = null
@PrimaryKey(autoGenerate = true) var id : Int? = null
):Serializable
尝试过的解决方案1:--这不起作用
.diskCacheStrategy(DiskCacheStrategy.DATA)
知道如何使用 Room db 存储这些图像 ???????? 需要帮助...在此先感谢
解决方案
尝试将其存储为 URI。或者像这样创建一个类型转换器
class Converter {
@TypeConverter
fun fromBitmap(bmp: Bitmap): ByteArray {
val outputStream = ByteArrayOutputStream()
bmp.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
return outputStream.toByteArray()
}
@TypeConverter
fun toBitmap(byteArray: ByteArray): Bitmap {
return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
}
}
并在你的 roomdb 类中添加这个类型转换器
@TypeConverters(Converter::class)
abstract class RunningDatabase : RoomDatabase() {
///
}
推荐阅读
- node.js - 如何迭代这个来自表单的发布结果的对象并只获取有价值的元素?
- acumatica - 选择行时修改过滤的数据网格
- ios - 多行文本标签不在 Swift 中换行
- serverless-framework - Serverless, Inc 是否会看到我的 AWS 凭证?
- arrays - 如何对 Array 的值执行求和运算,直到它在 Swift 中达到某个数字?
- sql - 使用 between 子句时 Sql 查询没有返回所有值
- python - 谁能解释我在 tensorflow keras 中的问题?
- java - 无法跨不同包访问 Spring 项目中的 @Value 并且 Environment 也不包含这些值
- c - 在 AVR MCU 上使用中断通过 USART 进行传输
- apache-spark - Pyspark 2.4.3,从 Kafka 读取 Avro 格式消息 - Pyspark 结构化流