24
   private SimpleTarget target = new SimpleTarget<Bitmap>() {  

    @Override
    public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
        // do something with the bitmap
        // for demonstration purposes, let's just set it to an ImageView
        imageView1.setImageBitmap( bitmap );
    }
};

private void loadImageSimpleTarget() {  
    Glide.with(context)
        .load(uri)
        .override(600, 600)
        .fitCenter()
        .into(target);
}

I tried to convert it into Kotlin like as follow.

val finish_target = object : SimpleTarget<Bitmap>() {
                override fun onResourceReady(bitmap: Bitmap?, glideAnimation: GlideAnimation<in Bitmap>?) {
                    preview_image.setImageBitmap(bitmap)
                }
            }

        Glide.with(context)
                .load(uri)
                .override(600, 600)
                .fitCenter()
                .into(finish_target)

But compilation error shows that

public open fun <Y : Target<GlideDrawable!>!> into(target: (???..???)): (???..???) defined in com.bumptech.glide.DrawableRequestBuilder
public open fun into(view: ImageView!): Target<GlideDrawable!>! defined in com.bumptech.glide.DrawableRequestBuilder

Please kindly help me how to solve this problem.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Than Htike Aung
  • 261
  • 1
  • 2
  • 5
  • See also https://stackoverflow.com/questions/26054420/set-visibility-of-progress-bar-gone-on-completion-of-image-loading-using-glide-l – rds Jan 14 '21 at 14:45

5 Answers5

33
 Glide.with(context)
        .load(url)
        .listener(object : RequestListener<Drawable> {
            override fun onLoadFailed(p0: GlideException?, p1: Any?, p2: Target<Drawable>?, p3: Boolean): Boolean {
                Log.e(TAG, "onLoadFailed")
                //do something if error loading
                return false
            }
            override fun onResourceReady(p0: Drawable?, p1: Any?, p2: Target<Drawable>?, p3: DataSource?, p4: Boolean): Boolean {
                Log.d(TAG, "OnResourceReady")
                //do something when picture already loaded
                return false
            }
        })
        .into(imgView)

With Glide you can add Listener to your chain, which monitor state of your image loading. You have to override two methods, in onResourceReady method you have callback that your image is already loaded and you can do something , for example hide loader or let finish animation from another view. In onLoadFailed you get information about some error while loading and also you can react somehow. This way you can avoid those errors.

moyo
  • 1,312
  • 1
  • 13
  • 29
Paulina
  • 930
  • 8
  • 13
  • It's often best to include an explanation of what you did and why you did it when answering a question. This helps newcomers to the ideas and languages you wrote about understand why something like this would be done. – Ethan Field Sep 19 '17 at 14:50
  • 14
    please remember "Target" is a kotlin annotation so you must use "com.bumptech.glide.request.target.Target" – Emre Akcan Jul 09 '19 at 10:23
  • I want to use drawable in DecorView and it's not possible to use using this way. any other way possible ? – KamDroid Jun 12 '22 at 10:14
8

Android Studio 3.5 - Kotlin 1.3.41 - Glide 4.9.0

  • add this dependency to your build.gradle under dependencies:

    implementation 'com.github.bumptech.glide:glide:4.9.0'

Go to the top of your class and add these imports (pay attention expecially to target class which is different from the kotlin target class annotation):

import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import android.graphics.drawable.Drawable
import android.support.graphics.drawable.Animatable2Compat

I've put some extra parameter as override(600, 600), if you don't need remove it..

// Start animation
        Glide
            .with(this)
            .load(R.drawable.tossina_pose1)
            .centerCrop()
            .override(600, 600)
            .placeholder(R.drawable.tossina_idle_0)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .listener(object : RequestListener<Drawable> {
                override fun onLoadFailed(p0: GlideException?, p1: Any?, p2: Target<Drawable>?, p3: Boolean): Boolean {
                    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                }
                override fun onResourceReady(p0: Drawable?, p1: Any?, p2: Target<Drawable>?, p3: DataSource?, p4: Boolean): Boolean {
                    (p0 as GifDrawable).setLoopCount(1)
                    p0.registerAnimationCallback(object : Animatable2Compat.AnimationCallback() {
                        override fun onAnimationEnd(drawable: Drawable) {
                            println("animation ends")
                        }
                    })
                    return false
                }
            })
            .into(img)

Some indications: R.drawable.tossina_pose1 is my GIF, you can put also gif local image like this example. The final line .into(img) have img that is my imageView

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
5

The problem is that in the Java code, you used the type SimpleTarget as the type of target. This is a raw type (missing generic parameters), and is one of the big legacy problems in Java generics. Kotlin doesn't allow raw types, and this is why you got problems while converting.

To fix this, you should do the following in Java:

private SimpleTarget<Bitmap> target = new SimpleTarget<Bitmap>() { ... }

Which will force you to add asBitmap() to your Glide call:

Glide.with(context)
        .load(uri)
        .asBitmap()
        .override(600, 600)
        .fitCenter()
        .into(target);

Now that your code is using generics safely, it can be translated to Kotlin without a problem:

Glide.with(context)
        .load(uri)
        .asBitmap()
        .override(600, 600)
        .fitCenter()
        .into<SimpleTarget<Bitmap>>(target)
zsmb13
  • 85,752
  • 11
  • 221
  • 226
4

For those who are using Glide 3.8:

 Glide.with(this)
      .load(imgUrl)
      .listener(object : RequestListener<String, GlideDrawable> {
                        override fun onException(e: Exception?, model: String?, target: Target<GlideDrawable>?, isFirstResource: Boolean): Boolean {
                            return false
                        }
                        override fun onResourceReady(resource: GlideDrawable?, model: String?, target: Target<GlideDrawable>?, isFromMemoryCache: Boolean, isFirstResource: Boolean): Boolean {
                            return false
                        }
                    })
       .into(image)
Zoe
  • 27,060
  • 21
  • 118
  • 148
Nilesh Deokar
  • 2,975
  • 30
  • 53
0

If you only want to set the Bitmap into the ImageView using Glide then you can try out for a Extension Function in Kotlin with which you will only need to pass the parameters like uri/url or size.

For Example:

class KotlinActivity : AppCompatActivity() {

    var sampleImageView : ImageView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin)
        sampleImageView = findViewById(R.id.imageView) as ImageView?
        sampleImageView?.setImage("https://kbob.github.io/images/sample-3.jpg")
    }

//Extension function for ImageView
    fun ImageView.setImage(url:String, context:Context){
        Glide.with(context).load(url).into(this)
    }
}
Mohammed Rampurawala
  • 3,033
  • 2
  • 22
  • 32