Home > Mobile >  Facebook profile picture is not loading in my app
Facebook profile picture is not loading in my app

Time:11-19

I have implemented Facebook SDK and Firebase implementations. I am trying to display name, email, and profile picture of the user who signs in with Facebook. Name and email are getting loaded but the profile picture is not getting loaded.

Here is the code:

Kotlin Code

auth= FirebaseAuth.getInstance()
        val currentuser = auth.currentUser
        email.text=currentuser?.email
        val photourl = currentuser?.photoUrl.toString()
        Log.d("url","URL of profile image is $photourl")
        Glide.with(this).load(currentuser?.photoUrl.toString()).into(profile_image)

The XML Code

  <ImageView
      android:id="@ id/profile_image"
      android:layout_width="150dp"
      android:layout_height="150dp"
      android:layout_marginTop="70dp"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.5"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      tools:srcCompat="@tools:sample/avatars" />

I am also attaching an image of the Activity: Dashboard Activity

CodePudding user response:

You aren't getting the correct image URL because of the following line of code:

val photourl = currentuser?.photoUrl.toString()

And this is happening because you are trying to get the URL from the FirebaseUser object. When you authenticate your users with Facebook, the photoUrl field inside the currentuser object is not populated. To be able to actually get that URL, you need to use the following lines of code:

val auth = FirebaseAuth.getInstance()
auth.currentUser?.apply {
    for (userInfo in providerData) {
        if (userInfo.providerId == "facebook.com") {
            val photoUrl = userInfo.photoUrl
            Log.d("TAG", photoUrl.toString())
        }
    }
}

So you have to get the URL from the UserInfo object and not from the FirebaseUser object.

CodePudding user response:

Use FirebaseUser Object

private FirebaseUser user;
FirebaseAuth auth = FirebaseAuth.getInstance();
user = auth.getCurrentUser();

Now Call - user.getPhotoUrl()

Glide.with(getApplicationContext()).load(user.getPhotoUrl()).into(userImage);
  • Related