Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve profile picture using Facebook SDK 3.0 for Android

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

like image 657
Giulio Bider Avatar asked Nov 28 '12 17:11

Giulio Bider


3 Answers

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.

like image 90
Jesse Chen Avatar answered Nov 20 '22 19:11

Jesse Chen


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;
}
like image 22
Muhammad Aamir Ali Avatar answered Nov 20 '22 17:11

Muhammad Aamir Ali


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();
like image 2
Nick Zisis Avatar answered Nov 20 '22 19:11

Nick Zisis