krishan pathirathne
krishan pathirathne

Reputation: 15

How to retrieve Firebase images to ImageView?

Here is my Firebase Realtime Database structure:

enter image description here

I want to retrieve each and every image in a child(images1,images2,images3,images4 are the child names)

private void loadmybanners1() {
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("BannerImages");
        ref.orderByChild("images2").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot ds : snapshot.getChildren()) {
                    final String myImg1 = "" + ds.child("image").getValue();


                    try{
                        Picasso.get().load(myImg1).networkPolicy(NetworkPolicy.OFFLINE).into(ban2Iv, new Callback() {
                            @Override
                            public void onSuccess() {

                            }

                            @Override
                            public void onError(Exception e) {
                                Picasso.get().load(myImg1).into(ban2Iv);
                            }
                        });
                    } catch (Exception e) {
                        ban2Iv.setImageResource(R.drawable.ic_image);
                    }
                }


            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

I have used this code to retrieve the image2. But from that, I can only retrieve the images1 child data. What are the modifications to do?

Upvotes: 0

Views: 68

Answers (2)

Sahil Goyal
Sahil Goyal

Reputation: 425

Add this dependency

implementation 'com.github.bumptech.glide:glide:4.10.0'

After getting Url from Firebase.Just Use below line

Glide.with(context).load(Firbase Url).into(imageView); 

Upvotes: 0

Alex Mamo
Alex Mamo

Reputation: 138824

I want to retrieve each and every image in a child

To get each and every child within your BannerImages node, there is no need to use a query, you can simply use a DatabaseReference, as in the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference bannerImagesRef = rootRef.child("BannerImages");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String myImg1 = dataSnapshot.child("images1").child("image").getValue(String.class);
        Picasso.get().load(myImg1)
        String myImg2 = dataSnapshot.child("images2").child("image").getValue(String.class);
        Picasso.get().load(myImg2)

        //And so on for the other images3, images4, and images5 children
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
    }
};
bannerImagesRef.addListenerForSingleValueEvent(valueEventListener);

Upvotes: 1

Related Questions