How to get a list of user photo albums using the Facebook Graph API on Android

756 views Asked by At

How can you get the list of user photo albums using the Facebook Graph API?

1

There are 1 answers

0
AJ9 On

In the OnCreate of the Activity..

    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

            AccessToken token =  loginResult.getAccessToken();

            GraphRequest.Callback callback = new GraphRequest.Callback() {
                @Override
                public void onCompleted(GraphResponse graphResponse) {
                    //Use the data
                }
            };

            GraphRequest req = GraphRequest.newGraphPathRequest(token, "me/albums",  callback);
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id, name, count");
            req.setParameters(parameters);
            req.executeAsync();


        }

        @Override
        public void onCancel() {
        //User Cancelled
        }

        @Override
        public void onError(FacebookException e) {
            //Handle Error
        }
    });

In your OnClick method:

LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("user_photos"));

You'll also need this:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}