Get user image from Facebook Graph-API

前端 未结 7 619
长发绾君心
长发绾君心 2020-12-01 03:22

I want to show the users profile picture in a list view. When I try to call the graph-api from android to retrieve the image, I always get the following error.

<         


        
相关标签:
7条回答
  • 2020-12-01 03:30

    I know this question is kind of old, but there is another way to get User's picture nowadays easily.

    First, in the xml layout use:

    <com.facebook.widget.ProfilePictureView
            android:id="@+id/userImage"
            android:layout_width="69dp"
            android:layout_height="69dp"
            android:layout_gravity="center_horizontal" />
    

    Then in your fragment or activity, in the method onSessionStateChange:

    private void onSessionStateChange(Session session, SessionState state,
                Exception exception) {
            if (state.isOpened()) {
                Log.i(TAG, "Logged in...");
    
                // Request user data and show the results
                Request.newMeRequest(session, new Request.GraphUserCallback() {
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            //HERE: DISPLAY USER'S PICTURE
                            userPicture.setProfileId(user.getId());
                        }
                    }
                }).executeAsync();
    
            } else if (state.isClosed()) {
                Log.i(TAG, "Logged out...");
    
                userPicture.setProfileId(null);
            }
        }
    

    Hope this can help someone. I was facing the same problem and I get this post but it's 2011. Today (2013) the way of doing things has changed.

    0 讨论(0)
  • 2020-12-01 03:31
     ImageView user_picture;
     userpicture=(ImageView)findViewById(R.id.userpicture);
     URL img_value = null;
     img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=large");
     Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
     userpicture.setImageBitmap(mIcon1);
    

    Where ID is one your profile ID.

    For Further details check this Reference for Graph API

    ............................

    0 讨论(0)
  • 2020-12-01 03:31

    There is an easy way to get facebook user image for logged in user.

    To make the code work add this import statement:

    import com.facebook.Profile;
    

    See example below (in this example I use Picasso library to set image):

    Uri imageUri = Profile.getCurrentProfile().getProfilePictureUri(400, 400);
    Picasso.with(this).load(imageUri).into(imageView);
    

    Numbers 400 and 400 are image width and height respectively.

    0 讨论(0)
  • 2020-12-01 03:32

    Can be written in another way:

    ImageView user_picture;
             ImageView userpicture = (ImageView)findViewById(R.id.userpicture);
             URL img_value = null;
             try {
                img_value = new URL("http://graph.facebook.com/"+"100004545962286"+"/picture?type=large");
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             Bitmap mIcon1 = null;
            try {
                mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             userpicture.setImageBitmap(mIcon1);
    
    0 讨论(0)
  • 2020-12-01 03:37

    You can it do it by Using ProfilePictureView instead of an image view:

    <com.facebook.widget.ProfilePictureView
       android:id="@+id/friendProfilePicture"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:gravity="center_horizontal"
       android:padding="5sp"
       facebook:preset_size="small" />
    

    You can set the size to small/normal/large/custom.

    Then in your code, set the user facebook id like this:

    ProfilePictureView profilePictureView;
    profilePictureView = (ProfilePictureView) findViewById(R.id.friendProfilePicture);
    profilePictureView.setProfileId(userId);
    

    Hope this help.

    0 讨论(0)
  • 2020-12-01 03:39
    private String facebookUser;
    
    
    AccessToken token;
        token = AccessToken.getCurrentAccessToken();
    
        facebookUser = AccessToken.getCurrentAccessToken().getUserId();
        ProfilePictureView profilePictureView;
        profilePictureView = (ProfilePictureView) findViewById(R.id.facebookUser);
    

    profilePictureView.setProfileId(facebookUser);

    xml
    <com.facebook.login.widget.ProfilePictureView
     android:id="@+id/facebookUser"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentTop="true"
     android:layout_centerHorizontal="true"></com.facebook.login.widget.ProfilePictureView>
    

    hope it helps !

    0 讨论(0)
提交回复
热议问题