如何在Facebook SDK 3.0 Android中获取用户的Facebook个人资料图片

15

我正在使用 Facebook SDK 3.0,我需要获取用户登录的个人资料图片。这是我使用的代码:

URL image_value = new URL("http://graph.facebook.com/"+id+"/picture" );
profPict=BitmapFactory.decodeStream(image_value.openConnection().getInputStream());

但我没有得到期望的结果。


1
我也在寻找同样的问题,但是没有获取到准确的头像图片,使用了相同的代码 :) - user
8个回答

24

16

我也遇到了同样的问题,请使用https://而不是http://

URL image_value = new URL("https://graph.facebook.com/"+id+"/picture" );
profPict = BitmapFactory.decodeStream(image_value.openConnection().getInputStream());

1
谢谢!唯一有用的答案。 - Dima
我在你的帮助下解决了相同的问题。谢谢。 - TeachMeJava

12
如果您想在应用程序中显示个人资料图片,请使用Facebook SDK中的ProfilePictureView
请参考此处
只需在其上调用setProfileId(String profileId)
它将负责显示图像。

它解决了问题吗? - Vishal Pawale
它是异步加载的吗? - Elad Benda
是的,它异步加载图片 :) - Vishal Pawale
有没有办法将这个应用程序与我获取的用户好友的范围ID一起使用? - Ziv Kesten
在新的SDK中,setProfileId无法正常工作,它没有加载显示默认头像的图像。如果有人有想法,请让我知道。 - Fazal Hussain
显示剩余2条评论

5
String id = user.getId();
try {
  URL url = new URL("http://graph.facebook.com/"+ id+ "/picture?type=large");
  String image_path = uri.toString();
  System.out.println("image::> " + image_path);
}
catch (MalformedURLException e) {
  e.printStackTrace();
}

5
使用 Facebook SDK 中的 ProfilePictureView

使用用户ID加载图像时出现了无法解释的行为,它没有成功加载。 - Fazal Hussain

3

您可以在线程内执行以下操作:

String url = "http://graph.facebook.com/"+id+"/picture";
HttpConnection conn = new HttpConnection(url);
conn.openConnection();

Drawable d = Drawable.createFromStream(new BufferedInputStream(conn.getInputStream()), "image");

conn.close();

我希望您能从中受益。

2

试试这个...

 try {
        imageURL = new URL("https://graph.facebook.com/" +
                                                id+ "/picture?type=large");
        Log.e("URL", imageURL.toString());
        } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
             try {
                    bitmap = BitmapFactory.decodeStream(imageURL
                                .openConnection().getInputStream());
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

      ProfileDp.setImageBitmap(bitmap);

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接