Glide callback after success in Kotlin

Than Htike Aung picture Than Htike Aung · May 31, 2017 · Viewed 19.8k times · Source
   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.

Answer

Paulina picture Paulina · Sep 19, 2017
 Glide.with(context)
        .load(url)
        .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 {
                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.