Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 670
Pascal Klein Avatar asked Dec 16 '10 13:12

Pascal Klein


2 Answers

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

like image 111
Chirag_CID Avatar answered Oct 20 '22 00:10

Chirag_CID


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.

like image 22
Antonio Avatar answered Oct 19 '22 23:10

Antonio