从 Facebook 获取个人头像并在 ImageView 中设置

4

我获取了像用户ID、电子邮件、姓名等详细信息,但我想在我的图像视图中设置用户个人资料图片。
如何完成这个任务?

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

    String id;
    String name;
    String email;
    String gender;

    @Override
    public void onSuccess(LoginResult loginResult) {

        System.out.println("onSuccess");
        GraphRequest request = GraphRequest.newMeRequest
                (loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        // Application code
                        Log.v("LoginActivity", response.toString());
                        //System.out.println("Check: " + response.toString());
                        try {
                            id = object.getString("id");
                            // picture= object.getJSONObject("picture").getJSONObject("data").getString("url");
                            name = object.getString("name");
                            email = object.getString("email");
                            gender = object.getString("gender");
                            // birthday = object.getString("birthday");
                            // String location = object.getString("user_location");

                            Log.v("id", id);
                            Log.v("name", name);
                            Log.v("email", email);
                            Log.v("gender", gender);

                            SharedPreferences.Editor e = mSharedPreferences.edit();
                            e.putBoolean(PREF_KEY_FACEBOOK_LOGIN, true);
                            e.putString("id", id);
                            e.putString("name", name);
                            e.putString("email", email);
                            e.putString("gender", gender);
                            e.commit();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    // Toast.makeText(getApplicationContext(), id + "\n"  + name + 
                    // "\n" + email + "\n" + gender, Toast.LENGTH_LONG).show();
                }


    });

        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,email,gender, birthday");
        request.setParameters(parameters);
        request.executeAsync();
}

我正在使用v2.3版本,登录按钮是Facebook库的预定义按钮,请帮助或指导我,谢谢。


请参考以下帖子:https://dev59.com/2nA75IYBdhLWcg3w8-HZ,了解如何使用Facebook Graph API显示用户的个人资料图片。 - YesR
2个回答

4

最终我得到了答案,我使用了这段代码

       public static Bitmap getFacebookProfilePicture(String userID) throws IOException {
                URL imageURL = new URL("https://graph.facebook.com/" + userID + "/picture?type=large");
                Bitmap bitmap = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());

                return bitmap;
            }

   // on createView method 

            try {
                Bitmap mBitmap = getFacebookProfilePicture(id);
                imageView.setImageBitmap(mBitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }

2

只需使用 Facebook ID 即可获取用户的个人资料图片。

试试这个...

String fbImg = "http://graph.facebook.com/"+retrived_user_id+"/picture";

现在你可以使用“fbImg”字符串来加载个人资料图片。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接