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?
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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With