1

When I do a request for a profile picture, mine in this case I receive some kind of encoded string in my HttpResponseHandler. The code below is the request for my profile picture.

private static AsyncHttpClient client = new AsyncHttpClient();

client.get("http://graph.facebook.com/1206433/picture",  fbPictureHandler);

The code below is my handler to retrieve a response. I get the response as a string, but I am not sure what to do with this response object. I have tried converting to a byte array and writing to "file.jpg" this hasnt worked. My main question is what do I do with this response object?

private static AsyncHttpResponseHandler fbPictureHandler = new AsyncHttpResponseHandler ()        
{
@Override
public void onStart() {
Log.d(TAG,"started picture handler");
}
@Override
public void onSuccess(String response) {
        //Not sure what to do here, have been unable to do anything with this Byte   //array
    byte[] imageBackground = response.getBytes();

    }
    @Override
    public void onFailure(Throwable error) {
        Log.d(TAG, "unable to retrieve picture");
        error.printStackTrace();
    }
    @Override
    public void onFinish() {
        Log.d(TAG,"Finished picture handler");
    }
};

This is the PrintString of the response object

11-29 19:42:12.640: D/Yatter Facebook(3551): ÿØÿà��JFIF������������ÿþ��;CREATOR: gd-jpeg     v1.0 (using IJG JPEG v62), quality = 95

ANy help is greatly appreciated and hopefully this can help others.

Thank you,

Danuofr
  • 1,661
  • 19
  • 25

2 Answers2

2

Use the following request instead of the one that you are issuing

http://graph.facebook.com/1206433?fields=picture

This will return a JSON string to you in the following format which contains the original path to the profile image.

{
   "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/260615_1206433_140418666_q.jpg"
}

Parse this string to get the path of "picture" and use it in your code to get the picture.


Here is a sample request example

NOTE : http://profile.ak.fbcdn.net/hprofile-ak-snc4/260615_1206433_140418666_q.jpg is obtained by parsing the JSON string in the first step.

WebRequest request = WebRequest.Create("http://profile.ak.fbcdn.net/hprofile-ak-snc4/260615_1206433_140418666_q.jpg");
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
pictureBox1.Image = Image.FromStream(stream);

This will load the image to a picture box in a windows form application.

If you need anymore help let me know.

Robin
  • 2,339
  • 1
  • 15
  • 11
  • Great this actually helps, now I know how to get the actual redirect URL using a JSONObject, with the picture field. Just to let you know though I am using this code to retrieve the bitmap `bitmap = BitmapFactory.decodeStream((InputStream) new URL( "http://graph.facebook.com/"+facebookId+"/picture?type=medium").getContent());` – Danuofr Dec 01 '11 at 00:08
  • I am retreiving each image using a AsyncTask by the way, thanks alot Robin. – Danuofr Dec 01 '11 at 00:15
0

you can use ?redirect=false follow '/picture' for get direct link

http://graph.facebook.com/+facebookid+/picture?redirect=false

and response contain url static link (json format)

{"data":{
    "url":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-frc1\/t1.0-1\/c126.33.409.409\/s50x50\/551571_4079894629426_190963543_n.jpg","is_silhouette":false}
}