Reputation: 35
I'm making a little geography app where I wish to display a country's flag (svg). The flag image resources are online link (in svg format).
Eg: Albania Flag - > https://flagcdn.com/al.svg
This is my code:
flag = "https://flagcdn.com/al.svg"
val thread = Thread {
try {
val flagURL = URL(flag)
println("variable URL : ${flagURL }")
val flagValue = BitmapFactory.decodeStream(flagURL.openConnection().getInputStream())
println("variable in flagValue : ${flagValue}")
binding.flagValue.setImageBitmap(flagValue)
} catch (e: Exception) {
e.printStackTrace()
println("failed")
}
}
thread.start()
When the code runs, an error is detected in the try / catch section:
D/skia: --- Failed to create image decoder with message 'unimplemented'
I/System.out: variable in flagValue : null
I got my code from this previously asked question, but it doesn't work for me. Any ideas? Thanks
Upvotes: 0
Views: 493
Reputation: 55
Coil dependency works, Add below dependencies in gradle,
implementation("io.coil-kt:coil:1.2.0")
implementation("io.coil-kt:coil-svg:1.2.0")
And use below code,
fun ImageView.loadImageFromUrl(imageUrl: String) {
val imageLoader = ImageLoader.Builder(this.context)
.componentRegistry { add(SvgDecoder(this@Splashh))
}
.build()
val imageRequest = ImageRequest.Builder(this.context)
.crossfade(true)
.crossfade(300)
.data(imageUrl)
.target(
onSuccess = { result ->
val bitmap = (result as BitmapDrawable).bitmap
this.setImageBitmap(bitmap)
},
)
.build()
imageLoader.enqueue(imageRequest)
}
Call method,
var flag = "https://flagcdn.com/al.svg"
img.loadImageFromUrl(flag)
Upvotes: 0