【问题标题】:Get user profile image Android facebook SDK获取用户头像 Android facebook SDK
【发布时间】:2017-07-01 23:07:04
【问题描述】:

如何在 Android 上显示 fecebook 用户个人资料图片?

我一直在网上搜索,答案似乎是将https 添加到URL,但我已经设置了它并且我无法获取图像,而不是那个,我得到一个审讯标记:

这是我的代码:

    URL imageURL = null;
    try {
        imageURL = new URL("https://graph.facebook.com/" + userID + "/picture?width=" + width +"&height=" + width);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = null;
    try {
        bitmap = getCroppedBitmap(BitmapFactory.decodeStream(imageURL.openConnection().getInputStream()));
    } catch (IOException e) {
        e.printStackTrace();
    }

几个月前,我可以显示有任何问题的个人资料图片,但现在我明白了……有什么想法吗?

【问题讨论】:

    标签: android facebook facebook-android-sdk


    【解决方案1】:

    对于 facebook 的新 SDK,试试这个:

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                Log.v("LoginActivity", response.toString());
    
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,picture");
                request.setParameters(parameters);
                request.executeAsync();
            }
    
            @Override
            public void onCancel() {
            }
    
            @Override
            public void onError(FacebookException e) {
                e.printStackTrace();
            }
        });
    

    【讨论】:

      【解决方案2】:
      loginButton = (LoginButton) findViewById(R.id.login_button);
          loginButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday, user_friends"));
          callbackManager = CallbackManager.Factory.create();
          loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
              @Override
              public void onSuccess(LoginResult loginResult) {
                  Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
                  AccessToken accessToken = loginResult.getAccessToken();
                  GraphRequest request = GraphRequest.newMeRequest(
                          accessToken,
                          new GraphRequest.GraphJSONObjectCallback() {
                              @Override
                              public void onCompleted(
                                      JSONObject object,
                                      GraphResponse response) {
                                  try {
      
                                      String userID = (String) object.get("id");
                                      String userName = (String) object.get("name");
      
                                      Picasso.with(MainActivity.this).load("https://graph.facebook.com/" + userID+ "/picture?type=large").into(imageView);
                                      //Bitmap b = (Bitmap) object.get("picture");
                                      tv = (TextView) findViewById(R.id.textView2);
                                      tv.setText("Hello" + " " + userName);
                                      Log.d("o/p", "name");
                                  } catch (JSONException e) {
                                      e.printStackTrace();
                                  }
      
                              }
                          });
                  Bundle parameters = new Bundle();
                  parameters.putString("fields", "id,name,link,birthday,picture");
                  request.setParameters(parameters);
                  request.executeAsync();
              }
      

      试试这个。

      【讨论】:

      • 你还需要下载一个毕加索图书馆,可以在here找到它
      • 有效,但如果您想稍后像使用名称和 ID 一样从 JSON 中获取它,您实际上还需要在参数对象中指定“电子邮件”字段
      【解决方案3】:

      检查这个answer

      imageURL = new URL("https://graph.facebook.com/" + userID + "/picture?width=" + width +"&height=" + height);
      InputStream inputStream = (InputStream) imageURL.getContent();
      bitmap = getCroppedBitmap(BitmapFactory.decodeStream(inputStream));
      

      【讨论】:

      • 我收到下一个错误:“android.os.NetworkOnMainThreadException”。您是否将此代码放入 AsyncTask 中?谢谢
      【解决方案4】:

      这是正确的网址:

      URL MyProfilePicURL = new URL("https://graph.facebook.com/me/picture?type=normal&method=GET&access_token="+ Access_token );
      

      【讨论】:

        【解决方案5】:

        使用 ImageView 小部件显示个人资料图片。

        imageURL = new URL("https://graph.facebook.com/" + userID + "/picture?width=" + width +"&height=" + height);
        Picasso.with(this.context).load(imageUrl.toString()).fit().into(imageView);
        

        要导入毕加索,请将下面的行放在应用程序中build.gradle 文件的dependencies 部分中。

        compile 'com.squareup.picasso:picasso:2.5.0'
        

        【讨论】:

          【解决方案6】:

          这是我在应用中的操作方式:

          Profile currentProfile = Profile.getCurrentProfile();
          Uri profilePictureUri = currentProfile.getProfilePictureUri(width, height);
          // download the image or set it to a view
          downloadUserProfilePicture(profilePictureUri.toString());
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多