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

15

我正在使用Android中的Facebook SDK 4.4.0,并希望使用图形请求获取用户当前个人资料图片。如何做到这一点?

我已经看到人们使用

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

提取个人资料图片的API,但我无法弄清如何从中提取个人资料图片。


GraphRequest获取图片时,您可以传递某些参数。所有这些参数都在https://developers.facebook.com/docs/graph-api/reference/user/picture/上有记录。 - ADM
9个回答

63
您需要调用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();

这对我来说会抛出错误,因为decodeStream正在寻找URL而不是字符串 - 我做错了什么吗? - ntgCleaner
你尝试过使用 BitmapFactory.decodeStream(url.openConnection().getInputStream()); 吗? - Rajesh Jadav
1
我已经使用了你的确切文本,将 getFacebookProfilePicture() 放在 onCreate 之外,但它显示 .openConnection() 未解决。不确定我做错了什么。 - ntgCleaner
1
太棒了!现在它完美地工作了。我使用了 AccessToken.getCurrentAccessToken()(根据 Facebook API),而不是 token。它非常好用!谢谢你更新了一个旧答案! - ntgCleaner
4
哇!谢谢你,FabCoder!因为这几个词“picture.type(large)”。我在Facebook的文档中找不到这个机会,但我知道它存在!谢谢! - Sirelon
显示剩余2条评论

17

从最新的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");
 }

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

依赖关系
implementation 'com.github.bumptech.glide:glide:4.1.1'

3
protected void rajendra(LoginButton login_button) {
    login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult login_result) {
            GraphRequest request = GraphRequest.newMeRequest(
                    login_result.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object,GraphResponse response) {
                       
                            response.getError();

                            try {
                                if (android.os.Build.VERSION.SDK_INT > 9) {
                                    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                                    StrictMode.setThreadPolicy(policy);
                                    String profilePicUrl = object.getJSONObject("picture").getJSONObject("data").getString("url");
                               
                                    URL fb_url = new URL(profilePicUrl);//small | noraml | large
                                    HttpsURLConnection conn1 = (HttpsURLConnection) fb_url.openConnection();
                                    HttpsURLConnection.setFollowRedirects(true);
                                    conn1.setInstanceFollowRedirects(true);
                                    Bitmap fb_img = BitmapFactory.decodeStream(conn1.getInputStream());
                                    image.setImageBitmap(fb_img);
                                }
                            }catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,picture");
            request.setParameters(parameters);
            request.executeAsync();
        }
 }

3

3

如果你想要一个真正的大图片,你至少需要规定一张图片的尺寸 - 比如说。

String profileImg = "https://graph.facebook.com/" + loginResult.getAccessToken().getUserId() + "/picture?type=large&width=1080";

此外,您可以同时指定两个尺寸(添加 &height=some_val),但是Facebook会裁剪此个人资料图片。

2
try {
String fbId="970463683015249";
URL fb_url = new URL("http://graph.facebook.com/"+fbId+"/picture?type=small");//small | noraml | large
HttpsURLConnection conn1 = (HttpsURLConnection) fb_url.openConnection();
HttpsURLConnection.setFollowRedirects(true);
conn1.setInstanceFollowRedirects(true);
Bitmap fb_img = BitmapFactory.decodeStream(conn1.getInputStream());
}catch (Exception ex) {
  ex.printStackTrace();
  }

0

1
你应该写一些内容来解释你正在做什么。 - trungduc
please add some description. - Pranjal Bikash Das

0

只需调用此URL:

graph.facebook.com/<facebook_user_id>/picture?type=large

类型可以是大、中或小。

另一种方法是使用ProfilePictureView

<com.facebook.login.widget.ProfilePictureView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    facebook:preset_size="small"/>

之后,您可以在代码中像这样设置Facebook ID

profilePictureView.setProfileId(facebookUserId);

谢谢,这个可行。你能告诉我如何使用这个链接与图形请求API吗?一个小代码片段。 - anupam x

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接