Android,Facebook SDK获取个人资料图片失败

时间:2016-01-17 18:05:04

标签: android facebook-sdk-4.0

我正在尝试使用Facebook SDK获取包含用户ID的个人资料照片。

但它没有用,我找不到任何错误:

public class MainActivity extends AppCompatActivity {

    private TextView info;
    private LoginButton loginButton;
    private CallbackManager callbackManager;
    public String user_id;
    public ImageView imageView;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        setContentView(R.layout.activity_main);
        info = (TextView)findViewById(R.id.info);
        imageView =(ImageView)findViewById(R.id.profile_image);
        loginButton = (LoginButton)findViewById(R.id.login_button);

        loginButton.setReadPermissions(Arrays.asList("public_profile, email"));

        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {

                                try {
                                    user_id = response.getJSONObject().get("id") + "";
                                    Log.i("User id", user_id);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                                try {
                                    response.getJSONObject().get("id");
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender, birthday");
                request.setParameters(parameters);
                request.executeAsync();
                setProfile();
            }


            @Override
            public void onCancel() {
                info.setText("Login attempt canceled.");
            }

            @Override
            public void onError(FacebookException error) {
                info.setText("Login attempt failed.");
            }
        });

    }
    public void setProfile(){

        getFacebookProfilePicture(user_id);

    }

    public  void getFacebookProfilePicture(String userID){

        try {



            String imageUrl = "https://graph.facebook.com/" + userID + "/picture?type=large";

            Picasso.with(getApplicationContext()).load(imageUrl).into(imageView);
             //bitmap = BitmapFactory.decodeStream((InputStream)imageUrl.getContent());

            //InputStream is = (InputStream) imageUrl.getContent();
            //Drawable d = Drawable.createFromStream(is, "src name");

        }
        catch (Exception e){
            Log.e("ReadRdaJSONFeedTask", e.getLocalizedMessage() == null ? "ERROR IS NULL" : "ERROR IS NOT NULL AND IT IS:"+e.getLocalizedMessage());
        }

    }
}

1 个答案:

答案 0 :(得分:0)

setProfile();放在

之下
user_id = response.getJSONObject().get("id") + "";
Log.i("User id", user_id);

 @Override
 public void onCompleted(JSONObject object, GraphResponse response) { });

所以你的代码应该是

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {

                                try {
                                    user_id = response.getJSONObject().get("id") + "";
                                    Log.i("User id", user_id);
                                    setProfile();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                                try {
                                    response.getJSONObject().get("id");
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender, birthday");
                request.setParameters(parameters);
                request.executeAsync();

            }