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.