简体   繁体   中英

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

I used Firebase Authentication with only Email/Password in an android app project. I tried to access the picture of the email account which has been used to log into the app. However, the photoUrl is null when I try these three methods below and I am testing with my own email account which I know has a profile picture.

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

I am able to get the name of the email account easily like this

    String name = mFirebaseUser.getDisplayName();

Try This

  private FirebaseAuth mAuth;
  private FirebaseUser mCurrentUser;

Initialize it

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

Load the Photo Url Using Picasso in Your ImageView

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

As you are using Email sign in only in FirebaseAuth, so the problem happens when you try to retrieve the photo url of the user because there is no photo url for the user by default. So what you can do is you can ask user to set the photo and save the url with the FirebaseAuth (so that you can retrieve later). You can use the code below to save the photo url for the user.

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

after setting the photo url. Try getting the photo url

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

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM