Skip to content Skip to sidebar Skip to footer

Get Cover Photo Using Facebook Api

In my Android application, I am trying to get the cover photo of the user from his Facebook account. I can get the profile picture by using the below code. profilePicUrl = new URL(

Solution 1:

The "source" tag (JSONObject) is nested inside another JSONObject, the "cover" tag. To parse this result, you will have to use something like this:

JSONObjectJOSource = JOCover.optJSONObject("cover");
String coverPhoto = JOSource.getString("source");

The JOCover used in the example assumes that you already have a JSONOBject (JOCover) to parse the root. You can substitute your own JSONObject in its place.

The "source" tag cannot be accessed directly as it is nested in the "cover" tag. You will have to use ".optJSONObject("cover")". I have seen people use .getString instead of the .optJSONObject but I have never used it. Choose what works for you.

EDIT

As per your request for a solution using Graph API, I am editing the earlier solution and replacing it with the Graph API solution.

Preferably, in an AsyncTask, use this code in the doInBackground:

StringURL = "https://graph.facebook.com/" + THE_USER_ID + "?fields=cover&access_token=" + Utility.mFacebook.getAccessToken();

String finalCoverPhoto;

try {

    HttpClient hc = newDefaultHttpClient();
    HttpGet get = newHttpGet(URL);
    HttpResponse rp = hc.execute(get);

    if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String result = EntityUtils.toString(rp.getEntity());

        JSONObjectJODetails = newJSONObject(result);

        if (JODetails.has("cover")) {
            String getInitialCover = JODetails.getString("cover");

            if (getInitialCover.equals("null")) {
                finalCoverPhoto = null;
        } else {
            JSONObjectJOCover = JODetails.optJSONObject("cover");

            if (JOCover.has("source"))  {
                finalCoverPhoto = JOCover.getString("source");
            } else {
                finalCoverPhoto = null;
            }
        }
    } else {
        finalCoverPhoto = null;
    }
} catch (Exception e) {
    // TODO: handle exception
}

I have tested this solution and works perfectly. You will have to add any addition fields to the base URL that are required for your activity. For the sake of testing, I used just the fields=cover

And in the onPostExecute, do your thing to display the cover picture. Hope this helps.

Solution 2:

Note: Fetching Cover Photo using Facebook API and endpoint https://graph.facebook.com/me?fields=cover no longer works as on 20th Dec 2014.

It was supposed to give following response:

{"cover":{"cover_id":"10151008748223553","source":"http://sphotos-a.ak.fbcdn.net/hphotos-ak-ash4/s720x720/391237_10151008748223553_422785532_n.jpg","offset_y":0},"id":"19292868552"}

But now it just gives User's id:

{"id":"19292868552"}

Verified this using Graph Tool explorer 2.2 using me?fields=cover.

Solution 3:

You have already cover photo Url just read that String from the Json array, from that String you can get the Cover photo. this is the cover photo url

"source":"http://sphotos-a.ak.fbcdn.net/hphotos-ak-ash4/s720x720/391237_10151008748223553_422785532_n.jpg",

like this you can get the Image Url form the Json Object.

Stringcover_photo= JsonObject.getString("source");

Solution 4:

I implemented a similar feature in my app. This is how I did it, using FQL:

Stringcover_photo= jsonObj.getJSONObject("pic_cover").getString("source");

This gives the url to a cover photo, since the source tag is nested within the pic_cover object, which can then be set to an ImageView using a library like Universal Image Loader

Solution 5:

object.getJSONObject("cover").getString("source")

Post a Comment for "Get Cover Photo Using Facebook Api"