Test User
Test User

Reputation: 21

display image from firebase database in imageview

There are no direct answers to this question, I have tried various methods. I need an image stored in my Firebase database to be displayed in an ImageView.

This is the current code I have, which is not working:

mUsersDB_photoUrl.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String photoUrl = dataSnapshot.child("photoUrl").getValue(String.class);
            try {
                Picasso.with(DisplayActivity.this).load(photoUrl).placeholder(R.mipmap.ic_launcher).into(Photo);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Method for calling in username works:

mUsersDB_name.addValueEventListener(new ValueEventListener() {
        @Override

        public void onDataChange(DataSnapshot dataSnapshot) {
            Name.setText(dataSnapshot.getValue(String.class));

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

I'm not that great at all of this, I'm teaching myself Firebase. Pointers and guidance welcomed.

Upvotes: 2

Views: 5185

Answers (3)

Test User
Test User

Reputation: 21

I ended up just changing my database reference. (First line of the code)

Works perfectly.

mUsersDB.child(user_id).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String photoUrl = dataSnapshot.child("photoUrl").getValue(String.class);
            try {
                Picasso.with(DisplayActivity.this).load(photoUrl).placeholder(R.mipmap.ic_launcher).into(disPhoto);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Upvotes: 0

S.M
S.M

Reputation: 641

You are using Picasso, so I suggest you to use getDownloadUrl() while fetching image.

StorageReference storageRef = storage.getReference().child("profiles").child(myAccount.getUID()).child("profile");//reach out to your photo file hierarchically
        storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                Log.d("URI", uri.toString()); //check path is correct or not ?
               Picasso.with(DisplayActivity.this).load(uri.toString()).into(imageView);
            }
        }).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
    // Handle errors
}
});

Upvotes: 2

Mohamed Nagy Mostafa
Mohamed Nagy Mostafa

Reputation: 353

okey .. first set Log below this code line and check if photo url is valid or not.

String photoUrl = dataSnapshot.child("photoUrl").getValue(String.class);

then use glide to set your image to ImageView

Glide.with([CONTEXT]).load([IMAGE_URL]).into([IMAGE_VIEW]);

NOTE : if you want use Picasso then you have to pass your context to with([CONTEXT])

Upvotes: 0

Related Questions