【问题标题】:How to get Facebook profile image in Android如何在 Android 中获取 Facebook 个人资料图片
【发布时间】:2015-11-25 11:11:26
【问题描述】:

我在 android 中使用 Facebook sdk 4.4.0,我想使用图形请求获取用户的当前个人资料图片。该怎么做?

我看到人们使用

https://graph.facebook.com/me/picture?access_token=ACCESS_TOKEN

提取头像的API,但我不知道如何从中提取头像。

【问题讨论】:

标签: android facebook image profile


【解决方案1】:

您需要调用 GraphRequest API 来获取用户的所有详细信息,其中 API 还提供当前头像的 URL。

Bundle params = new Bundle();
params.putString("fields", "id,email,gender,cover,picture.type(large)");
new GraphRequest(AccessToken.getCurrentAccessToken(), "me", params, HttpMethod.GET,
        new GraphRequest.Callback() {
            @Override
            public void onCompleted(GraphResponse response) {
                if (response != null) {
                    try {
                        JSONObject data = response.getJSONObject();
                        if (data.has("picture")) {
                            String profilePicUrl = data.getJSONObject("picture").getJSONObject("data").getString("url");
                            Bitmap profilePic= BitmapFactory.decodeStream(profilePicUrl .openConnection().getInputStream());
                            mImageView.setBitmap(profilePic);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
}).executeAsync();

【讨论】:

  • 这会引发错误,因为 decodeStream 正在寻找 URL 而不是字符串 - 我做错了什么吗?
  • 你试过BitmapFactory.decodeStream(url.openConnection().getInputStream()); ??
  • 我在那里使用了您的确切文本,将 getFacebookProfilePicture() 放在 onCreate 之外,它显示 .openConnection() 未解决。不知道我做错了什么
  • 漂亮!它现在完美运行。而不是token,我使用AccessToken.getCurrentAccessToken()(根据facebook API)。效果很好!感谢您更新旧答案!
  • 哇!谢谢你,FabCoder!对于这几个词“picture.type(large)”。我在 facebook 的文档中找不到这个机会,但我知道这是存在的!谢谢!
【解决方案2】:

从上一个 sdk 4.5.0

 String url;
 Bundle parametersPicture = new Bundle();
 parametersPicture.putString("fields", "picture.width(150).height(150)");

 GraphResponse lResponsePicture = new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/",
                        parametersPicture, null).executeAndWait();
 if (lResponsePicture != null && lResponsePicture.getError() == null &&
                            lResponsePicture.getJSONObject() != null) {
     url = lResponsePicture.getJSONObject().getJSONObject("picture")
                                .getJSONObject("data").getString("url");
 }

【讨论】:

    【解决方案3】:

    从 Facebook 获取图片

    String image_url = "http://graph.facebook.com/" + Profile.getCurrentProfile().getId() + "/picture?type=large";
    Glide.with(activity)
         .load(image_url)
         .into(imageView);
    

    依赖

    implementation 'com.github.bumptech.glide:glide:4.1.1'
    

    【讨论】:

      【解决方案4】:

      您可以通过 2 种不同的方式获得它。

      方式1:集成图形api支持 https://developers.facebook.com/docs/graph-api/reference/user/picture/

      方式2:通过获取调用 http://graph.facebook.com/{facebook-Id}/picture?width=x&height=y

      其中 x 和 y 可以是任何整数,例如100

      【讨论】:

        【解决方案5】:

        如果您想要真正的大图,则必须指定。至少一种尺寸的图片 - 例如。

        String profileImg = "https://graph.facebook.com/" + loginResult.getAccessToken().getUserId() + "/picture?type=large&width=1080";
        

        此外,您可以指定两种尺寸(添加 &height=some_val),但随后 facebook 会裁剪此个人资料图片。

        【讨论】:

          【解决方案6】:
          protected void rajendra(LoginButton login_button) {
              login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                  @Override
                  public void onSuccess(LoginResult login_result) {
                      GraphRequest request = GraphRequest.newMeRequest(
                              login_result.getAccessToken(),
                              new GraphRequest.GraphJSONObjectCallback() {
                                  @Override
                                  public void onCompleted(JSONObject object,GraphResponse response) {
                                 
                                      response.getError();
          
                                      try {
                                          if (android.os.Build.VERSION.SDK_INT > 9) {
                                              StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                                              StrictMode.setThreadPolicy(policy);
                                              String profilePicUrl = object.getJSONObject("picture").getJSONObject("data").getString("url");
                                         
                                              URL fb_url = new URL(profilePicUrl);//small | noraml | large
                                              HttpsURLConnection conn1 = (HttpsURLConnection) fb_url.openConnection();
                                              HttpsURLConnection.setFollowRedirects(true);
                                              conn1.setInstanceFollowRedirects(true);
                                              Bitmap fb_img = BitmapFactory.decodeStream(conn1.getInputStream());
                                              image.setImageBitmap(fb_img);
                                          }
                                      }catch (Exception ex) {
                                          ex.printStackTrace();
                                      }
                                  }
                              });
                      Bundle parameters = new Bundle();
                      parameters.putString("fields", "id,picture");
                      request.setParameters(parameters);
                      request.executeAsync();
                  }
           }
          

          【讨论】:

            【解决方案7】:
            try {
            String fbId="970463683015249";
            URL fb_url = new URL("http://graph.facebook.com/"+fbId+"/picture?type=small");//small | noraml | large
            HttpsURLConnection conn1 = (HttpsURLConnection) fb_url.openConnection();
            HttpsURLConnection.setFollowRedirects(true);
            conn1.setInstanceFollowRedirects(true);
            Bitmap fb_img = BitmapFactory.decodeStream(conn1.getInputStream());
            }catch (Exception ex) {
              ex.printStackTrace();
              }
            

            【讨论】:

              【解决方案8】:

              只需调用此 URL:

              graph.facebook.com/<facebook_user_id>/picture?type=large
              

              类型可以是大、普通或小。

              另一种方法是使用ProfilePictureView

              <com.facebook.login.widget.ProfilePictureView
                  android:id="@+id/image"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerInParent="true"
                  facebook:preset_size="small"/>
              

              之后,您可以在代码中像这样设置 facebook id

              profilePictureView.setProfileId(facebookUserId);
              

              【讨论】:

              • 感谢这项工作。您能告诉我如何将此链接与图形请求 api 一起使用吗?一个小代码sn-p
              【解决方案9】:

              脸书图片

              https://graph.facebook.com/" +facebookUid + "/picture?height=9999&redirect=0"

              谷歌图片

              String googleEmailName = googleEmail.substring(0,googleEmailName.indexOf("@"))

              http://picasaweb.google.com/data/entry/api/user/"+googleEmailName+"?alt=json

              【讨论】:

              • 你应该写一些东西来解释你在做什么。
              • 请添加一些描述。
              猜你喜欢
              • 1970-01-01
              • 2013-11-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-25
              • 1970-01-01
              • 2019-11-04
              相关资源
              最近更新 更多