Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Facebook profile image in Android

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.

like image 728
anupam x Avatar asked Aug 31 '15 12:08

anupam x


People also ask

Can you download Facebook profile pictures?

You can download all photos at once from Facebook only within your profile account or page, regardless of whether you're using a browser or the mobile app version of Facebook. In the Facebook app or within a browser, navigate to “Settings,” then tap/click on “Your Facebook information.”


3 Answers

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();
like image 162
Rajesh Avatar answered Oct 11 '22 19:10

Rajesh


From last sdk 4.5.0

 String url;
 Bundle parametersPicture = new Bundle();
 parametersPicture.putString("fields", "picture.width(150).height(150)");

 GraphResponse lResponsePicture = new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/",
                        parametersPicture, null).executeAndWait();
 if (lResponsePicture != null && lResponsePicture.getError() == null &&
                            lResponsePicture.getJSONObject() != null) {
     url = lResponsePicture.getJSONObject().getJSONObject("picture")
                                .getJSONObject("data").getString("url");
 }
like image 31
Dmitriy Puchkov Avatar answered Oct 11 '22 17:10

Dmitriy Puchkov


Get Image From Facebook

String image_url = "http://graph.facebook.com/" + Profile.getCurrentProfile().getId() + "/picture?type=large";
Glide.with(activity)
     .load(image_url)
     .into(imageView);

dependency

implementation 'com.github.bumptech.glide:glide:4.1.1'
like image 23
Ahamadullah Saikat Avatar answered Oct 11 '22 19:10

Ahamadullah Saikat