Android and Facebook: How to get picture of logged in User

前端 未结 7 1035
攒了一身酷
攒了一身酷 2020-12-08 06:30

I use the official Facebook SDK in my Android Application. After the user logs in, I can get the uid and the name of the facebook user like so:

Facebook mFac         


        
相关标签:
7条回答
  • 2020-12-08 06:40

    request("me/picture") throws an error because the server returns a 302 (redirect to the image url) and the facebook sdk does not handle this.

    0 讨论(0)
  • 2020-12-08 06:45

    Add one line of code and that will be resolved.

    HttpURLConnection.setFollowRedirects(true);
    
    0 讨论(0)
  • 2020-12-08 06:47

    I also had that problem some time ago. What I did was download the picture using an async task, and then set an ImageView with the image just downloaded. I will paste the code snippet:

    ImageView fbUserAvatar = (ImageView) findViewById(R.id.fb_user_avatar);
    
    private synchronized void downloadAvatar() {
        AsyncTask<Void, Void, Bitmap> task = new AsyncTask<Void, Void, Bitmap>() {
    
            @Override
            public Bitmap doInBackground(Void... params) {
                URL fbAvatarUrl = null;
                Bitmap fbAvatarBitmap = null;
                try {
                    fbAvatarUrl = new URL("http://graph.facebook.com/"+USER_ID+"/picture");
                    fbAvatarBitmap = BitmapFactory.decodeStream(fbAvatarUrl.openConnection().getInputStream());
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return fbAvatarBitmap;
            }
    
            @Override
            protected void onPostExecute(Bitmap result) {
                fbUserAvatar.setImageBitmap(result);
            }
    
        };
        task.execute();
    }
    

    This code works for me. I hope it works for you too.

    0 讨论(0)
  • 2020-12-08 06:47

    Use this, (usuario is a GraphUser):

    ProfilePictureView p;
    p = (ProfilePictureView) rootView.findViewById(R.id.fotoPerfil);
    p.setProfileId(usuario.getId());
    

    and xml markup:

    <com.facebook.widget.ProfilePictureView
        android:id="@+id/profilePicture"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:gravity="center_horizontal"
        android:layout_marginBottom="10dp"
        facebook:preset_size="normal"/>
    
    0 讨论(0)
  • 2020-12-08 06:54

    For displaying profile pic in your app, use ProfilePictureView from Facebook SDK.

    Refer This

    Just call setProfileId(String profileId) on it.

    It will take care of displaying the image.

    0 讨论(0)
  • 2020-12-08 06:56
    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 ur profile ID...

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