Using Glide,
Follow Below Step
Add below dependancy in your app level gradle
def glide = "<Latest Version>"
implementation "com.github.bumptech.glide:glide:$glide"
annotationProcessor "com.github.bumptech.glide:compiler:$glide"
Create method like below in your ImageUtils class
@Composable
fun loadImage(url: String, @DrawableRes drawable: Int): MutableState<Bitmap?> {
val bitmapState: MutableState<Bitmap?> = remember {
mutableStateOf(null)
}
Glide.with(LocalContext.current)
.asBitmap()
.load(url)
.into(object : CustomTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
bitmapState.value = resource
}
override fun onLoadCleared(placeholder: Drawable?) {
}
})
return bitmapState
}
Used like below wherever required in your app
model.imageUrl?.let { url ->
val image = loadImage(url = url, drawable = DEFAUlT_IMAGE).value
image?.let {
Image(
modifier = Modifier
.fillMaxWidth()
.height(220.dp)
.padding(
top = 10.dp,
bottom = 10.dp,
start = 5.dp,
end = 5.dp
),
bitmap = image.asImageBitmap(),
contentScale = ContentScale.Crop,
contentDescription = ""
)
}
}
Here is an alternative approach that can also help you achieve this
- You may refer to the official Glide documentation for more information. Although this approach is simpler than the one mentioned earlier, it is still in an experimental state, and therefore, I have not recommended it.
- You can also used Accompanist for same
Other referance