Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - get facebook profile picture

I don't know why, but I am always getting null when I try to get the profile picture of the user. Do I need to set some specific permissions to get access?

Below is my method:

public static Bitmap getFacebookProfilePicture(String userID) throws SocketException, SocketTimeoutException, MalformedURLException, IOException, Exception
{
   String imageURL;

   Bitmap bitmap = null;
   imageURL = "http://graph.facebook.com/"+userID+"/picture?type=large";
   InputStream in = (InputStream) new URL(imageURL).getContent();
   bitmap = BitmapFactory.decodeStream(in);

   return bitmap;
}

Bitmap bitmap = getFacebookProfilePicture(userId);

I am getting null. I don't know the reason why? Any help is appreciable.

like image 803
Zbarcea Christian Avatar asked Nov 08 '13 09:11

Zbarcea Christian


4 Answers

This should work:

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

    return bitmap;
}

Bitmap bitmap = getFacebookProfilePicture(userId);

Edit:

As suggested by @dvpublic in the comments, the problem of image not being downloaded is fixed using by "https" in favour of "http".

like image 99
Jeffrey Klardie Avatar answered Nov 04 '22 12:11

Jeffrey Klardie


use the facebook ProfilePictureView instead of an Imageview

<com.facebook.login.widget.ProfilePictureView
    android:id="@+id/friendProfilePicture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    facebook:preset_size="small"/>

After that you can set facebook id like this in code

ProfilePictureView profilePictureView;

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

profilePictureView.setProfileId(userId);

it works.. also You can set the size to small/normal/large/custom for ProfilePictureView

like image 28
Nikhil Borad Avatar answered Nov 04 '22 13:11

Nikhil Borad


Simply use Picasso. Add Picasso Library and then use this simple line code:

userpicture = (ImageView) row.findViewById(R.id.postuserid);

Picasso.with(context)
       .load("https://graph.facebook.com/" + userID+ "/picture?type=large")
       .into(userpicture);
like image 29
Jesmeen Hoque Avatar answered Nov 04 '22 12:11

Jesmeen Hoque


Best way to Get Profile Picture URL

int dimensionPixelSize = getResources().getDimensionPixelSize(com.facebook.R.dimen.com_facebook_profilepictureview_preset_size_large);
Uri profilePictureUri= Profile.getCurrentProfile().getProfilePictureUri(dimensionPixelSize , dimensionPixelSize);

or

Uri profilePictureUri = ImageRequest.getProfilePictureUri(Profile.getCurrentProfile().getId(), dimensionPixelSize , dimensionPixelSize );

Use Glide to show Image

Glide.with(this).load(profilePictureUri)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(profilePictureView);

No more hard-coded string

like image 18
OM PRAKASH SEERVI Avatar answered Nov 04 '22 13:11

OM PRAKASH SEERVI