This question is old, but I stumbled across similar scenario where I needed to check for the original image size. After some digging I found this thread on Github which has the solution.
I will copy the latest (glide v4) solution written by pandasys.
This code is Kotlin but Java folks should have no trouble. The code to do the load would look something like:
Glide.with(activity)
.`as`(Size2::class.java)
.apply(sizeOptions)
.load(uri)
.into(object : SimpleTarget<Size2>() {
override fun onResourceReady(size: Size2, glideAnimation: Transition<in Size2>) {
imageToSizeMap.put(image, size)
holder.albumArtDescription.text = size.toString()
}
override fun onLoadFailed(errorDrawable: Drawable?) {
imageToSizeMap.put(image, Size2(-1, -1))
holder.albumArtDescription.setText(R.string.Unknown)
}
})
The resusable options are:
private val sizeOptions by lazy {
RequestOptions()
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.DATA)}
My size class is approximately:
data class Size2(val width: Int, val height: Int) : Parcelable {
companion object {
@JvmField val CREATOR = createParcel { Size2(it) }
}
private constructor(parcelIn: Parcel) : this(parcelIn.readInt(), parcelIn.readInt())
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeInt(width)
dest.writeInt(height)
}
override fun describeContents() = 0
override fun toString(): String = "$width x $height"
}
Here's the relevant part of my AppGlideModule
class BitmapSizeDecoder : ResourceDecoder<File, BitmapFactory.Options> {
@Throws(IOException::class)
override fun handles(file: File, options: Options): Boolean {
return true
}
override fun decode(file: File, width: Int, height: Int, options: Options): Resource<BitmapFactory.Options>? {
val bmOptions: BitmapFactory.Options = BitmapFactory.Options()
bmOptions.inJustDecodeBounds = true
BitmapFactory.decodeFile(file.absolutePath, bmOptions)
return SimpleResource(bmOptions)
}
}:
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
registry.prepend(File::class.java, BitmapFactory.Options::class.java, BitmapSizeDecoder())
registry.register(BitmapFactory.Options::class.java, Size2::class.java, OptionsSizeResourceTranscoder())
class OptionsSizeResourceTranscoder : ResourceTranscoder<BitmapFactory.Options, Size2> {
override fun transcode(resource: Resource<BitmapFactory.Options>, options: Options): Resource<Size2> {
val bmOptions = resource.get()
val size = Size2(bmOptions.outWidth, bmOptions.outHeight)
return SimpleResource(size)
}
}
So back to the original question, onResourceReady
callback you can check for the width and height and decide what whether you show the image or not