Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load remote svg files with Picasso library

I'm building an android app that requires downloading svg images from a server. I have tried using Picasso the usual way but it displays nothing.

Picasso.get().load(url).into(imageView)

Is there anyway to display vector images with Picasso?

like image 520
Ikemefuna Nwodo Avatar asked Mar 04 '23 20:03

Ikemefuna Nwodo


1 Answers

You can use a library called GlideToVectorYou which uses Glide internally.

fun ImageView.loadSvg(url: String?) {
    GlideToVectorYou
        .init()
        .with(this.context)
        .setPlaceHolder(R.drawable.loading, R.drawable.actual)
        .load(Uri.parse(url), this)
}

or You can also use Coil library to load svg. Just add these lines in build.gradle

// ... Coil (https://github.com/coil-kt/coil)
implementation("io.coil-kt:coil:0.12.0")
implementation("io.coil-kt:coil-svg:0.12.0")

Then Add an extension function

fun AppCompatImageView.loadSvg(url: String) {
    val imageLoader = ImageLoader.Builder(this.context)
        .componentRegistry { add(SvgDecoder([email protected])) }
        .build()

    val request = ImageRequest.Builder(this.context)
        .crossfade(true)
        .crossfade(500)
        .data(url)
        .target(this)
        .build()

    imageLoader.enqueue(request)
}

Then call this method in your activity or fragment

your_image_view.loadSvg("your_file_name.svg")
like image 113
Aminul Haque Aome Avatar answered Mar 16 '23 12:03

Aminul Haque Aome