java - 无法将 Firebase 数据库中存储的图像(静态)提取到我的应用程序中

标签 java android firebase-realtime-database firebase-storage picasso

我正在尝试访问存储在 Firebase 存储中的图像,但它没有显示在应用程序中。相反,所有细节(例如产品名称、产品描述和价格)都可以完美看到,但图像未显示,甚至 Logcat 中也没有错误。

我是 Android Studio 的初学者。

这是我的 Java,代号为“KeyChainActivity.java”

 package com.example.giftngame;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;


import com.example.giftngame.Model.Products;
import com.example.giftngame.ViewHolder.ProductViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import com.squareup.picasso.Picasso;

public class KeyChainActivity extends AppCompatActivity
{
    private DatabaseReference ProductsRef;
    private RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;

    private String CategoryName;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);

        CategoryName=getIntent().getExtras().get("category").toString().toString();
        Toast.makeText(this,CategoryName,Toast.LENGTH_SHORT).show();

        ProductsRef= FirebaseDatabase.getInstance().getReference().child("Products");

        recyclerView=findViewById(R.id.recycler_menu);
        recyclerView.setHasFixedSize(true);
        layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

    }

    @Override
    protected void onStart()
    {
        super.onStart();

        FirebaseRecyclerOptions<Products> options=
                new FirebaseRecyclerOptions.Builder<Products>()
                .setQuery(ProductsRef,Products.class)
                .build();


        FirebaseRecyclerAdapter<Products, ProductViewHolder> adapter=
                new FirebaseRecyclerAdapter<Products, ProductViewHolder>(options) {
                    @Override
                    protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull Products model)
                    {

                        holder.txtProductName.setText(model.getPname());
                        holder.txtProductDescription.setText(model.getDescription());
                        holder.txtProductPrice.setText("Price= "+model.getPrice()+ " INR");
                        Picasso.get().load(model.getImage()).into(holder.imageView);



                    }

                    @NonNull
                    @Override
                    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
                    {
                        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_key_chain,parent,false);
                        ProductViewHolder holder=new ProductViewHolder(view);
                        return holder;

                    }
                };

        recyclerView.setAdapter(adapter);
        adapter.startListening();


    }
}

这是另一个 java 代码,我在其中声明了名为“Products.java”的 Firebase 数据库内容

     package com.example.giftngame.Model;

public class Products
{
    private String pname,description,image,category,date,time;
    private long pid,price;

    public Products()
    {

    }

    public Products(String pname, String description, long price, String image, String category, long pid, String date, String time)
    {
        this.pname = pname;
        this.description = description;
        this.price = price;
        this.image = image;
        this.category = category;
        this.pid = pid;
        this.date = date;
        this.time = time;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public long getPrice() {
        return price;
    }

    public void setPrice(long price) {
        this.price = price;
    }

    public String  getImage() {
        return image;
    }

    public void setImage(String  image) {
        this.image = image;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public long getPid() {
        return pid;
    }

    public void setPid(long pid) {
        this.pid = pid;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

This is the image of my Firebase DataBase

最佳答案

您的 View 中没有显示任何内容,因为您的 image 属性包含正确的图像 URL。您正在存储的内容:

gs://giftngame-ae160-appspot...

它实际上是您的图片在 Firebase 存储中的位置,而不是实际的网址。要解决此问题,当您创建 Products 类的新对象时,请传递图像的下载 URL,如我在以下帖子中的回答中所述:

关于java - 无法将 Firebase 数据库中存储的图像(静态)提取到我的应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59526783/

相关文章:

android - 如何检查Firebase实时数据库中多个节点的读取完成情况?

firebase - 验证 Firebase 实时数据库规则中匿名用户的显示名称

android - 谷歌地图 API V2 'Failed to Load Map. Could not contact Google Servers'

Android:如何在 FrameLayout 元素中绘制?

JavaFX 计算器应用程序设置操作事件

java - 解决间歇性垃圾收集问题 - Java

android - 以编程方式将 WhatsApp 发送到没有选择器的联系人列表

javascript - JS NG2 Firebase - 将 foreach 值添加到数组并在模板中使用

Java 的 Func 和 Action 等价物

java - 检查最终实例变量是否已初始化