获取用户个人资料图片 Android facebook SDK

Get user profile image Android facebook SDK

如何在 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();
    }

几个月前我可以毫无问题地显示个人资料图片,但现在我知道了...有什么想法吗?

这里是正确的URL:

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

勾选这个answer

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

对于 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();
        }
    });
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();
        }

试试这个。

以下是我在我的应用程序中的做法:

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

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

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

要导入 Picasso,请将以下行放入您应用中 build.gradle 文件的 dependencies 部分。

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