15

I've following problem with Facebook SDK 3.0 for Android. I wanna get my (and my friends) profile picture without using their ProfilePictureView widget, so if I use Graph Explorer I see that Json response is:

{
   "data": {
         "url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/372127_1377011138_1538716206_q.jpg", 
         "is_silhouette": false
    }
}

I need that "url" path to download and show the picture, but with the following code:

Request.executeGraphPathRequestAsync(Session.getActiveSession(), 
                                     "me/picture", 
                                      new Request.Callback() {
        @Override
        public void onCompleted(Response response) {
            GraphObject go = response.getGraphObject();
            Log.i("APP_NAME", go.toString());           
        }
});

I obtain this:

GraphObject{graphObjectClass=GraphObject, 
    state={"FACEBOOK_NON_JSON_RESULT":"����\u0000\u0010JFIF\u0000\u0001\u0001\u0000\u0000\u0001\u0000\u0001\u0000\u0000��\u0000"}}

Someone can help me please? Thanks

Cromir

Hitesh
  • 3,449
  • 8
  • 39
  • 57
Giulio Bider
  • 1,386
  • 2
  • 10
  • 20
  • 1
    I have same problem. How did you solve it? – Geek Oct 11 '13 at 06:06
  • @Geek the answer is to set the redirect to false as per http://stackoverflow.com/questions/25055159/android-facebook-sdk-decoding-pictures-from-me-picture-graph-call – kha Aug 25 '15 at 18:35

6 Answers6

10

An easier way would be to execute a GET request to graph.facebook.com/USER_ID/picture so you don't have to first request an URL to the picture, and then execute another GET request to download the picture from the given URL.

Instead of using Request.executeGraphPathRequestAsync, just do a normal GET request to the URL above, e.g. http://graph.facebook.com/4/picture.

Jesse Chen
  • 4,928
  • 1
  • 20
  • 20
  • What if I actually want to get the URL? The "Picture as Dictionary" setting seems to not help. – kar Jan 22 '13 at 10:00
  • When you get the `GraphObject` how do you manage the conversion for using the picture in an `ImageView`? – 5agado Apr 26 '13 at 10:05
  • 1
    my method above doesn't use a GraphObject, you'd just download the image over HTTP and load it (see http://stackoverflow.com/a/2472175/931354). otherwise, if you are using the android sdk to fetch a GraphObject, use our supplied ProfilePictureView to set the picture https://developers.facebook.com/docs/reference/android/3.0/ProfilePictureView/ – Jesse Chen Apr 27 '13 at 19:37
9
You can retreive user information for executeMeRequest in facebook 3.0 sdk.

    public void executeMeRequest(Session session) {

        Bundle bundle = new Bundle();
        bundle.putString("fields", "picture");
        final Request request = new Request(session, "me", bundle,
                HttpMethod.GET, new Request.Callback() {

            @Override
            public void onCompleted(Response response) {
                GraphObject graphObject = response.getGraphObject();
                if(graphObject != null) {
                    try {
                        JSONObject jsonObject = graphObject.getInnerJSONObject();
                        JSONObject obj = jsonObject.getJSONObject("picture").getJSONObject("data");
                        final String url = obj.getString("url");
                            new Thread(new Runnable() {

                                @Override
                                public void run() {

                                    final Bitmap bitmap = BitmapFactory.decodeStream(HttpRequest(url);
                                    runOnUiThread(new Runnable() {

                                        @Override
                                        public void run() {
                                            imageView.setImageBitmap(bitmap);
                                        }
                                    });
                                }
                            }).start();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        Request.executeBatchAsync(request);
    }

public static InputStream HttpRequest(String strUrl) {

    HttpResponse responce = null;
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(strUrl));
        responce = httpClient.execute(request);
        HttpEntity entity = responce.getEntity();
        return entity.getContent();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    return null;
}
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
  • 1
    This works really well. I just preferred to not use the method HttpRequest; instead I've used InputStream in = new java.net.URL(url).openStream(); final Bitmap bitmap = BitmapFactory.decodeStream( in ); – Juampa Oct 28 '14 at 00:07
2

You can get the profile picture by requesting to the Graph API. You need to pass the accessToken ,the user ID and also set the redirect false. Then the Graph API returns the JSON and from the JSON you can get url field which is the user profile.

  GraphRequest request = new GraphRequest(accessToken, "/" + userID + "/picture",null,HttpMethod.GET, new GraphRequest.Callback() {
                @Override
                public void onCompleted(GraphResponse response) {
                    Log.d("Photo Profile", response.getJSONObject().toString());
                    JSONObject jsonObject = response.getJSONObject();
                    try {

                        JSONObject data = jsonObject.getJSONObject("data");
                        String url = data.getString("url");
                        Picasso.with(getApplicationContext()).load(url).into(ivProfilePicture);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            Bundle parameters =new Bundle();
            parameters.putString("type","large");
            parameters.putBoolean("redirect",false);
            request.setParameters(parameters);
            request.executeAsync();
Nick Zisis
  • 288
  • 2
  • 12
1

You probably need to enable "Picture As Dictionary" on the advanced settings of your app in the developer console at https://developers.facebook.com/apps

Gray
  • 2,333
  • 1
  • 19
  • 24
1

As far as I know, it's not possible to get the image directly using the facebook graph API, because their API is designed to only accept JSON responses. It seems weird that they would allow a request that results in a non-JSON response, and especially strange that they would put that request in their official documentation *(https://developers.facebook.com/docs/graph-api/reference/user/picture/).

I am using the built in Android DefaultHttpClient library.

private Bitmap downloadImage(url) {
    Bitmap image = null;
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(imageUrl);
    try {
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();

        int imageLength = (int)(entity.getContentLength());

        InputStream is = entity.getContent();

        byte[] imageBlob = new byte[imageLength];

        int bytesRead = 0;

        // Pull the image's byte array

        while (bytesRead < imageLength) {
            int n = is.read(imageBlob, bytesRead, imageLength - bytesRead);
            bytesRead= n;
        }

        image = BitmapFactory.decodeByteArray(imageBlob, 0, imageBlob.length);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return image;
}
Cypress Frankenfeld
  • 2,317
  • 2
  • 28
  • 40
0

I got answer... it is due to redirection. hence pass params instead of null:

Bundle params = new Bundle();
params.putBoolean("redirect", false);
karan
  • 3,319
  • 1
  • 35
  • 44