如何在Android中获取Facebook个人资料图片

anu*_*m x 13 profile android facebook image

我在android中使用Facebook sdk 4.4.0,我希望使用图形请求获取用户的当前个人资料图片.怎么做?

我见过人们用

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

用于提取个人资料图片的API,但我无法想象如何从中提取个人资料图片.

Raj*_*esh 56

您需要调用GraphRequest API来获取用户的所有详细信息,其中API还提供当前个人资料图片的URL.

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();
Run Code Online (Sandbox Code Playgroud)

  • 哇!谢谢你,FabCoder!对于这几个词"picture.type(large)".我在facebook的文档中找不到这个机会,但我知道这是存在的!谢谢! (2认同)

Dmi*_*kov 16

从上一个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");
 }
Run Code Online (Sandbox Code Playgroud)