WISHY
WISHY

Reputation: 11999

Get facebook profile picture with preferred width and height, android?

I am getting the facebook profile information and also the profile picture.

public void getProfileInformationFacebook(AccessToken accToken) {
    GraphRequest request = GraphRequest.newMeRequest(
            accToken,
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(
                        JSONObject object,
                        GraphResponse response) {
                    Log.e("object", object.toString());
                    String fbId = null;
                    String fbEmail = null;
                    String fbName = null;
                    String fbUrl = null;

                    try {
                        fbId = object.getString("id");
                        fbEmail = object.getString("email");
                        fbName = object.getString("name");
                        JSONObject picture = object.getJSONObject("picture");
                        JSONObject pictureData = picture.getJSONObject("data");
                        fbUrl = pictureData.getString("url");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } finally {
                        regDetails("Facebook", fbId, fbName, fbEmail, fbUrl);
                    }
                }
            });
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,email,location,birthday,picture");
    request.setParameters(parameters);
    request.executeAsync();
}

The response I receive is this

{"picture":{"data":{"url":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-xfa1\/v\/t1.0-1\/c0.0.50.50\/p50x50\/32044_407244053957_1365573_n.jpg?oh=df9a5f89e6b19af9942cb948c952a026&oe=57559847&__gda__=1465809549_0402ad5c0dcf64f932ceabc982b02f5f","is_silhouette":false}},"id":"10153520390508958","email":"[email protected]","name":"Wishy Gupta"}

And the profile image I receive from the URL is too small. How to increase its width and height??

Upvotes: 1

Views: 868

Answers (4)

jshyam09
jshyam09

Reputation: 1

Don't declare two objects to parse the url object, just use below line of code :

String profilePicUrl = object.getJSONObject("picture").getJSONObject("data").getString("url");

Here profilePicUrl, contains the parsed url, something like :

https://xxxxxxxx.xx.fbfbf.net/v/t1.1-1/p50x50/11111111_11111111111111111_1111111111111111111_n.jpg?_nc_cat=0&oh=1ac96e312de6b0e9386c04a4462f4175&oe=1A1A1111

Upvotes: -1

andyrandy
andyrandy

Reputation: 73984

This would be the API call to get a preferred size:

/me/picture?width=400&height=200

(or with redirect=false to not get redirected)

The type parameter is another way, but it´s not that specific. Although, keep in mind that you will not always get a picture in that specific size, it´s more like the "minimum width/height".

Upvotes: 3

Ajinkya
Ajinkya

Reputation: 1077

Use this type of url to get large picture

http://graph.facebook.com/"+fbId+"/picture?type=large 

so your url will be http://graph.facebook.com/10153520390508958/picture?type=large it will redirect user to

https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfa1/v/t1.0-1/p200x200/32044_407244053957_1365573_n.jpg?oh=093ba782d1b509d64c7185fb17c1293f&oe=575AC1B2&gda=1465526737_22e158b3e9e01c68ee8ca303f2934170

but will get larger picture with size 200 by 200

Upvotes: 2

Shubham A.
Shubham A.

Reputation: 2534

You can do this,

fbProfilePicURL = "https://graph.facebook.com/" + fbID + "/picture?type=large&redirect=true";

Just change the type attribute to anyone of enum{small, normal, album, large, square}.

Source : Documentation.

Upvotes: 4

Related Questions