获取用户档案图片Android facebook SDK

时间:2015-04-06 02:12:14

标签: android facebook facebook-android-sdk

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

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

enter image description here

这是我的代码:

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

几个月前,我可以显示个人资料图片有任何问题,但现在我得到了这个...任何想法?

6 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:6)

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

试试这个。

答案 2 :(得分:4)

选中answer

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

答案 3 :(得分:0)

这是正确的网址:

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

答案 4 :(得分:0)

使用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,请将以下行放入应用中dependencies文件的build.gradle部分。

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

答案 5 :(得分:-1)

以下是我在应用中的表现:

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