Reputation: 17
I'm trying to update the user profile image in the Firebase Realtime Database on my application but I'm unable to get the reference of the current user as I declare it in another activity.
My code and database structure is as follows:
as in this current picture and the code, I'm writing manually the id of the child node (gli) because it is the user_name of the current user in the firebase, please help me how can I give it a path through coding.
code:
private void profileImage(){
pImageRef = FirebaseDatabase.getInstance().getReference("doctors/doctors_registration/gli/pimage"); 👈👈
pImageRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String imageurl= String.valueOf(dataSnapshot.getValue()) ;
Toast.makeText(HomeActivity.this, "link:"+imageurl, Toast.LENGTH_SHORT).show();
Picasso.get()
.load(imageurl)
.into(profileimg);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Please see the screenshot of the database structure and help me how I can get the current user profile image?
Upvotes: 1
Views: 85
Reputation: 139029
First of all, stop ignoring errors. At a minimum, please add inside the onCancelled method:
Log.d(TAG, error.getMessage());
Please also note that Picasso is a library that can help you download images and not update them in the Realtime Database. If you want to update the image at an existing location, remember that there is no need to read it first. To solve this, please use the following line of code:
pImageRef.updateChildren("yourNewUrl");
You can also use in this case addOnCompleteListener()
to see if something goes wrong.
If you however need to update the image right after you read it, then inside the onDataChange() method, use the following line of code:
dataSnapshot.getRef().updateChildren("yourNewUrl");
Upvotes: 1