【问题标题】:Cannot fetch the image stored in Firebase database (statically) into my application无法将存储在 Firebase 数据库中的图像(静态)提取到我的应用程序中
【发布时间】:2020-04-18 23:03:21
【问题描述】:

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

【问题讨论】:

  • 先生,您是否在浏览器中打开图像链接进行检查。此链接是否有效
  • 您尝试打开的网址无效。因为它通过 appid 提供了图像在存储中的位置。所以我建议提供上传文件代码。或者你必须做正则表达式来检索图像的位置。
  • 我可以做些什么来使图像可见吗?
  • model.getImage() 是什么?它是字符串 url 还是其他东西?
  • model 是我在产品的 onBindViewHolder 方法中声明的变量的名称

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


【解决方案1】:

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

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

它实际上是您的图片在 Firebase 存储中的位置,而不是实际的 URL。为了解决这个问题,当您创建 Products 类的新对象时,请按照我在以下帖子中的回答中的说明传递图像的下载 URL:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 2020-09-26
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多