Android - get facebook profile picture

Zbarcea Christian picture Zbarcea Christian · Nov 8, 2013 · Viewed 89.6k times · Source

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.

Answer

Jeffrey Klardie picture Jeffrey Klardie · Nov 8, 2013

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