我无法获取Facebook个人资料图片,以便在我的片段中显示它。名称,生日和链接已成功获取,但是当我尝试获取个人资料图片时,应用程序停止工作。如何摆脱这种情况?
if(a1bool == true) {
if (!document.getElementById("arbol")) {
var s = '<p align="center" id="arbol"> <img border=0 src="arbol.png" width="250" /></p>';
var div = document.createElement('div');
div.innerHTML = s;
var elements = div.firstChild;
document.body.appendChild(elements);
}
correctAnswers++;
}
答案 0 :(得分:2)
你能得到的一切。这就是代码。
private CallbackManager callbackManager;
private LoginButton loginButton;
in OnCreate(){
loginButton = (LoginButton) findViewById(R.id.login_button);
// don't forget to give this.
loginButton.setReadPermissions(Arrays.asList("public_profile,email,user_birthday"));
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDetails();
}
});
}
private void getDetails() {
//for facebook
// FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
//register callback object for facebook result
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
try {
Profile profile = Profile.getCurrentProfile();
if (profile != null) {
String facebook_id = profile.getId();
String f_name = profile.getFirstName();
String l_name = profile.getLastName();
profile_image = profile.getProfilePictureUri(400, 400).toString();
}
String email_id = jsonObject.getString("email"); //email id
} catch (JSONException e) {
Logger.logError(e);
}
}
});
这肯定有效。
检查您是否使用此功能。 com.facebook.android:facebook-android-sdk:4.0.0
答案 1 :(得分:1)
要获得快速解决方案,您可以通过以下方式获取用户的个人资料照片:
String profilePictureUrl = "https://graph.facebook.com/"+userID+"/picture?width=500&height=500";
使用您的图像加载程序库下载到图像视图。
编辑:
这是一个简单的Android ImageLoading库(旧但很好) https://github.com/nostra13/Android-Universal-Image-Loader
易于使用: 将其作为gradle依赖项添加到项目中。
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(profilePictureUrl, yourImageView);
答案 2 :(得分:0)
我正在使用:
public void getUserDetailsFromFB(AccessToken accessToken) {
GraphRequest req=GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Toast.makeText(getApplicationContext(),"graph request completed",Toast.LENGTH_SHORT).show();
try{
String email = object.getString("email");
String birthday = object.getString("birthday");
String gender = object.getString("gender");
String name = object.getString("name");
String id = object.getString("id");
String photourl =object.getJSONObject("picture").getJSONObject("data").getString("url");
}catch (JSONException e)
{
Toast.makeText(getApplicationContext(),"graph request error : "+e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday,picture.type(large)");
req.setParameters(parameters);
req.executeAsync();
}
然后使用Picasso ,例如:Picasso.with(context).load(photourl).into(imageView);
示例Json
格式:
{
"id": "10204968538960870",
"name": "Yasin Kaçmaz",
"email": "yasin_5775@hotmail.com",
"gender": "male",
"birthday": "12/06/1994",
"picture": {
"data": {
"is_silhouette": false,
"url": "https://scontent.xx.fbcdn.net/v/t1.0..."
}
}
}
您可以尝试Graph Api
查看示例Json
格式:Graph Api Explorer