Search code examples
androidfacebookphotos

Getting user photos using Facebook login on android


I'm using Facebook SDK 4.0 and I'm able to login a user using Facebook login on android using this -

info = (TextView)findViewById(R.id.info);    
loginButton = (LoginButton)findViewById(R.id.login_button);    


  loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                info.setText("User ID:  " +
                        loginResult.getAccessToken().getUserId() + "\n" +
                        "Auth Token: " + loginResult.getAccessToken().getToken());
            }

            @Override
            public void onCancel() {
                info.setText("Login attempt cancelled.");
            }

            @Override
            public void onError(FacebookException e) {
                info.setText("Login attempt failed.");
            }
        });

But how can I go to get user's photos ? Any help is appreciated.


Solution

  • You can use this :

    https://graph.facebook.com/{Facebook_id}/picture?type=large
    

    for example Mark Zuckerberg's Facebook ID is 4 so you can use it like this :

    https://graph.facebook.com/4/picture?type=large

    In your case you need to put it inside the onSuccess :

            @Override
            public void onSuccess(LoginResult loginResult) {
                String userId = loginResult.getAccessToken().getUserId();
                String token = loginResult.getAccessToken().getToken();
                info.setText("User ID:  " +
                        userId + "\n" +
                        "Auth Token: " + token);
                String imageUrl = String.format("https://graph.facebook.com/%s/picture?type=large", userId);
            }