使用Facebook SDK 3.0 for Android检索个人资料图片

15

我在使用Android的Facebook SDK 3.0时遇到了问题。我想获取我的(和我的朋友们的)个人资料图片,而不使用他们的ProfilePictureView小部件。如果我使用Graph Explorer,我看到Json响应如下:

{
   "data": {
         "url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/372127_1377011138_1538716206_q.jpg", 
         "is_silhouette": false
    }
}

我需要那个"url"路径来下载并显示图片,但使用以下代码:

Request.executeGraphPathRequestAsync(Session.getActiveSession(), 
                                     "me/picture", 
                                      new Request.Callback() {
        @Override
        public void onCompleted(Response response) {
            GraphObject go = response.getGraphObject();
            Log.i("APP_NAME", go.toString());           
        }
});

我得到了这个:

GraphObject{graphObjectClass=GraphObject, 
    state={"FACEBOOK_NON_JSON_RESULT":"����\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u0000\u0001\u0000\u0001\u0000\u0000��\u0000"}}

有人可以帮助我吗? 谢谢

Cromir


1
我有同样的问题。你是怎么解决的? - Geek
@Geek,答案是根据https://dev59.com/d18f5IYBdhLWcg3wFfT_的建议设置重定向为false。 - kha
6个回答

10
更简单的方法是执行一个GET请求到graph.facebook.com/USER_ID/picture,这样你就不需要先请求图片的URL,然后再执行另一个GET请求从给定的URL下载图片。

而不是使用Request.executeGraphPathRequestAsync,只需对上面的URL进行普通的GET请求,例如http://graph.facebook.com/4/picture


如果我真的想获取URL怎么办?“图片作为字典”设置似乎没有帮助。 - kar
当您获取GraphObject时,如何管理转换以在ImageView中使用图片? - 5agado
1
我的方法不使用GraphObject,你只需要通过HTTP下载图片并加载它(参见https://dev59.com/h3E95IYBdhLWcg3wEpvo#2472175)。否则,如果你正在使用Android SDK来获取GraphObject,请使用我们提供的ProfilePictureView来设置图片https://developers.facebook.com/docs/reference/android/3.0/ProfilePictureView/。 - Jesse Chen

9
You can retreive user information for executeMeRequest in facebook 3.0 sdk.

    public void executeMeRequest(Session session) {

        Bundle bundle = new Bundle();
        bundle.putString("fields", "picture");
        final Request request = new Request(session, "me", bundle,
                HttpMethod.GET, new Request.Callback() {

            @Override
            public void onCompleted(Response response) {
                GraphObject graphObject = response.getGraphObject();
                if(graphObject != null) {
                    try {
                        JSONObject jsonObject = graphObject.getInnerJSONObject();
                        JSONObject obj = jsonObject.getJSONObject("picture").getJSONObject("data");
                        final String url = obj.getString("url");
                            new Thread(new Runnable() {

                                @Override
                                public void run() {

                                    final Bitmap bitmap = BitmapFactory.decodeStream(HttpRequest(url);
                                    runOnUiThread(new Runnable() {

                                        @Override
                                        public void run() {
                                            imageView.setImageBitmap(bitmap);
                                        }
                                    });
                                }
                            }).start();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        Request.executeBatchAsync(request);
    }

public static InputStream HttpRequest(String strUrl) {

    HttpResponse responce = null;
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(strUrl));
        responce = httpClient.execute(request);
        HttpEntity entity = responce.getEntity();
        return entity.getContent();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    return null;
}

1
这个方法非常有效。我只是更喜欢不使用HttpRequest方法;相反,我使用了InputStream in = new java.net.URL(url).openStream();和final Bitmap bitmap = BitmapFactory.decodeStream( in );。 - Juampa

2

您可以通过向Graph API发出请求来获取个人资料图片。您需要传递accessToken、用户ID,并将重定向设置为false。然后,Graph API将返回JSON格式的数据,从中您可以获取url字段,该字段是用户个人资料的网址。

  GraphRequest request = new GraphRequest(accessToken, "/" + userID + "/picture",null,HttpMethod.GET, new GraphRequest.Callback() {
                @Override
                public void onCompleted(GraphResponse response) {
                    Log.d("Photo Profile", response.getJSONObject().toString());
                    JSONObject jsonObject = response.getJSONObject();
                    try {

                        JSONObject data = jsonObject.getJSONObject("data");
                        String url = data.getString("url");
                        Picasso.with(getApplicationContext()).load(url).into(ivProfilePicture);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            Bundle parameters =new Bundle();
            parameters.putString("type","large");
            parameters.putBoolean("redirect",false);
            request.setParameters(parameters);
            request.executeAsync();

1

这个设置已启用,事实上我获得了一个Json响应而不仅仅是URL字符串。 - Giulio Bider

1
据我所知,使用 Facebook 图形 API 无法直接获取图像,因为他们的 API 只接受 JSON 响应。他们允许请求产生非 JSON 响应似乎很奇怪,尤其是在官方文档中放置该请求更加奇怪。(https://developers.facebook.com/docs/graph-api/reference/user/picture/)。
我正在使用内置的 Android DefaultHttpClient 库。
private Bitmap downloadImage(url) {
    Bitmap image = null;
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(imageUrl);
    try {
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();

        int imageLength = (int)(entity.getContentLength());

        InputStream is = entity.getContent();

        byte[] imageBlob = new byte[imageLength];

        int bytesRead = 0;

        // Pull the image's byte array

        while (bytesRead < imageLength) {
            int n = is.read(imageBlob, bytesRead, imageLength - bytesRead);
            bytesRead= n;
        }

        image = BitmapFactory.decodeByteArray(imageBlob, 0, imageBlob.length);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return image;
}

0

我得到了答案...这是由于重定向。因此,传递params而不是null

Bundle params = new Bundle();
params.putBoolean("redirect", false);

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