我已经做了所有事情来获取一个网址,我可以获得Facebook用户的个人资料照片。
现在唯一的问题是将该图像转换为位图对象。
因为,http://graph.facebook.com现在首先将连接重定向(我们可以在网址中看到)https://fbcdn-profile-a.akamaihd.net/ ...(类似这样)。
所以,我想问一下如何从网址获取位图:http://graph.facebook.com/ ... 重定向到https://fbcdn-profile-a.akamaihd.net/ ...
答案 0 :(得分:10)
你说http://graph.facebook.com首先将连接重定向(我们可以在网址中看到)重定向到https://fbcdn-profile-a.akamaihd.net/,但是 -
当原始和重定向协议相同时,自动重定向会自动生效。
因此,如果您尝试从 https 而不是http:“https://graph.facebook.com/USER_ID/picture”加载图片;由于图片的网址是“https://fbcdn-profile-a.akamaihd.net/ ....”,BitmapFactory.decodeStream
会再次使用,以获取位图。
这是代码 -
URL imgUrl = new URL("https://graph.facebook.com/{user-id}/picture?type=large");
InputStream in = (InputStream) imgUrl.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(in);
希望有所帮助。祝你好运。
答案 1 :(得分:0)
更新:Sahil Mittal给出的方法非常好,我肯定会请你们使用他的方法。
至于我在此期间使用的方法,你可以阅读这个答案。
的
我不知道Sahil Mittal给出的方法是否有效。
我没有尝试过,但是我使用了另一段似乎对我有用的代码段。
但是我会尽快回来看它是否有效。
击>
Bitmap getUserBitmap(String username){
HttpGet httpRequest = null;
Bitmap userbmp = null;
try {
URL url = new URL("http://graph.facebook.com/" + username + "/picture?type=small");
httpRequest = new HttpGet(url.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
userbmp = BitmapFactory.decodeStream(instream);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
userbmp = getRoundedShape(userbmp); // function call to make the image round
return (userbmp);
}