Created
November 5, 2012 10:58
-
-
Save danilao/4016626 to your computer and use it in GitHub Desktop.
Retrieve final URL from facebook profile image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
URL img_value = null; | |
try { | |
/* Public image url */ | |
img_value = new URL( "http://graph.facebook.com/" + id + "/picture?type=large" ); | |
/* For caching purposes we need to get an unique url, not the public */ | |
URL img_value_new = new URL("http://graph.facebook.com/" + id + "/picture?type=large&redirect=false" ); | |
/* Open the above url and get the final image profile URL */ | |
HttpClient http = new DefaultHttpClient(); | |
HttpGet httpMethod = new HttpGet(); | |
httpMethod.setURI (new URI(img_value_new .toString())); | |
HttpResponse response = http. execute(httpMethod ); | |
int responseCode = response. getStatusLine().getStatusCode (); | |
String responseBody = ""; | |
JSONObject resp = null; | |
/* If there're any problems we will use public URL */ | |
if(responseCode == 200){ | |
HttpEntity entity = response. getEntity(); | |
if (entity != null){ | |
responseBody = EntityUtils .toString( entity); | |
resp = new JSONObject( responseBody); | |
/* We extract the url key from 'data' JSONObject */ | |
img_value = new URL(resp .getJSONObject( "data").getString ("url")); | |
} | |
} | |
/* Bitmap creation */ | |
Bitmap bm = BitmapFactory.decodeStream(img_value); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment