Android: Jak uzyskać większe zdjęcie profilowe z Facebook za pomocą Firebaseauthor?

Używam Firebaseauthor do logowania użytkownika przez FB. Oto kod:

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

Problem w tym, że zdjęcie, które otrzymuję z używania user.getPhotoUrl() jest bardzo małe. Potrzebuję większego obrazu i nie mogę znaleźć na to sposobu. Każda pomoc będzie bardzo mile widziana. Próbowałem już tego uzyskaj większy obraz facebook poprzez firebase login ale to nie działa, chociaż są one dla swift nie sądzę, API powinno się różnić.

Author: neer17, 2016-08-23

5 answers

Nie jest możliwe uzyskanie zdjęcia profilowego z Firebase, które jest większe niż to Dostarczone przez getPhotoUrl(). Jednak Facebook Facebook Wykres sprawia, że dość proste, aby uzyskać zdjęcie profilowe użytkownika w dowolnym rozmiarze chcesz, tak długo, jak masz użytkownika 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);
 26
Author: Mathias Brandt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-28 12:40:05

Jeśli ktoś szuka tego, ale dla konta Google za pomocą Firebaseauthor. Znalazłem rozwiązanie tego problemu. Jeśli podasz adres URL obrazka:

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

/S96-c/ określa rozmiar obrazu (w tym przypadku 96x96), więc wystarczy zastąpić tę wartość żądanym rozmiarem.

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

Możesz przeanalizować adres URL zdjęcia, aby sprawdzić, czy istnieje inny sposób na zmianę jego rozmiaru.

Jak powiedziałem w na początku działa to tylko dla kont Google. Sprawdź odpowiedź @ Mathias Brandt 's, aby uzyskać niestandardowy rozmiar zdjęcia profilu facebook.

 8
Author: Sebastian Duque,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-03 07:35:03

Dwie linijki kodu. FirebaseUser user = firebaseAuth.getCurrentUser();

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

Po prostu dołącz "?height=500" na końcu

 8
Author: Soropromo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-05 23:14:35
photoUrl = "https://graph.facebook.com/" + facebookId+ "/picture?height=500"

Możesz zapisać ten link do bazy danych firebase za pomocą facebookId użytkownika i użyć go w aplikacji. Możesz również zmienić wysokość jako parametr

 2
Author: Omelchenko Anatolii,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-27 21:26:20

Nie dla Androida, ale dla iOS, ale pomyślałem, że może to być pomocne dla innych ludzi(nie znalazłem wersji iOS tego pytania).

Na podstawie podanych odpowiedzi stworzyłem rozszerzenie Swift 4.0, które dodaje funkcję urlForProfileImageFor(imageResolution:) do obiektu Firebase User. Możesz poprosić o standardową miniaturę, wysoką rozdzielczość (ustawiłem to na 1024px, ale łatwo zmieniłem) lub niestandardową rozdzielczość obrazu. 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
Author: HixField,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-21 14:19:39