簡體   English   中英

如何通過具有電子郵件登錄功能的FirebaseAuth獲取登錄Android應用的電子郵件帳戶用戶的照片url

[英]how to get photo url of email account user signed into android app through FirebaseAuth with Email signIn

我在Android應用程序項目中僅對電子郵件/密碼使用了Firebase身份驗證。 我試圖訪問已用於登錄應用程序的電子郵件帳戶的圖片。 但是,當我嘗試以下三種方法時,photoUrl為空,並且正在使用自己的電子郵件帳戶進行測試,我知道該帳戶具有個人資料圖片。

    // METHOD ONE
    Uri photoUrl = mFirebaseUser.getPhotoUrl();

    if (photoUrl != null) {
        log.d("PhotoUrl",photoUrl.toString());
    }


    // METHOD TWO
    UserInfo userInfo = mFirebaseUser.getProviderData().get(0);
    Uri photoUrl = (Uri) userInfo.getPhotoUrl();

    if (photoUrl != null) {
        log.d("PhotoUrl",photoUrl.toString());
    }


    // METHOD THREE
    for (UserInfo userInfo:mUser.getProviderData()) {

        Uri photoUrl = userInfo1.getPhotoUrl();

        if (photoUrl != null){
            break;
        }
    }

    if (photoUrl != null) {
        log.d("PhotoUrl",photoUrl.toString());
    }

我可以像這樣輕松獲得電子郵件帳戶的名稱

    String name = mFirebaseUser.getDisplayName();

嘗試這個

  private FirebaseAuth mAuth;
  private FirebaseUser mCurrentUser;

初始化

mAuth = FirebaseAuth.getInstance();
mCurrentUser = mAuth.getCurrentUser();

在您的ImageView中使用Picasso加載照片網址

 Picasso.get().load(mCurrentUser.getPhotoUrl())
                .into(imageView);

由於僅在FirebaseAuth中使用電子郵件登錄,因此當您嘗試檢索用戶的照片url時會發生問題,因為默認情況下該用戶沒有照片url。 因此,您可以要求用戶設置照片並使用FirebaseAuth保存網址(以便以后可以檢索)。 您可以使用以下代碼為用戶保存照片網址。

UserProfileChangeRequest userProfileChangeRequest = new UserProfileChangeRequest.Builder() .setDisplayName("set new display name") .setPhotoUri(uri) .build(); FirebaseAuth.getInstance().getCurrentUser().updateProfile(userProfileChangeRequest);

設置照片網址后。 嘗試獲取照片網址

FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl();

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM