问题描述
我想将api中的图像和api存储到房间db中.我从api中接收数据和图像.当我处于联机模式时,将使用api提供的url加载图像,但是当处于脱机状态时,应在脱机模式下存储图像并从数据库中检索图像.
I want to store images from and api into room db.I receive data and images from an api. When I'm in online mode, the images are loaded using the urls provided by the api but when offline, images should be stored and retrieved from the database in offline mode.
im像这样加载图像(它加载图像):----
im loading the images like this (it loads images):----
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是这样的:--------
my JSON is like this:--------
"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:-这不起作用
tried solution 1:--this is not working
.diskCacheStrategy(DiskCacheStrategy.DATA)
任何想法如何使用Room db存储此图像????????需要帮忙 ...在此先感谢
Any idea how do i store this images using Room db ????????need help ...thanks in Advance
推荐答案
尝试将其存储为URI.或创建像这样的类型转换器
Try to store it as URI. or create a type converter like this
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类中添加此类型转换器
and add this type converter in you roomdb class
@TypeConverters(Converter::class)
abstract class RunningDatabase : RoomDatabase() {
///
}
这篇关于如何将图像从服务器(API)存储到会议室数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!