Facebook 个人资料图片未加载到我的应用程序中

Facebook profile picture is not loading in my app

我已经实现了 Facebook SDK 和 Firebase 实现。我正在尝试显示使用 Facebook 登录的用户的姓名、电子邮件和个人资料图片。正在加载姓名和电子邮件,但未加载个人资料图片。

代码如下:

Kotlin 代码

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)

XML代码

  <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" />

我还附上了 Activity 的图片:

由于以下代码行,您没有得到正确的图像 URL:

val photourl = currentuser?.photoUrl.toString()

发生这种情况是因为您正试图从 FirebaseUser 对象获取 URL。当您使用 Facebook 对用户进行身份验证时,currentuser 对象中的 photoUrl 字段不会被填充。为了能够真正获得 URL,您需要使用以下代码行:

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())
        }
    }
}

因此您必须从 UserInfo 对象而不是 FirebaseUser 对象获取 URL。

使用 FirebaseUser 对象

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

现在调用 - user.getPhotoUrl()

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