简体   繁体   中英

Is it possible to handle no profile picture from Facebook and Google login SDK for android?

Currently I am using the following methods for getting facebook and google profile pictures:

Google:

    private void updateUI(GoogleSignInAccount acct) {
    if (acct != null) {
        Uri personPhoto = acct.getPhotoUrl();
        if (!(Uri.EMPTY.equals(personPhoto)) && personPhoto != null) {
            editor.putString("imgUrl", personPhoto.toString());
        }
    }

Facebook:

    private void facebookLogin() {
    LoginManager.getInstance().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 {

                                String userId = object.getString("id");
                                String imgUrl = "https://graph.facebook.com/" + userId + "/picture?type=large";
                            } catch (org.json.JSONException e) {
                                Log.e("", e.getMessage(), e);
                            }

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

        @Override
        public void onCancel() {
        }

        @Override
        public void onError(FacebookException error) {
            Toast.makeText(getApplicationContext(), "failed " + error.toString(),
                    Toast.LENGTH_LONG).show();
        }
    });
}

Anyway everything is OK with this method until the user actually does not have profile picture. It does not return null or something which I can handle.
That's what they give as picture when user does not have one

Facebook: picture

Google: picture

Facebook's picture field has an is_silhouette property, see https://developers.facebook.com/docs/graph-api/reference/profile-picture-source/

And Google seems to have something similar called isDefault , see How to check whether Google User's image is default or uploaded?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM