[英]how to store images from server(api) to room database
我想將來自 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 class
@TypeConverters(Converter::class)
abstract class RunningDatabase : RoomDatabase() {
///
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.