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

时间:2012-11-28 17:28:23

标签: android facebook facebook-graph-api facebook-android-sdk

我关注Facebook SDK 3.0 for Android的问题。 我想在不使用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

6 个答案:

答案 0 :(得分:10)

更简单的方法是向GET执行graph.facebook.com/USER_ID/picture请求,这样您就不必首先请求图片的网址,然后执行另一个GET请求下载来自给定网址的图片。

不要使用Request.executeGraphPathRequestAsync,只需对上面的网址执行正常的GET请求,例如http://graph.facebook.com/4/picture

答案 1 :(得分: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;
}

答案 2 :(得分:2)

您可以通过请求图谱API来获取个人资料照片。你需要通过 accessToken,用户ID并设置重定向false。然后Graph API返回JSON,您可以从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();

答案 3 :(得分:1)

你可能需要启用" Picture As Dictionary"关于应用程序在https://developers.facebook.com/apps

的开发者控制台中的高级设置

答案 4 :(得分:1)

据我所知,使用脸谱图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;
}

答案 5 :(得分:0)

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

Bundle params = new Bundle();
params.putBoolean("redirect", false);
相关问题