You can use Coil Image loading library,
Add the following dependencies to your app level build.gradle file,
def coilVersion = '1.2.2'
implementation "io.coil-kt:coil:$coilVersion"
implementation "io.coil-kt:coil-svg:$coilVersion"
Now create this method,
fun ImageView.loadImageFromUrl(imageUrl: String) {
val imageLoader = ImageLoader.Builder(this.context)
.componentRegistry { add(SvgDecoder(this@loadImageFromUrl.context))
}
.build()
val imageRequest = ImageRequest.Builder(this.context)
.crossfade(true)
.crossfade(300)
.data(imageUrl)
.target(
onStart = {
//set up an image loader or whatever you need
},
onSuccess = { result ->
val bitmap = (result as BitmapDrawable).bitmap
this.setImageBitmap(bitmap)
//dismiss the loader if any
},
onError = {
/**
* TODO: set an error drawable
*/
}
)
.build()
imageLoader.enqueue(imageRequest)
}
Now you can call this extension function from your ImageView,
imgView.loadImage(imageUrl)
This will work for svgs, pngs. I have not tried it with jpgs, but should work with them as well.