我正在尝试访问存储在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;
}
}
在您的视图中没有显示任何内容,因为您的image
属性确实不是包含正确的图像URL。您存储的内容:
gs:// giftngame-ae160-appspot ...
它实际上是图像在Firebase存储中的位置,而不是实际的URL。为了解决这个问题,当您创建Products
类的新对象时,请按照以下帖子中我的回答中的说明传递图像的下载URL: