You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Glide Binding Adapter无法加载Unsplash API返回的URL图片问题排查

Unsplash API返回图片无法加载问题解决方案

核心错误原因

你的BindingAdapter逻辑判断存在错误:Unsplash接口返回的urls是包含多规格图片链接的结构体对象,并非固定字符串"images",因此carouselItem?.urls == "images"的判断条件永远无法成立,代码始终走else分支展示错误占位符,Glide加载逻辑完全不会被触发。

修复方案

  • 调整判断逻辑:先校验carouselItem不为空,再从urls结构体中提取对应规格的图片链接,校验链接有效后再调用Glide加载
  • 确认CarouselItem类中urls字段的类型定义和Unsplash返回结构匹配,常规情况下Unsplash的urls会包含regular(常规尺寸)、small(小尺寸)等可用链接字段

修正后代码示例

@BindingAdapter("pictureOfDay")
fun CustomImage.displayPicture(carouselItem: CarouselItem?) {
    // 按你的业务需求替换regular为其他可用尺寸字段即可
    val targetImageUrl = carouselItem?.urls?.regular
    if (!targetImageUrl.isNullOrBlank()) {
        Glide.with(context)
            .load(targetImageUrl)
            .error(R.drawable.glowing_x)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(this)
    } else {
        setImageResource(R.drawable.glowing_x)
    }
}

额外排查项

如果调整代码后仍无法加载,可检查以下配置:

  • 确认AndroidManifest.xml中已添加INTERNET网络权限
  • 如果使用http协议的图片链接,Android 9及以上系统默认禁止明文流量,可在Manifest的application节点添加android:usesCleartextTraffic="true"配置适配

内容的提问来源于stack exchange,提问作者OEThe11

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 10:21:03