How to get Facebook profile image in Android

anupam x picture anupam x · Aug 31, 2015 · Viewed 34.5k times · Source

I am using Facebook sdk 4.4.0 in android and I want to get current profile picture of the user using graph request. How to do that?

I have seen that people use

https://graph.facebook.com/me/picture?access_token=ACCESS_TOKEN

API to extract profile picture but I cant figure how to extract profile picture from it.

Answer

Rajesh picture Rajesh · Aug 31, 2015

You need to call GraphRequest API for getting all the details of user in which API also gives URL of current profile picture.

Bundle params = new Bundle();
params.putString("fields", "id,email,gender,cover,picture.type(large)");
new GraphRequest(AccessToken.getCurrentAccessToken(), "me", params, HttpMethod.GET,
        new GraphRequest.Callback() {
            @Override
            public void onCompleted(GraphResponse response) {
                if (response != null) {
                    try {
                        JSONObject data = response.getJSONObject();
                        if (data.has("picture")) {
                            String profilePicUrl = data.getJSONObject("picture").getJSONObject("data").getString("url");
                            Bitmap profilePic= BitmapFactory.decodeStream(profilePicUrl .openConnection().getInputStream());
                            mImageView.setBitmap(profilePic);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
}).executeAsync();