2012-11-28 160 views
15

我在使用Facebook SDK 3.0 for Android时遇到以下问题。 我想要得到我的(和我的朋友)资料照片上没有使用他们的ProfilePictureView部件,所以如果我使用图形浏览器我看到JSON响应是:使用Facebook SDK 3.0获取个人资料图片Android版

{ 
    "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

+0

@Geek答案是将重定向设置为false按照http://stackoverflow.com/questions/25055159/android-facebook-sdk-decoding-pictures-from-me-picture-graph-call – kha

回答

1

您可能需要启用“图片作为字典”上的开发者控制台您的应用程序的高级设置在https://developers.facebook.com/apps

+0

此设置启用,实际上我获得了一个Json响应,而不仅仅是url作为字符串。 –

10

一个更简单的方法是将执行GET请求graph.facebook.com/USER_ID/picture因此您不必首先请求图片的网址,然后执行另一个GET请求从给定网址下载图片。

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

+0

它的工作原理!谢谢!! –

+0

如果我真的想要获取网址,该怎么办? “图片为字典”设置似乎没有帮助。 –

+3

url始终是http://graph.facebook.com/YOUR_USER_ID/picture –

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); – Proverbio

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; 
} 
2

您可以通过向Graph API请求获取配置文件图片。您需要传递 accessToken,用户标识并将重定向设置为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(); 
0

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

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