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.


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".


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


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