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

Cannot fetch the image stored in Firebase database (statically) into my application

我正在尝试访问存储在 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

您的视图中没有显示任何内容,因为您的 image 属性 而不是 持有正确的图像 URL。您存储的内容:

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

它实际上是您的图像在 Firebase 存储中的位置,而不是实际的 URL。要解决此问题,当您创建 Products class 的新对象时,按照我在以下 post 中的回答中的解释传递图像的下载 URL:

  • How to get the download url from Firebase Storage?