从Facebook Graph-API获取用户图片

myb*_*cks 20 android facebook facebook-graph-api

我想在列表视图中显示用户个人资料图片.当我尝试从android调用graph-api来检索图像时,我总是得到以下错误.

java.io.IOException: Hostname <fbcdn-profile-a.akamaihd.net> was not verified
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.getSecureSocket(HttpConnection.java:170)
    at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnection$HttpsEngine.connect(HttpsURLConnection.java:398)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.sendRequest(HttpURLConnection.java:1224)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.doRequestInternal(HttpURLConnection.java:1558)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.doRequest(HttpURLConnection.java:1551)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1052)
    at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnection.getInputStream(HttpsURLConnection.java:252)
    at com.facebook.android.Util.openUrl(Util.java:200)
    at com.facebook.android.Facebook.request(Facebook.java:559)
Run Code Online (Sandbox Code Playgroud)

这是我使用的代码:

private static void retrieveProfilePicture(String userId) throws MalformedURLException, IOException{
        facebook = FacebookHelper.getInstance();
        Bundle bundle = new Bundle();
        bundle.putString(Facebook.TOKEN, facebook.getAccessToken());
        Object picture = facebook.request("/"+userId+"/picture", bundle, "GET");
Run Code Online (Sandbox Code Playgroud)

当我在浏览器中执行相同的调用(https://graph.facebook.com//picture?access_token=)时,我会在这样的URL上返回图像 https://fbcdn-profile-a.akamaihd.net/...

以什么格式传送给我的图像?带有引用映像的JSON(url)?

Ven*_*nky 83

 ImageView user_picture;
 userpicture=(ImageView)findViewById(R.id.userpicture);
 URL img_value = null;
 img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=large");
 Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
 userpicture.setImageBitmap(mIcon1);
Run Code Online (Sandbox Code Playgroud)

ID是您的个人资料ID.

有关详细信息,请参阅此参考图表API

............................

  • 没有授权令牌? (4认同)
  • @FelipeConde您不需要任何访问令牌.您可以在任何浏览器上使用此URL,并查看它适用于任何配置文件ID (3认同)
  • @IncrediApp你是对的.在使用"me"用户ID时,您只需要令牌. (2认同)

kid*_*uxa 16

我知道这个问题有点陈旧,但现在还有另一种方法可以轻松获取用户的图片.

首先,在xml布局中使用:

<com.facebook.widget.ProfilePictureView
        android:id="@+id/userImage"
        android:layout_width="69dp"
        android:layout_height="69dp"
        android:layout_gravity="center_horizontal" />
Run Code Online (Sandbox Code Playgroud)

然后在你的片段或活动中,在onSessionStateChange方法中:

private void onSessionStateChange(Session session, SessionState state,
            Exception exception) {
        if (state.isOpened()) {
            Log.i(TAG, "Logged in...");

            // Request user data and show the results
            Request.newMeRequest(session, new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if (user != null) {
                        //HERE: DISPLAY USER'S PICTURE
                        userPicture.setProfileId(user.getId());
                    }
                }
            }).executeAsync();

        } else if (state.isClosed()) {
            Log.i(TAG, "Logged out...");

            userPicture.setProfileId(null);
        }
    }
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助别人.我遇到了同样的问题而且我得到了这个帖子,但它是2011年.今天(2013年),做事方式发生了变化.


Nik*_*rad 6

你可以通过使用ProfilePictureView而不是图像视图来做到这一点:

<com.facebook.widget.ProfilePictureView
   android:id="@+id/friendProfilePicture"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:gravity="center_horizontal"
   android:padding="5sp"
   facebook:preset_size="small" />
Run Code Online (Sandbox Code Playgroud)

您可以将大小设置为小/正常/大/自定义.

然后在你的代码中,设置用户的facebook id如下:

ProfilePictureView profilePictureView;
profilePictureView = (ProfilePictureView) findViewById(R.id.friendProfilePicture);
profilePictureView.setProfileId(userId);
Run Code Online (Sandbox Code Playgroud)

希望这有帮助.