简体   繁体   中英

How to display image from URL from Firestore

I could get it right with Realtime Database, but I went over to Cloud FireStore and now I'm running into some problems. I want to display an image that is in Firestore via a download URL from Firestore. I'm using the FirestoreRecyclerAdapter, I want to populate an imageView in a RecyclerView.

I tried to do it the same way it worked in Realtime Database, but I get an error, but this time I get an error telling me NumberFormatException: For input string:

@Override
protected void onBindViewHolder(@NonNull NoteHolder holder, int p, 
   @NonNull promo_one_data promo_one) {
    holder.produk_product.setText(promo_one.getProduct());
    holder.produk_discription.setText(promo_one.getDiscription());
    holder.produk_discription_two.setText(promo_one.getDiscription_two());
    holder.produk_size.setText(promo_one.getSize());
    holder.produk_product_two.setText(promo_one.getProduct_two());
    holder.produk_price.setText(promo_one.getPrice());
    holder.produk_department.setText(promo_one.getDepartment());
    if ("1".equals(holder.produk_department.getText().toString())) {
        holder.itemView.setBackgroundColor(Color.parseColor("#FFCDD2"));
    }  else if ("2".equals(holder.produk_department.getText().toString())) {
        holder.itemView.setBackgroundColor(Color.parseColor("#FFA6AF"));
    } else {
            holder.itemView.setBackgroundColor(Color.WHITE);
        }
    holder.produk_image.setImageResource(Integer.parseInt(promo_one.getImage()));
    Picasso.get().load(promo_one.getImage()).into(holder.produk_image);

}

@NonNull
@Override
public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_promo, parent, false);
    return new NoteHolder(v);
}

How can I display an image in a recyclerView from Cloud Firestore. It shows me "java.lang.NumberFormatException: For input string:...(the URL here), which I could gather means the string is to long to convert into a int to fill the imageView. How do I do this, or is there a way around? Or even how do I shorten the URL, or maybe is there a way to do it and not use Picasso?

Your problem is this line :

holder.produk_image.setImageResource(Integer.parseInt(promo_one.getImage()));

you should not be trying to parse the URL of the image to an integer here, the next line using Picasso:

Picasso.get().load(promo_one.getImage()).into(holder.produk_image);

should handle the image loading for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM