Displaying Internet Image using open source library called Glide

 Glide 

Displaying a photo from a web URL might sound straightforward but there is a quite a bit of engineering to make it work well. The image has to be downloaded, buffered, and decoded from the compressed format it's in, to the image that can be used by Android. 

It should be cached either to an in-memory cache, a storage-based cache, or both. All of this has to happen in low-priority background threads so the UI remains responsive. 

Also we might want to fetch and decode more than one image at once for best network and CPU performance. Learning how to effectively load images from the network could be a class in itself. 

Luckily, Glide will help use to download, buffer, decode and cache our images. So we have to do less work. 

Glide basically needs 2 things: 

  • URL of the image you want to load or show 
  • ImageView 

// Glide
implementation "com.github.bumptech.glide:glide:$version_glide"

Create a binding adapter that will take the URL from an XML attribute associated with an ImageView and use Glide to load the image. 

val imgUri = imgUrl.toUri().buildUpon().scheme("https").build()
Glide.with(imgView.context)
.load(imgUri)
.apply(
RequestOptions()
.placeholder(R.drawable.loading_animation)
.error(R.drawable.ic_broken_image))
.into(imgView)



Comments

Popular Posts