50

我正在使用 Glide 加载图像,并添加了一个侦听器以了解资源何时准备好或是否存在任何类型的错误:

Glide.with(mContext)
    .load(url)
    .placeholder(R.drawable.glide_placeholder)
    // use dontAnimate and not crossFade to avoid a bug with custom views
    .dontAnimate()
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .listener(new RequestListener<String, GlideDrawable>() {
        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            // do something
            return true;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            // do something
            return true;
         }
    })
    .into(mCustomImageView);

该应用程序永远不会在内部运行,onResourceReady或者onException但是如果我删除了侦听器并让异步下载没有回调,它会正确运行:

Glide.with(mContext)
    .load(url)
    .placeholder(R.drawable.glide_placeholder)
    // use dontAnimate and not crossFade to avoid a bug with custom views
    .dontAnimate()
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .into(mCustomImageView);

我也尝试使用GlideDrawableImageViewTarget而不是侦听器来接收回调,但应用程序在内部运行onLoadStarted但从未在内部运行onLoadClearedonLoadFailed并且onResourceReady.

4

7 回答 7

42

如果 ImageView 不可见或消失,这似乎是一个错误。我在这里打开了一个问题:https ://github.com/bumptech/glide/issues/618

于 2015-09-11T07:12:59.173 回答
26

这是一种方法:

        Glide.with(context).load(...)
                .listener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                        //TODO handle error images while loading photo
                        return true
                    }

                    override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                        //TODO use "resource" as the photo for your ImageView
                        return true
                    }

                }).submit()
于 2017-12-13T10:32:17.173 回答
10

遇到同样的问题。让 onResourceReady 返回 false 对我有用。

于 2016-05-10T13:41:32.843 回答
8

您只需要将onResourceReadyand的返回值onLoadFailed从 true 更改为 false。

它适用于我glide 4.9.1

如果您查看 RequestListener 评论,您应该明白。

于 2019-05-02T06:32:59.510 回答
6

遇到同样的问题,因为我的宽度和高度ImageView都是 0,0(两者layout_widthlayout_height设置为最初wrap_content没有设置图像ImageView)。提供ImageView默认宽度和高度解决了我的问题。

于 2018-09-18T06:50:55.010 回答
0

Kotlin 使用 Glide 和监听器的方式

Glide.with(context)
                .load(image_url)
                .listener(object : com.bumptech.glide.request.RequestListener<Drawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Drawable>?,
                        isFirstResource: Boolean
                    ): Boolean {
                        return false
                    }

                    override fun onResourceReady(
                        resource: Drawable?,
                        model: Any?,
                        target: Target<Drawable>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ): Boolean {
                        img_product_banner.visibility = View.VISIBLE
                        return false
                    }

                }).placeholder(R.drawable.placeholder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(img_product_banner)
于 2020-09-19T02:21:56.123 回答
-1

这对我有用

try {
        Glide.with(context)
            .asBitmap()
            .load(characterDB.url)
            .listener(object : RequestListener<Bitmap> {
                override fun onResourceReady(
                    resource: Bitmap?,
                    model: Any?,
                    target: Target<Bitmap>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    return false
                }

                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Bitmap>?,
                    isFirstResource: Boolean
                ): Boolean {
                    return false
                }
            }
            )
            .submit()
    }
    catch (e : Exception)
    {
        Log.d("Excepion",e.message.toString())
    }
于 2019-11-08T21:50:22.557 回答