5

I have followed this tutorial where it is declared a custom Glide Module to load different image sizes from server depending on the ImageView size. I have also taken a look to this Glide wiki which explains the same.

But the implementation on the tutorial and the Glide wiki only works if the String you send to the Custom Module is a http/https url. How can I modify this Custom Module or create a new one in order to handle all other types (String, Uri, int, etc as Glide.load() does) and keep the functionality presented in the tutorial?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Damia Fuentes
  • 5,308
  • 6
  • 33
  • 65

2 Answers2

-1

Instead of registering the new ModelLoader with append(), which handle new types of data, register it using prepend(), which handle subsets of existing data where you do want to fall back to Glide’s default behavior if your ModelLoader fails. So instead of creating the new Glide's input data (in the tutorial named as CustomImageSizeModelFutureStudio), tell Glide, in the case of a String, to check if you want to modify the String and create your url or let Glide do his work without modifying the String. Here is my implementation in Kotlin. In this case, if your input is "https://...." it will request your custom url. If your input is "content://..." your ModelLoader will fail because of the handles() method and Glide will do it's work.

The implementation of AppGlideModule:

@GlideModule
class MyGlideModule : AppGlideModule() {
    override fun registerComponents(context: Context?, glide: Glide?, registry: Registry?) {
        registry?.prepend(String::class.java, InputStream::class.java, CustomImageSizeUrlLoaderFactory())
    }
}

The implementation of ModelLoaderFactory:

class CustomImageSizeUrlLoaderFactory : ModelLoaderFactory<String, InputStream> {
    private val modelCache = ModelCache<String, GlideUrl>(500)

    override fun build(multiFactory: MultiModelLoaderFactory): ModelLoader<String, InputStream> {
        val modelLoader = multiFactory.build(GlideUrl::class.java, InputStream::class.java)
        return CustomImageSizeUrlLoader(modelLoader, modelCache)
    }

    override fun teardown() {

    }
}

The implementation of BaseGlideUrlLoader:

class CustomImageSizeUrlLoader(concreteLoader: ModelLoader<GlideUrl, InputStream>, modelCache: ModelCache<String, GlideUrl>?) : BaseGlideUrlLoader<String>(concreteLoader, modelCache) {
    override fun getUrl(baseImageUrl: String, width: Int, height: Int, options: Options?): String {
        return  baseImageUrl + "?w=" + width + "&h=" + height;
    }

    override fun handles(model: String): Boolean {
        return baseImageUrl.startsWith("http://")
                || baseImageUrl.startsWith("https://")
    }
}

And call your Glide as you will normally do, not as the tutorial does.

Damia Fuentes
  • 5,308
  • 6
  • 33
  • 65
-2

To load image in different sizes, you can use glide's default method override Check below code snippet for load image from different sizes.

GlideApp  
    .with(context)
    .load(url)
    .override(customwidth, customheight) // resizes the image to these dimensions (in pixel). resize does not respect aspect ratio
    .into(imageViewResize);

If you want to maintain aspect ratio as well, you can use fitCenter() or centerCrop().

Samir Bhatt
  • 3,041
  • 2
  • 25
  • 39