获取具有首选宽度和高度的 Facebook 个人资料图片,android?

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

我正在获取 Facebook 个人资料信息以及个人资料照片。

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();
}

我收到的回复是这样的

{"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":"wishygupta@yahoo.com","name":"Wishy Gupta"}

而且我从 URL 收到的个人资料图片太小了。如何增加它的宽度和高度??

你可以做到,

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

只需将 type 属性更改为 enum{small, normal, album, large, square} 中的任何一个即可。

来源:Documentation.

使用这种类型url来获得大图

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

所以你的 url 将是 http://graph.facebook.com/10153520390508958/picture?type=large 它会将用户重定向到

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

但会得到尺寸为 200 x 200 的更大图片

这将是获得首选尺寸的 API 调用:

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

(或使用 redirect=false 不被重定向)

类型参数是另一种方式,但不是那么具体。虽然请记住,您不会总是得到特定尺寸的图片,但它更像是 "minimum width/height".

不要声明两个对象来解析 url 对象,只需使用下面的代码行:

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

此处 profilePicUrl,包含已解析的 url,类似于:

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