10

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 mFacebook = new Facebook(APP_ID);
// ... user logs in ...
//String jsonUser = mFacebook.request("me/picture"); // throws error
String jsonUser = mFacebook.request("me");
JSONObject obj = Util.parseJson(jsonUser);
String facebookId = obj.optString("id");
String name = obj.optString("name");

I also know that the I can access the profile picture with those links:

https://graph.facebook.com/<facebookId>/picture 
https://graph.facebook.com/<facebookId>/picture?type=large

I would love to use this code to geht the profile picture:

public static Drawable getPictureForFacebookId(String facebookId) {

    Drawable picture = null;
    InputStream inputStream = null;

    try {
        inputStream = new URL("https://graph.facebook.com/" + facebookId + "/picture").openStream();
    } catch (Exception e) {        
     e.printStackTrace();
     return null;

    }
    picture = Drawable.createFromStream(inputStream, "facebook-pictures");

    return picture;
}

But it just wont work. I always get the following error:

SSL handshake failure: Failure in SSL library, usually a protocol error

And I cant solve this issue. It seems to be rather complicated(look here or here). So what other options are there to get the picture of a facebook user that successfully logged into my application?

Community
  • 1
  • 1
Pascal Klein
  • 23,665
  • 24
  • 82
  • 119

7 Answers7

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

Chirag_CID
  • 2,224
  • 1
  • 24
  • 33
  • nice and straight way but i would say id even can be name of the user like "parag.chauhan" – MKJParekh Nov 04 '11 at 13:40
  • 1
    thts true..but this cant be use for each user, as thr could be so many user of same name... – Chirag_CID Nov 05 '11 at 07:31
  • 1
    Not working for higher version ...This works like charm in lower version but not in higher version. What should I do for that? – AndroidHacker Feb 18 '14 at 12:10
  • 1
    The current URL is a 302 redirect, get the 302 location and a second request to get the image. This helped me out [link](http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/) – Olavz Jun 20 '14 at 10:00
9

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.

Volo
  • 28,673
  • 12
  • 97
  • 125
Antonio
  • 3,128
  • 2
  • 22
  • 14
  • 1
    This code just couldn't work for you because it didn't compiled. `AsyncTask` takes 3 parameters and not 1 (so you can't use `AsyncTask`, but `AsyncTask`). Also it doesn't have `taskComplete` method, but have `onPostExecute` instead. I edited your code to make it compilable. – Volo Dec 05 '11 at 11:14
  • I use my own AsyncTask with only one parameter, so yes, it was working. Anyway, thanks for editing... – Antonio Dec 05 '11 at 17:11
5

You can request a direct URl which contains your Access token:

URL MyProfilePicURL = new URL("https://graph.facebook.com/me/picture?type=normal&method=GET&access_token="+ Access_token );

Then get a decoded BitMap and assign it to image view:

Bitmap MyprofPicBitMap = null;
try {
    MyprofPicBitMap = BitmapFactory.decodeStream(MyProfilePicURL.openConnection().getInputStream());
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

MyProfilePicImageView.setImageBitmap(mIcon1);
Mohamed Sayed
  • 59
  • 1
  • 2
4

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.

Robert Brooker
  • 2,148
  • 24
  • 22
2

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.

Vishal Pawale
  • 3,416
  • 3
  • 28
  • 32
1

Add one line of code and that will be resolved.

HttpURLConnection.setFollowRedirects(true);
Christian Specht
  • 35,843
  • 15
  • 128
  • 182
0

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