无法通过 Android 下载 Facebook 用户个人资料图片
Can't download Facebook user profile picture via Android
我的问题是,我无法通过 ID 下载用户个人资料图片。我可以在使用浏览器时获取用户个人资料图片 URL :
http://graph.facebook.com/janno.hindrekson/picture?type=small
我可以看到这个用户的头像:
但是当我想通过Android获取这张图片时,我无法
// Execute the task
new LoadImage().execute("http://graph.facebook.com/janno.hindrekson/picture?type=small");
}
private class LoadImage extends AsyncTask<String, String, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(TESTPROFILEPICT.this);
pDialog.setMessage("Loading Image ....");
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if (image != null) {
fbUserAvatar.setImageBitmap(image);
pDialog.dismiss();
} else {
pDialog.dismiss();
Toast.makeText(TESTPROFILEPICT.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
有什么想法吗?我应该如何通过 Android 下载个人资料图片?
谢谢,
凯文
我也有这个问题。尝试用相应的 facebookId 替换名称。
new LoadImage().execute("http://graph.facebook.com/100001997422983/picture?type=small");
编辑
我知道这个问题在 Whosebug 上被问过好几次了。
无论如何,原因是 facebook 重定向了 url。因此,您必须调用重定向的 url。
这是一个很好的例子:
http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/
或者您必须使用 Facebook API:
https://developers.facebook.com/docs/graph-api/reference/user/picture/
我选择了redirect-method,因为它是通用的,你可以从整个互联网上获取图片。
在 Android,如果您可以将 Facebook SDK 导入到您的项目中,则有一种更简单的方法可以完成此任务。
SDK 中有一个方法叫做 Profile.getProfilePictureUri(int, int):
https://developers.facebook.com/docs/reference/android/current/class/Profile/
我的问题是,我无法通过 ID 下载用户个人资料图片。我可以在使用浏览器时获取用户个人资料图片 URL :
http://graph.facebook.com/janno.hindrekson/picture?type=small
我可以看到这个用户的头像:
但是当我想通过Android获取这张图片时,我无法
// Execute the task
new LoadImage().execute("http://graph.facebook.com/janno.hindrekson/picture?type=small");
}
private class LoadImage extends AsyncTask<String, String, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(TESTPROFILEPICT.this);
pDialog.setMessage("Loading Image ....");
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if (image != null) {
fbUserAvatar.setImageBitmap(image);
pDialog.dismiss();
} else {
pDialog.dismiss();
Toast.makeText(TESTPROFILEPICT.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
有什么想法吗?我应该如何通过 Android 下载个人资料图片?
谢谢, 凯文
我也有这个问题。尝试用相应的 facebookId 替换名称。
new LoadImage().execute("http://graph.facebook.com/100001997422983/picture?type=small");
编辑
我知道这个问题在 Whosebug 上被问过好几次了。
无论如何,原因是 facebook 重定向了 url。因此,您必须调用重定向的 url。 这是一个很好的例子:
http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/
或者您必须使用 Facebook API: https://developers.facebook.com/docs/graph-api/reference/user/picture/
我选择了redirect-method,因为它是通用的,你可以从整个互联网上获取图片。
在 Android,如果您可以将 Facebook SDK 导入到您的项目中,则有一种更简单的方法可以完成此任务。
SDK 中有一个方法叫做 Profile.getProfilePictureUri(int, int):
https://developers.facebook.com/docs/reference/android/current/class/Profile/