【问题标题】:Get user image from Facebook Graph-API从 Facebook Graph-API 获取用户图像
【发布时间】:2011-08-16 01:12:09
【问题描述】:

我想在列表视图中显示用户的个人资料图片。 当我尝试从 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)

这是我使用的代码:

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");

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

图片以哪种格式发送给我?带有图像参考 (url) 的 JSON?

【问题讨论】:

    标签: android facebook facebook-graph-api


    【解决方案1】:
     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);
    

    其中 ID 是您的个人资料 ID。

    更多详情请查看Reference for Graph API

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

    【讨论】:

    • 你好 Venkatesh,这正是我要找的。谢谢
    • @FelipeConde 您不需要任何访问令牌。您可以在任何浏览器上使用此 URL,并查看它适用于任何个人资料 ID
    • @IncrediApp 你是对的。使用“我”插入用户 ID 时,您只需要令牌。
    • @Venky 我收到下一个错误:“android.os.NetworkOnMainThreadException”。您是否将此代码放入 AsyncTask 中?谢谢
    • 是的,这段代码会阻塞主线程直到下载完成。真的很糟糕,特别是如果你一次说出 30 个这样的请求
    【解决方案2】:

    我知道这个问题有点老了,但是现在有另一种方法可以轻松获取用户的图片。

    一、在xml布局中使用:

    <com.facebook.widget.ProfilePictureView
            android:id="@+id/userImage"
            android:layout_width="69dp"
            android:layout_height="69dp"
            android:layout_gravity="center_horizontal" />
    

    然后在您的片段或活动中,在 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);
            }
        }
    

    希望这可以帮助某人。我遇到了同样的问题,我收到了这篇文章,但它是 2011 年。今天(2013 年)做事的方式发生了变化。

    【讨论】:

      【解决方案3】:

      您可以通过使用 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" />
      

      您可以将尺寸设置为小/正常/大/自定义。

      然后在您的代码中,像这样设置用户 facebook id:

      ProfilePictureView profilePictureView;
      profilePictureView = (ProfilePictureView) findViewById(R.id.friendProfilePicture);
      profilePictureView.setProfileId(userId);
      

      希望对您有所帮助。

      【讨论】:

      • 迄今为止最简单的解决方案,但我认为该软件包已移至 com.facebook.login.widget.ProfilePictureView
      【解决方案4】:

      有一种简单的方法可以获取登录用户的 facebook 用户图像。

      要使代码正常工作,请添加以下导入语句:

      import com.facebook.Profile;
      

      见下面的例子(在这个例子中我使用Picasso library来设置图像):

      Uri imageUri = Profile.getCurrentProfile().getProfilePictureUri(400, 400);
      Picasso.with(this).load(imageUri).into(imageView);
      

      数字 400 和 400 分别是图像的宽度和高度。

      【讨论】:

        【解决方案5】:

        可以写成另一种方式:

        ImageView user_picture;
                 ImageView userpicture = (ImageView)findViewById(R.id.userpicture);
                 URL img_value = null;
                 try {
                    img_value = new URL("http://graph.facebook.com/"+"100004545962286"+"/picture?type=large");
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 Bitmap mIcon1 = null;
                try {
                    mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 userpicture.setImageBitmap(mIcon1);
        

        【讨论】:

        • 好像url有重定向,图片会为空。
        【解决方案6】:

        使用以下代码获取带有Facebook Graph API 的用户个人资料图片。

             ImageView profileImage = (ImageView) findViewById(R.id.profileImage);
             Bundle params = new Bundle();
             params.putBoolean("redirect", false);
             params.putString("type", "large");
                  new GraphRequest(
                  AccessToken.getCurrentAccessToken(),
                  "me/picture",
                  params,
                  HttpMethod.GET,
                  new GraphRequest.Callback() {
                  public void onCompleted(GraphResponse response) {
                  try {
                    String picUrlString = (String) response.getJSONObject().getJSONObject("data").get("url");
                    Glide.with(getApplicationContext()).load(picUrlString).placeholder(R.drawable.ic_launcher).into(profileImage);
                  } catch (JSONException | IOException e) {
                    e.printStackTrace();
                }
            }
        }
        ).executeAsync();    
        

        更多信息请参考This

        简单获取用户图片

        String UserImageUrl="https://graph.facebook.com/" + facebookUser.getFacebookID() + "/picture?type=large"; 
        Glide.with(getApplicationContext()).load(UserImageUrl).placeholder(R.drawable.ic_launcher).into(profileImage); 
        

        【讨论】:

        • 这是比使用 httpclient 或 webclient 下载更好的获取用户图像的方法吗?哪种方式更好更快?
        【解决方案7】:
        private String facebookUser;
        
        
        AccessToken token;
            token = AccessToken.getCurrentAccessToken();
        
            facebookUser = AccessToken.getCurrentAccessToken().getUserId();
            ProfilePictureView profilePictureView;
            profilePictureView = (ProfilePictureView) findViewById(R.id.facebookUser);
        

        profilePictureView.setProfileId(facebookUser);

        xml
        <com.facebook.login.widget.ProfilePictureView
         android:id="@+id/facebookUser"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="true"
         android:layout_centerHorizontal="true"></com.facebook.login.widget.ProfilePictureView>
        

        希望对你有帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-12
          • 1970-01-01
          • 2014-11-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多