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

Glide加载资源失败+导航组件传值显示全屏图片问题求助

问题解决:Glide加载全屏图片失败的问题

问题描述

我开发了一个相册项目,通过RecyclerView结合Rest API展示ImageView与两个TextView。需求是点击RecyclerView中由Glide加载的小图,在另一个Fragment中用Glide加载同一张图片的全屏版本。我尝试使用Navigation组件的SafeArgs传递图片信息,但始终无法正常加载,报错信息如下:

GlideException: Failed to load resource // java.io.FileNotFoundException(/di.GlideRequest@e41163b4: open failed: ENOENT (No such file or directory))

核心问题原因

你传递给SafeArgs的不是图片的URL字符串,而是GlideRequest对象的toString()结果——这个字符串是Glide内部对象的标识(比如di.GlideRequest@e41163b4),并不是有效的图片资源地址。Glide会把它当成本地文件路径去查找,自然找不到对应的文件,因此抛出找不到文件的异常。

解决方案

1. 确认Navigation SafeArgs参数类型

在你的navigation.xml文件中,确保跳转action对应的参数类型为string,参数名与接收端一致(比如imageURL):

<action
    android:id="@+id/action_galleryListFragment_to_fullscreenPhotoFragment"
    app:destination="@id/fullscreenPhotoFragment">
    <argument
        android:name="imageURL"
        app:argType="string" />
</action>

2. 修改RecyclerView适配器的点击事件逻辑

直接传递图片的regular尺寸URL字符串,而非GlideRequest对象:

// 替换原适配器中的点击事件代码
holder.itemView.findViewById<ImageView>(R.id.imageforrest).setOnClickListener {
    // 直接获取高清图片URL
    val imageUrl = listwithelements[position].urls.regular
    // 创建导航Action并传递URL字符串
    val action = GalleryListFragmentDirections.actionGalleryListFragmentToPhotoFragment(imageUrl)
    view.findNavController().navigate(action)
}

3. 全屏Fragment代码保持(参数接收逻辑正确)

你的FullscreenPhotoFragment代码逻辑无误,只要SafeArgs参数类型为string,就能正常接收URL并加载图片:

@AndroidEntryPoint
class FullscreenPhotoFragment : Fragment() {
   private lateinit var binding : FragmentFullscreenPhotoBinding
   private val args:FullscreenPhotoFragmentArgs by navArgs()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
      binding = FragmentFullscreenPhotoBinding.inflate(inflater)
      return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Glide.with(this).load(args.imageURL).into(binding.fullscreenimage)
    }
}

补充说明

  • Glide只需要有效的资源地址(URL、本地路径等)就能加载图片,无需传递GlideRequest对象
  • 你已确认Urls模型类的regular字段命名正确,这部分无需修改

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 13:45:27