Reputation: 121
i try to show facebook avatar in my imageview .i use facebook graph api.i googled and i found one example.,but when i run program i can't show facebook avatar in imageview this is a my source
new Runnable() {
public void run() {
try {
img_value = new URL(
"http://graph.facebook.com/634179743325545/picture?type=large");
Bitmap mIcon1 = BitmapFactory.decodeStream(img_value
.openConnection().getInputStream());
user_img.setImageBitmap(mIcon1);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
what am i doing wrong ? if anyone knows solution please help me thanks
Upvotes: 1
Views: 1815
Reputation: 1594
This is Working Code
public static String getUrlFacebookUserAvatar(String name_or_idUser )
{
String address = "http://graph.facebook.com/"+name_or_idUser+"/picture";
URL url;
String newLocation = null;
try {
url = new URL(address);
HttpURLConnection.setFollowRedirects(false); //Do _not_ follow redirects!
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
newLocation = connection.getHeaderField("Location");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newLocation;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
and call this method
imgurl=getUrlFacebookUserAvatar("http://graph.facebook.com/100000351746269/picture?type=large");
new DownloadImageTask((ImageView) findViewById(R.id.img_mainpage_profile))
.execute(imgurl);
Upvotes: 1
Reputation: 73721
I never was able to get the profile image with a HTTP request, I used the built in Request to do it like this
Bundle b = new Bundle();
b.putString("fields", "id,name,email,picture,cover"); //only field you need it picture
Request r = Request.newMeRequest(fbSession, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
String url = getProfileUrl(response);
InputStream in = new URL(url).openStream();
Bitmap image = BitmapFactory.decodeStream(in);
}
});
r.setParameters(b);
r.executeAndWait();
private String getProfileUrl(Response response){
try{
JSONObject obj = new JSONObject(response.getRawResponse());
JSONObject JOSource = obj.optJSONObject("picture");
JSONObject data = JOSource.getJSONObject("data");
return data.getString("url");
}catch(Exception e){
e.printStackTrace();
}
return null;
}
you suply the bundle with the fields you want and the picture field returns JSON with the image url in it and then you just decode the stream
Upvotes: 0
Reputation: 178
have you tried to use Facebook's ProfilePictureView
?
It is intended to be used to insert the logged user's profile picture so basically what you want to do if I understand it well.
There is an example on how to use it in this tutorial.
Upvotes: 1
Reputation: 9996
I would suggest using AsyncTask
private class MyAsyncTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
img_value = new URL(
"http://graph.facebook.com/634179743325545/picture?type=large");
mIcon1 = BitmapFactory.decodeStream(img_value
.openConnection().getInputStream());
return null;
}
@Override
protected void onPostExecute(Void result){
user_img.setImageBitmap(mIcon1);
}
}
Upvotes: 2