使用 Picasso 在登录时获取 Facebook 个人资料图片

Using Picasso to get Facebook profile picture at login

我在互联网上到处搜索,找不到适合我的解决方案。当用户使用 facebook 登录按钮登录应用程序时,我想获取他们的 Facebook 个人资料图片并将其用作我的应用程序中的个人资料图片。我正在尝试使用 Picasso 从 Facebook URL 获取图片。这是我的代码。我遇到错误

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    if (getIntent().getBooleanExtra("EXIT", false)) {
    }

    FacebookSdk.sdkInitialize(getApplicationContext());
    AppEventsLogger.activateApp(this);

    loginButton = (LoginButton) findViewById(R.id.fb_login_btn);
    callbackManager = CallbackManager.Factory.create();


    accessTokenTracker = new AccessTokenTracker() {

        @Override
        protected void onCurrentAccessTokenChanged(AccessToken 
    oldAccessToken, AccessToken currentAccessToken) {

        }
    };

    accessToken = AccessToken.getCurrentAccessToken();

    if (AccessToken.getCurrentAccessToken() == null) {

        loginButton.registerCallback(callbackManager, new 
        FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

                Picasso.with(this) //I'm getting an error here "Picasso 
                cannot be applied"
                        .load("https://graph.facebook.com/" + 
                        loginResult.getAccessToken().getUserId(); + 
                        "/picture?type=large")
                        .into(profilePhoto);



                Toast.makeText(LoginActivity.this, "Login Successful", 
                Toast.LENGTH_SHORT).show();
                {
                    Intent intent = new Intent(LoginActivity.this, 
                    MainActivity.class);
                    startActivity(intent);
                }

                finish();

            }

            @Override
            public void onCancel() {

                Toast.makeText(LoginActivity.this, "Login Cancelled", 
                Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onError(FacebookException error) {

                Toast.makeText(LoginActivity.this, "Login Error", 
                Toast.LENGTH_SHORT).show();

            }
        });



    } else {

        Intent intent = new Intent(LoginActivity.this, 
        MainActivity.class);
        startActivity(intent);
        this.finish();

    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
data) {
    callbackManager.onActivityResult(requestCode,resultCode,data);
}

@Override
public void onDestroy() {
    super.onDestroy();
    accessTokenTracker.stopTracking();
}
}

这部分有问题

 Picasso.with(this) //I'm getting an error here "Picasso 
            cannot be applied"
                    .load("https://graph.facebook.com/" + 
                    facebook_id + "/picture?type=large")
                    .into(profilePhoto);

这里您需要传递 activity 或应用程序上下文而不是 'this'。 如果您在 activity 中,则键入 'YourActivityName.this',或者如果您在片段中,则使用 'getActivity'。 如果您在想为什么 'this' 不起作用,那么仅供参考 'this' 这里的意思是匿名内部 class。

试试这个

    loginButton.registerCallback(callbackManager, new 
    FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

            loginResult.getAccessToken().getUserId();



          GraphRequest request = GraphRequest.newMeRequest(
        loginResult.getAccessToken(),
        new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(
                    JSONObject object,
                    GraphResponse response) {
                // Application code

                try {


                    if (object.has("picture")) {
                        //String profilePicUrl="http://graph.facebook.com/"+object.getString("id")+"/picture?type=large";
                        String profilePicUrl = object.getJSONObject("picture").getJSONObject("data").getString("url");
                        profilePicUrl = profilePicUrl.replace("\", "");


                        Picasso.with(YourActivity.this)                          
                                .load(profilePicUrl)
                                .into(profilePhoto);

                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
           Bundle parameters = new Bundle();
                        parameters.putString("fields", "id,name,email,gender,picture.type(large)");
                        request.setParameters(parameters);
                        request.executeAsync();


            Toast.makeText(LoginActivity.this, "Login Successful", 
            Toast.LENGTH_SHORT).show();
            {
                Intent intent = new Intent(LoginActivity.this, 
                MainActivity.class);
                startActivity(intent);
            }

            finish();

        }

        @Override
        public void onCancel() {

            Toast.makeText(LoginActivity.this, "Login Cancelled", 
            Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(FacebookException error) {

            Toast.makeText(LoginActivity.this, "Login Error", 
            Toast.LENGTH_SHORT).show();

        }
    });