Get cover art using the Facebook API

In my Android app, I am trying to get the cover photo of a user from my Facebook account.

I can get the profile image using the code below.

profilePicUrl = new URL("http://graph.facebook.com/" + userId + "/picture?type=large"); profilePicBmp = BitmapFactory.decodeStream(profilePicUrl.openConnection().getInputStream()); 

The documentation states the following for cover art.

User cover photo (must be explicitly requested using the field = coverage parameter)

Access_token required

Returns: an array of fields id, source and offset_y

So, the structure of the JSON response will be something like this.

 { "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" } 

I am new to the Facebook Graph API and therefore do not know how to do this.

I tried this coverPicUrl = new URL("http://graph.facebook.com/" + userId + "/cover?type=large");

as well as this coverPicUrl = new URL("http://graph.facebook.com/" + userId + "/fields=cover");

But I was not able to get the user profile cover.

A search on the Internet also did not bring any fruitful results.

Any help would really be appreciated.

Thanks!

+7
source share
5 answers

The source tag (JSONObject) is nested in another JSONObject, the cover tag. To analyze this result, you will need to use something like this:

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

JOCover used in the example assumes that you already have a JSONOBject (JOCover) for parsing the root. You can replace your own JSONObject in your place.

Cannot access the source tag because it is nested in the cover tag. You will need to use " .optJSONObject("cover") ". I saw people use .getString instead of .optJSONObject , but I never used it. Choose what works for you.

EDIT

According to your request for a solution using the Graph API, I am editing an earlier solution and replacing it with a Graph API solution.

Preferably, in AsyncTask , use this code in doInBackground :

 String URL = "https://graph.facebook.com/" + THE_USER_ID + "?fields=cover&access_token=" + Utility.mFacebook.getAccessToken(); String finalCoverPhoto; try { HttpClient hc = new DefaultHttpClient(); HttpGet get = new HttpGet(URL); HttpResponse rp = hc.execute(get); if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String result = EntityUtils.toString(rp.getEntity()); JSONObject JODetails = new JSONObject(result); if (JODetails.has("cover")) { String getInitialCover = JODetails.getString("cover"); if (getInitialCover.equals("null")) { finalCoverPhoto = null; } else { JSONObject JOCover = JODetails.optJSONObject("cover"); if (JOCover.has("source")) { finalCoverPhoto = JOCover.getString("source"); } else { finalCoverPhoto = null; } } } else { finalCoverPhoto = null; } } catch (Exception e) { // TODO: handle exception } 

I tested this solution and worked great. You will need to add any addition of fields to the base URL that is necessary for your activity. For testing, I used only fields=cover

And in onPostExecute do your job to display the cover. Hope this helps.

+8
source

Note : Cover fetching using the Facebook API and the endpoint https://graph.facebook.com/me?fields=cover no longer works as of December 20, 2014.

He should have given the following answer:

 { "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 the user id :

 { "id": "19292868552" } 

This is verified using Graph Tool explorer 2.2 using me?fields=cover .

+3
source

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

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

for example, you can get Image Url from a Json object.

 String cover_photo = JsonObject.getString("source"); 
+1
source

I implemented a similar function in my application. Here is how I did it using FQL:

 String cover_photo = jsonObj.getJSONObject("pic_cover").getString("source"); 

This gives the cover URL, as the source tag is embedded in the pic_cover object, which can then be set in ImageView using a library such as Universal Image Loader

+1
source
 object.getJSONObject("cover").getString("source") 
0
source

All Articles