How can I use Glide with Firebase Database?

Viewed 608

I have a problem about Glide. I want to download image with using Glide. When I use a link is working like this;

Glide.with(activity.this).load(pics direct url).into(imageview);

But when I use the Firebase Database is not working like this;

FirebaseDatabase firebasedatabase = FirebaseDatabase().getInstance().getReference(“name of database”).child(id).child(“imageUrl”);

Glide.with(activity.this).load(firebasedatabase).into(imageview);

Where is the wrong? How can I fix it? Thank you so much everyone!

2 Answers

Your problem is that the following line returns FirebaseDatabase not a string:

FirebaseDatabase firebasedatabase = FirebaseDatabase().getInstance().getReference(“name of database”).child(id).child(“imageUrl”);

You need to listen to the string value when it's fetched and then add the Glide line like this:

firebasedatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String imageUrl = dataSnapshot.getValue(String.class);
            Glide.with(activity.this).load(imageUrl).into(imageview);

        }

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

        }
    })

You are using 'firebasedatabase' object in load istead of an URL. In load ,we put url.

            DatabaseReference myref2 = FirebaseDatabase.getInstance().getReference("Yourself").child(userid).child(s);
            myref2.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    String image = dataSnapshot.child("image").getValue(String.class);
                    Glide.with(getContext()).load(image).into(ImageView)
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
Related