AndroidKotlin-Glide在图片加载失败时尝试另一张图片地址
创始人
2024-10-08 18:02:18
0

在 Glide 加载图片时,可以使用 error() 方法来设置加载失败时要显示的图片,同时在 onFailed() 回调中尝试重新加载其它地址。

示例代码如下:

val url = "http://example.com/image.jpg"
val imageView = findViewById(R.id.imageView)

Glide.with(this)
    .load(url)
    .error(Glide.with(this).load(R.drawable.default_image))
    .listener(object: RequestListener {
        override fun onLoadFailed(e: GlideException?, model: Any?,
            target: Target?, isFirstResource: Boolean): Boolean {
            // 加载失败时尝试重新加载其它地址
            val anotherUrl = "http://example.com/image2.jpg"
            Glide.with(this@MainActivity)
                .load(anotherUrl)
                .error(Glide.with(this@MainActivity).load(R.drawable.default_image))
                .into(imageView)
            return true
        }
        override fun onResourceReady(resource: Drawable?, model: Any?,
            target: Target?, dataSource: DataSource?,
            isFirstResource: Boolean): Boolean {
            return false
        }
    })
    .into(imageView)

在这个示例代码中,我们首先将图片地址传递给 Glide,并设置加载失败时要显示的默认图片。然后通过实现 RequestListener 接口,重写 onLoadFailed() 回调来监听加载失败的事件。在 onFailed() 回调中,我们获取图片的另一个地址并使用 Glide 加载显示。同样,在这里也设置了另一张默认图片以防止重新加载失败。

相关内容

热门资讯

透视有挂!hhpoker可以控... 透视有挂!hhpoker可以控制牌吗,德普之星app安卓版破解版,手筋教程(真是是有挂)-哔哩哔哩1...
透视最新版!约局吧德州有挂吗(... 透视最新版!约局吧德州有挂吗(透视)开挂辅助app(一贯有挂)-哔哩哔哩1、下载好约局吧德州有挂吗辅...
透视数据!pokerrrr2辅... 透视数据!pokerrrr2辅助,wepoker私人局怎么玩,学习教程(一贯是真的挂)-哔哩哔哩1、...
透视透视挂!wepoker怎么... 透视透视挂!wepoker怎么提高运气,欢乐达人暗堡链接辅助,一直真的有挂(专业力荐教程)-哔哩哔哩...
透视检测!aa poker辅助... 透视检测!aa poker辅助包(透视)开挂辅助技巧(原来真的是有挂)-哔哩哔哩1、aa poker...
透视工具!pokemmo脚本辅... 透视工具!pokemmo脚本辅助下载,德扑之心免费透视,学习教程(一直存在有挂)-哔哩哔哩1)pok...
透视ai代打!wepoker轻... 透视ai代打!wepoker轻量版有透视吗,牵手游戏辅助,一贯是有挂(了解必备教程)-哔哩哔哩暗藏猫...
透视规律!wepoker有用吗... 透视规律!wepoker有用吗(透视)开挂辅助器(切实真的是有挂)-哔哩哔哩透视规律!wepoker...
透视挂!pokerworld破... 透视挂!pokerworld破解版下载,哈糖大菠萝破解器,秘籍教程(确实真的有挂)-哔哩哔哩1、不需...
受玩家影响!aapoker插件... 受玩家影响!aapoker插件,白银胡乐辅助脚本最新版安装方法,本来有挂(关于必备教程)-哔哩哔哩亲...