从Firebase检索图像以显示在我的Imageview中 [英] Retrive Image from Firebase to show in my Imageview

查看:75
本文介绍了从Firebase检索图像以显示在我的Imageview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能通过Firebase数据库的引用显示存储在Firebase Storage中的图像.

I'm looking forward to Display the images stored in Firebase Storage with refrence from Firebase Database.

以下是我的Firebase数据结构

Below is my Firebase Data Structure

,我的存储结构是

下面是我的回收适配器代码,我正在从Firebase中检索图像

Below is my code of Recycle adapter, I'm using to retrive images from Firebase

public class SubjectBooksAdapter extends RecyclerView.Adapter<SubjectBooksAdapter.MyViewHolder> {
    ArrayList<Books> bookslist;
    CardView cv;
    FirebaseAuth fauth;
    FirebaseDatabase database;
    DatabaseReference dbreference;
    Books b;

    public SubjectBooksAdapter(ArrayList<Books> bookslist){
        this.bookslist = bookslist;
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView bookName,bookAuthor,bookDesc,bookPrice,bookCall;
        ImageView iv;

        MyViewHolder(final View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.my_card_view);

            iv = (ImageView) itemView.findViewById(R.id.imageView);

            database = FirebaseDatabase.getInstance();
            dbreference = database.getReference("books");
            bookName = (TextView) itemView.findViewById(R.id.bookName);
            bookAuthor = (TextView) itemView.findViewById(R.id.bookAuthor);
            bookDesc = (TextView) itemView.findViewById(R.id.bookDesc);
            bookPrice = (TextView) itemView.findViewById(R.id.bookPrice);
            bookCall = (TextView) itemView.findViewById(R.id.bookCall);
            fauth = FirebaseAuth.getInstance();
        }
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {

        database = FirebaseDatabase.getInstance();
        dbreference = database.getReference("books");

        b = bookslist.get(position);
        holder.bookName.setText(b.getBname());
        holder.bookAuthor.setText(b.getBauthor());
        holder.bookDesc.setText(b.getBdesc());
        holder.bookPrice.setText("Rs. "+b.getPrice());
        holder.bookCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Log.e("Current user is ", fauth.getCurrentUser().getEmail());
                b = bookslist.get(position);
                String[] arr = {b.getSelleremail(),b.getSellername(),b.getBname(),b.getBauthor()};
                //Log.e("Seller is ",b.getSellername());
                Intent in = new Intent(v.getContext(),Chat.class);
                in.putExtra("seller",arr);
                v.getContext().startActivity(in);
            }
        });
        Glide.with(cv.getContext()).load(Uri.parse(b.getPics())).placeholder(R.drawable.bshelf).error(R.drawable.bshelf).into(holder.iv);

    }

    @Override
    public int getItemCount() {
        return bookslist.size();
    }
}

但是它显示所有其他细节,但不显示图像...这是屏幕截图

But it shows every other details but don't show the image...Here is the screenshot

因此它显示了可绘制文件夹中的默认bshel​​f.png.

It thus shows the default bshelf.png from drawable folder.

推荐答案

尝试一下:

public class ShowImagesActivity extends AppCompatActivity {
//recyclerview object
private RecyclerView recyclerView;

//adapter object
private RecyclerView.Adapter adapter;

//database reference
private DatabaseReference mDatabase;

//progress dialog
private ProgressDialog progressDialog;

//list to hold all the uploaded images
private List<Upload> uploads;

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


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


    progressDialog = new ProgressDialog(this);

    uploads = new ArrayList<>();

    //displaying progress dialog while fetching images
    progressDialog.setMessage("Please wait...");
    progressDialog.show();
    mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);

    //adding an event listener to fetch values
    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            //dismissing the progress dialog 
            progressDialog.dismiss();

            //iterating through all the values in database
            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                Upload upload = postSnapshot.getValue(Upload.class);
                uploads.add(upload);
            }
            //creating adapter
            adapter = new MyAdapter(getApplicationContext(), uploads);

            //adding adapter to recyclerview
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            progressDialog.dismiss();
        }
    });

}
}

请打开此链接,这对您非常有用:

Please open this link it is very useful to you:

https://www.simplifiedcoding.net/firebase-storage-example/

这篇关于从Firebase检索图像以显示在我的Imageview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆