我在他们的官方页面上遵循了facebook的教程:
Facebook登录教程
然而,在我完成这个我可以登录,但它不检索我的个人资料图片。有没有人有过这样的经历或者知道是什么原因导致的?我没有得到任何错误。
我想你很清楚获得facebook的userid和FacebookToken。
头像获取有两种方式:
1: Facebook已经为你提供了一个个人资料图片的视图,即ProfilePictureView
代替XML布局文件中的imageview。
在此视图中加载图像时,只需执行
ProfilePictureView profilePictureView;
profilePictureView = (ProfilePictureView) findViewById(R.id.friendProfilePicture);
profilePictureView.setCropped(true);
profilePictureView.setProfileId(USER_ID);
2:另一个选项是获得头像与facebook令牌的帮助
ImageView imgProfilePic12=(ImageView)findViewById(R.id.imgProfilePic);
new LoadProfileImage(imgProfilePic12)
.execute("https://graph.facebook.com/me/picture?type=normal&method=GET&access_token="
+ Faceboo_Access_Token);
/**
* Background Async task to load user profile picture from url
* */
private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public LoadProfileImage(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);
}
}
是否已成功登录Facebook ?如果是,则
try {
JSONObject fbResObj = new JSONObject(fbUser
.getInnerJSONObject().toString());
String id = fbResObj.getString("id");
url = new URL("http://graph.facebook.com/" + id
+ "/picture?style=small");
Log.v(TAG, url.toString());
bmp = BitmapFactory.decodeStream(url
.openConnection().getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
试试这个,其中fbUser是GraphUser的对象,你在执行GraphUserCallBack时得到,
private void getUserDetail() {
String fqlQuery = "SELECT name,uid,pic FROM user WHERE uid in (SELECT uid1 FROM friend WHERE uid1=me())";
Bundle _params = new Bundle();
_params.putString("q", fqlQuery);
Session _session = Session.getActiveSession();
Request _request = new Request(_session, "/fql", _params, HttpMethod.GET, new Request.Callback() {
public void onCompleted(Response response) {
GraphObject graphObject = response.getGraphObject();
if (graphObject != null) {
if (graphObject.getProperty("data") != null) {
try {
String _arry = graphObject.getProperty("data").toString();
JSONArray _jsonArray = new JSONArray(_arry);
if(_jsonArray.length()==1)
{
//here u will get your user's userId and Profile Picture and Name
String _uid = _jsonObject.getString("uid");
String _Name = _jsonObject.getString("name");
String _ImagePath = _jsonObject.getString("pic");
}
}
} catch (JSONException ex) {
if (ex != null) {
Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}
}
});
Request.executeBatchAsync(_request);
}
如果你有Facebook用户id获取图片使用这个链接
https://graph.facebook.com/ FACEBOOK_USER_ID /图片? type =大";
使用type = small