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

后端 未结 8 550
孤街浪徒
孤街浪徒 2021-01-30 14:25

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

private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
pr         


        
相关标签:
8条回答
  • 2021-01-30 14:51
    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

    0 讨论(0)
  • 2021-01-30 15:09

    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);
    
    0 讨论(0)
  • 2021-01-30 15:09

    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");
    
    0 讨论(0)
  • 2021-01-30 15:10

    Not for Android, but for iOS, but I thought it could be helpful for other people (I didn't find a iOS version of this question).

    Based the provided answers I created a Swift 4.0 extension that adds a function urlForProfileImageFor(imageResolution:) to the Firebase User object. You can either ask for the standard thumbnail, a high resolution (I put this to 1024px but easily changed) or a custom resolution image. Enjoy:

    extension User {
    
        enum LoginType {
            case anonymous
            case email
            case facebook
            case google
            case unknown
        }
    
        var loginType: LoginType {
            if isAnonymous { return .anonymous }
            for userInfo in providerData {
                switch userInfo.providerID {
                case FacebookAuthProviderID: return .facebook
                case GoogleAuthProviderID  : return .google
                case EmailAuthProviderID   : return .email
                default                    : break
                }
            }
            return .unknown
        }
    
        enum ImageResolution {
            case thumbnail
            case highres
            case custom(size: UInt)
        }
    
        var facebookUserId : String? {
            for userInfo in providerData {
                switch userInfo.providerID {
                case FacebookAuthProviderID: return userInfo.uid
                default                    : break
                }
            }
            return nil
        }
    
    
        func urlForProfileImageFor(imageResolution: ImageResolution) -> URL? {
            switch imageResolution {
            //for thumnail we just return the std photoUrl
            case .thumbnail         : return photoURL
            //for high res we use a hardcoded value of 1024 pixels
            case .highres           : return urlForProfileImageFor(imageResolution:.custom(size: 1024))
            //custom size is where the user specified its own value
            case .custom(let size)  :
                switch loginType {
                //for facebook we assemble the photoUrl based on the facebookUserId via the graph API
                case .facebook :
                    guard let facebookUserId = facebookUserId else { return photoURL }
                    return URL(string: "https://graph.facebook.com/\(facebookUserId)/picture?height=\(size)")
                //for google the trick is to replace the s96-c with our own requested size...
                case .google   :
                    guard var url = photoURL?.absoluteString else { return photoURL }
                    url = url.replacingOccurrences(of: "/s96-c/", with: "/s\(size)-c/")
                    return URL(string:url)
                //all other providers we do not support anything special (yet) so return the standard photoURL
                default        : return photoURL
                }
            }
        }
    
    }
    
    0 讨论(0)
  • 2021-01-30 15:11

    Check below response

    final graphResponse = await http.get(
    'https://graph.facebook.com/v2.12/me?fields=name,picture.width(800).height(800),first_name,last_name,email&access_token=${fbToken}');
    
    0 讨论(0)
  • 2021-01-30 15:14

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

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

    simply append "?height=500" at the end

    0 讨论(0)
提交回复
热议问题