Android : How to get larger profile pic from Facebook using FirebaseAuth?

16,010

Solution 1

It is not possible to obtain a profile picture from Firebase that is larger than the one provided by getPhotoUrl(). However, the Facebook graph makes it pretty simple to get a user's profile picture in any size you want, as long as you have the user's Facebook ID.

String facebookUserId = "";
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
ImageView profilePicture = (ImageView) findViewById(R.id.image_profile_picture);

// find the Facebook profile and get the user's id
for(UserInfo profile : user.getProviderData()) {
    // check if the provider id matches "facebook.com"    
    if(FacebookAuthProvider.PROVIDER_ID.equals(profile.getProviderId())) {
        facebookUserId = profile.getUid();
    }
}

// construct the URL to the profile picture, with a custom height
// alternatively, use '?type=small|medium|large' instead of ?height=
String photoUrl = "https://graph.facebook.com/" + facebookUserId + "/picture?height=500";

// (optional) use Picasso to download and show to image
Picasso.with(this).load(photoUrl).into(profilePicture);

Solution 2

Two lines of code. FirebaseUser user = firebaseAuth.getCurrentUser();

String photoUrl = user.getPhotoUrl().toString();
        photoUrl = photoUrl + "?height=500";

simply append "?height=500" at the end

Solution 3

If someone is looking for this but for Google account using FirebaseAuth. I have found a workaround for this. If you detail the picture URL:

https://lh4.googleusercontent.com/../.../.../.../s96-c/photo.jpg

The /s96-c/ specifies the image size (96x96 in this case)so you just need to replace that value with the desired size.

String url= FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl();
url = url.replace("/s96-c/","/s300-c/");

You can analyze your photo URL to see if there is any other way to change its size.

As I said in the begining, this only works for Google accounts. Check @Mathias Brandt 's answer to get a custom facebook profile picture size.

EDIT 2020:

Thanks to Andres SK and @alextouzel for pointing this out. Photo URLs format have changed and now you can pass URL params to get different sizes of the picture. Check https://developers.google.com/people/image-sizing.

Solution 4

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

You can store this link to firebase database with user facebookId and use this in app. Also you can change height as a parameter

Solution 5

I use this code in a Second Activity, after having already logged in, for me the Token that is obtained in loginResult.getAccessToken().getToken(); It expires after a while, so researching I found this and it has served me

final String img = mAuthProvider.imgUsuario().toString(); // is = mAuth.getCurrentUser().getPhotoUrl().toString;
        
final String newToken = "?height=1000&access_token=" + AccessToken.getCurrentAccessToken().getToken();
        
Picasso.get().load(img + newToken).into("Image reference");
Share:
16,010

Related videos on Youtube

Gaurav Sarma
Author by

Gaurav Sarma

Working on new ideas in technology. Always looking to improve my knowledge regarding all things tech.

Updated on December 14, 2020

Comments

  • Gaurav Sarma
    Gaurav Sarma over 3 years

    I am using FirebaseAuth to login user through FB. Here is the code:

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private CallbackManager mCallbackManager;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
    
        // Initialize Firebase Auth
        mAuth = FirebaseAuth.getInstance();
    
        mAuthListener = firebaseAuth -> {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
    
            if (user != null) {
                Log.d(TAG, "User details : " + user.getDisplayName() + user.getEmail() + "\n" + user.getPhotoUrl() + "\n"
                        + user.getUid() + "\n" + user.getToken(true) + "\n" + user.getProviderId());
            }
        };
    }
    

    The issue is that the photo in I get from using user.getPhotoUrl() is very small. I need a larger image and can't find a way to do that. Any help would be highly appreciated. I have already tried this Get larger facebook image through firebase login but it's not working although they are for swift I don't think the API should differ.

    • Frank van Puffelen
      Frank van Puffelen almost 8 years
    • Gaurav Sarma
      Gaurav Sarma almost 8 years
      @FrankvanPuffelen i have tried those answer and it didn't work + its for swift although the API should be similar but still its not working could you please remove your review
    • Frank van Puffelen
      Frank van Puffelen almost 8 years
      The approach to get such information is the same on every platform. If you have tried the approach outlined there, share what you've tried.
    • Gaurav Sarma
      Gaurav Sarma almost 8 years
      As i have shared in the code above i am using the FirebaseAuthListener and i get the following url with the user.getPhotoUrl() scontent.xx.fbcdn.net/v/t1.0-1/p100x100/…
    • Gaurav Sarma
      Gaurav Sarma almost 8 years
      @FrankvanPuffelen Did it work for you ? Waiting for your king response
    • Frank van Puffelen
      Frank van Puffelen almost 8 years
      Hmmm... I can't get Facebook auth to work on my device at the moment. Re-opened in hopes somebody else can chime in.
  • Gaurav Sarma
    Gaurav Sarma almost 8 years
    Although i had already found the answer thanks for posting it here i hope it helps other who are looking for the same solution. Please upvote the question i have accepted your answer. Thanks @Mathias
  • Yunus Haznedar
    Yunus Haznedar about 7 years
    what is facebook_provider_id? Is it facebook_app_id?
  • mfb
    mfb about 7 years
    facebook_provider_id is simply "facebook.com". I put it in Strings.xml so I could easily change it if Firebase decided to change it later for whatever reason.
  • Yunus Haznedar
    Yunus Haznedar about 7 years
    Sir, I'm getting "Target must not be null" error. Could you check out this:stackoverflow.com/questions/43260782/…
  • Riddhi Shankar
    Riddhi Shankar over 5 years
    Thank you so much!!
  • Momen Zaqout
    Momen Zaqout about 5 years
    What creativity!
  • li_developer
    li_developer about 5 years
    In latest Facebook API v3.3, the valid parameters for type values has changed The size of this picture. It can be one of the following values: small, normal, large, square.
  • Adrian Geanta
    Adrian Geanta over 4 years
    It's working, but .. The image clarity is so bad.. Can I do something about that ?
  • Andres SK
    Andres SK about 4 years
    Update: this doesn't seem to work in 2020. Now you get urls like https://lh3.googleusercontent.com/a-/AOh14Gg2pCyl90sYWeqrm4j‌​TC2xI3o0CFUzK_x7-Pcm‌​C3rA
  • alextouzel
    alextouzel almost 4 years
    @AndresSK We can now append different params to the URL to get different sizes. To get the size you want, you simply add "=s200" where 200 is the size. See the following link for all options: https://developers.google.com/people/image-sizing