Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Firebase Auth - Get User's Photo

How can I retrieve a photo of the user with decent resolution that can be used from a mobile app? I looked at the guides and the api docs and the recommended way seemed to be to use FirebaseUser#getPhotoUrl(). This however gives back a url to a photo with resolution 50x50 px, which is too low to be useful. Is there a way for the client to request a higher res photo of the user? I've tested the sdks of Facebook Login and Google Sign-in separately, and in both cases the resolutions of the photos are higher than what Firebase Auth gives back. Why does Firebase Auth change the original resolutions and how can I force it not to do that? Thanks.

like image 474
mobilekid Avatar asked May 23 '16 06:05

mobilekid


4 Answers

You can have better resolution profile pics, directly editing the URL for both providers (Google and Facebook):

Here is a javascript sample code but, you should be able to easily translate to Java:

getHigherResProviderPhotoUrl = ({ photoURL, providerId }: any): ?string => {
    //workaround to get higer res profile picture
    let result = photoURL;
    if (providerId.includes('google')) {
      result = photoURL.replace('s96-c', 's400-c');
    } else if (providerId.includes('facebook')) {
      result = `${photoURL}?type=large`;
    }
    return result;
  };

Basically, depending on the provider, you just need to:

  • for google replace s96-c in the profile pic url with s400-c
  • for facebook just append ?type=large at the end of the url

For example for google:

low-res pic becomes high-res pic

And for facebook:

low-res pic becomes high-res pic

like image 158
Gomino Avatar answered Oct 16 '22 09:10

Gomino


Facebook and Google PhotoURL :

       User myUserDetails = new User();
        myUserDetails.name = firebaseAuth.getCurrentUser().getDisplayName();
        myUserDetails.email = firebaseAuth.getCurrentUser().getEmail();

        String photoUrl = firebaseAuth.getCurrentUser().getPhotoUrl().toString();
        for (UserInfo profile : firebaseAuth.getCurrentUser().getProviderData()) {
            System.out.println(profile.getProviderId());
            // check if the provider id matches "facebook.com"
            if (profile.getProviderId().equals("facebook.com")) {

                String facebookUserId = profile.getUid();

                myUserDetails.sigin_provider = profile.getProviderId();
                // construct the URL to the profile picture, with a custom height
                // alternatively, use '?type=small|medium|large' instead of ?height=

                photoUrl = "https://graph.facebook.com/" + facebookUserId + "/picture?height=500";

            } else if (profile.getProviderId().equals("google.com")) {
                myUserDetails.sigin_provider = profile.getProviderId();
                ((HomeActivity) getActivity()).loadGoogleUserDetails();
            }
        }
        myUserDetails.profile_picture = photoUrl;




private static final int RC_SIGN_IN = 8888;    

public void loadGoogleUserDetails() {
        try {
            // Configure sign-in to request the user's ID, email address, and basic profile. ID and
            // basic profile are included in DEFAULT_SIGN_IN.
            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .build();

            // Build a GoogleApiClient with access to GoogleSignIn.API and the options above.
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                        @Override
                        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                            System.out.println("onConnectionFailed");
                        }
                    })
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .build();

            Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
            startActivityForResult(signInIntent, RC_SIGN_IN);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from
        //   GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) {
                GoogleSignInAccount acct = result.getSignInAccount();
                // Get account information
                String PhotoUrl = acct.getPhotoUrl().toString();

            }
        }
    }
like image 27
Jitty Aandyan Avatar answered Oct 16 '22 09:10

Jitty Aandyan


Inside onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth)

Try If you login with Facebook:

 if (!user.getProviderData().isEmpty() && user.getProviderData().size() > 1)
                String URL = "https://graph.facebook.com/" + user.getProviderData().get(1).getUid() + "/picture?type=large";
like image 8
Juan Labrador Avatar answered Oct 16 '22 09:10

Juan Labrador


Have you tried:

Uri xx = FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl();
like image 5
ErstwhileIII Avatar answered Oct 16 '22 11:10

ErstwhileIII