Al Hennessey
Al Hennessey

Reputation: 2445

Getting Facebook Profile Picture in Android

i am building an android app and have successfully integrated Facebook login as the only form of registration and login. I am trying to work out the best way of getting the users profile picture, i have had allow at the developer docs - https://developers.facebook.com/docs/graph-api/reference/user/picture/, however i am not sure how to handle the response, by that i mean how do i actually get the image in the response, is it sent via a bitmap or as a url link, i am not quite sure.

Here is the code the docs provide for getting the profile picture

    /* make the API call */
new GraphRequest(
    AccessToken.getCurrentAccessToken(),
    "/{user-id}/picture",
    null,
    HttpMethod.GET,
    new GraphRequest.Callback() {
        public void onCompleted(GraphResponse response) {
            /* handle the result */
        }
    }
).executeAsync();

Thanks for the help

Upvotes: 2

Views: 807

Answers (1)

McGuile
McGuile

Reputation: 828

You have to perform a GraphRequest like the Docs say but this is how you do so. Use Facebook's ProfilePictureView in your XML layout.

XML Layout

<com.facebook.login.widget.ProfilePictureView
        android:id="@+id/myProfilePic"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"/>

Activity/Fragment Code

ProfilePictureView profilePic = (ProfilePictureView) dialogView.findViewById(R.id.myProfilePic);
GraphRequestAsyncTask request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
    @Override
    public void onCompleted(JSONObject user, GraphResponse response) {
        if (user != null) {
            // set the profile picture using their Facebook ID
            profilePic.setProfileId(user.optString("id"));
        }
    }
}).executeAsync();

Upvotes: 4

Related Questions