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

后端 未结 8 557
孤街浪徒
孤街浪徒 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 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
                }
            }
        }
    
    }
    

提交回复
热议问题