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

无法从Firebase Realtime Database获取图片问题求助

Firebase Realtime Database图片加载异常排查与解决

问题描述

从Firebase Realtime Database拉取数据时,文本类数据能正常显示,但图片无法在Android应用内加载。通过Log可确认图片URL已正确获取,但ImageView始终无法渲染图片,且已在AndroidManifest中声明互联网权限。

相关Adapter代码

package com.example.bloomroom;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

    Context context;
    ArrayList<Flowers> list;

    public MyAdapter(Context context, ArrayList<Flowers> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.recylce_item,parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
        Flowers flowers = list.get(position);
        holder.name.setText(flowers.getName());
        holder.category.setText(flowers.getCategory());
        holder.price.setText(flowers.getPrice());
        Log.d("TestUrl", "onBindViewHolder " + flowers.getFurl());
        Glide.get(context).clearMemory();
        new Thread(() -> Glide.get(context).clearDiskCache()).start();
        Glide.with(holder.imgView.getContext())
                .load(flowers.getFurl())
                .into(holder.imgView);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{

        TextView name, category, price;
        ImageView imgView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            name=itemView.findViewById(R.id.txtName);
            category=itemView.findViewById(R.id.txtCat);
            price=itemView.findViewById(R.id.txtPrice);
            imgView =itemView.findViewById(R.id.imgView);
        }
    }
}

排查与解决步骤

1. 移除不必要的缓存清理代码

当前代码在每次绑定ViewHolder时强制清理Glide的内存和磁盘缓存,会破坏Glide的缓存机制,导致图片重复请求且可能因线程冲突引发加载失败。直接删除以下两行代码:

Glide.get(context).clearMemory();
new Thread(() -> Glide.get(context).clearDiskCache()).start();

2. 优化Glide加载逻辑

调整Glide调用方式,使用Adapter持有的Context,并添加占位图、错误监听定位问题:

Glide.with(context)
        .load(flowers.getFurl())
        .placeholder(R.drawable.placeholder_image) // 添加加载中占位图
        .error(R.drawable.error_image) // 添加加载失败占位图
        .diskCacheStrategy(DiskCacheStrategy.ALL) // 启用合理缓存策略
        .into(holder.imgView);

3. 验证图片URL有效性

复制Log中打印的图片URL到浏览器访问:

  • 若浏览器无法打开图片,说明问题出在Firebase Storage配置:检查Storage安全规则是否允许公开访问(可临时设置allow read: if true;测试),或URL是否过期。
  • 若浏览器能正常显示图片,说明问题出在应用内加载逻辑。

4. 检查Glide依赖配置

确保项目Module级build.gradle中引入正确的Glide依赖:

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.16.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0' // Kotlin项目替换为kapt
}

5. 确认网络权限配置

除互联网权限外,若图片URL为HTTP协议(Firebase Storage默认是HTTPS,此条可忽略),需在AndroidManifest.xml的<application>标签内添加:

android:usesCleartextTraffic="true"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 13:35:58