Firebase Auth: Android: Unable to display user email and photo

梦想的初衷 提交于 2019-12-08 02:01:58

问题


I have added Firebase Auth in my android app and I am using Facebook and Google sign in. Signing in is working fine. Now I want to retrieve user profile info from user's either Facebook account or Google account (whichever, user chooses to sign in) and display the info in user profile activity. I am able to retrieve and display user name and user UID, however, user email and user photo are not displaying, though retrieved.

Here is how I am retrieving user info (LoginActivity.java):

authStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

                FirebaseUser user = firebaseAuth.getCurrentUser();

                if (user != null) {
                    //User is signed in

                    for (UserInfo userInfo : user.getProviderData()){

                        String user_name = userInfo.getDisplayName();
                        String user_email = userInfo.getEmail();
                        Uri user_img = userInfo.getPhotoUrl();
                        String user_id = userInfo.getUid();

                        SharedPreferences preferences = getSharedPreferences(UserInfoConstants.STRING_NAME,0);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putString(UserInfoConstants.KEY_USER_NAME, user_name);
                        editor.putString(UserInfoConstants.KEY_USER_EMAIL, user_email);
                        editor.putString(UserInfoConstants.KEY_USER_ID, user_id);
                        editor.putString(UserInfoConstants.KEY_USER_IMG, user_img != null ? user_img.toString() : null);
                        editor.apply();
                    }

                } else {
                    //User is signed out
                    return;
                }
            }
        };

Here is how am displaying user info (ProfileActivity.java):

SharedPreferences preferences = getSharedPreferences(LoginActivity.UserInfoConstants.STRING_NAME,0);

        String user_img_uri = preferences.getString(LoginActivity.UserInfoConstants.KEY_USER_IMG,"");

        if (user_img_uri.isEmpty()){

            userImgView.setImageResource(R.drawable.com_facebook_profile_picture_blank_portrait);
        } else {

            userImgView.setImageBitmap(getImageBitmap(user_img_uri));
        }
        userName.setText(preferences.getString(LoginActivity.UserInfoConstants.KEY_USER_NAME,""));
        userEmail.setText(preferences.getString(LoginActivity.UserInfoConstants.KEY_USER_EMAIL,""));
        userId.setText("ID: " + preferences.getString(LoginActivity.UserInfoConstants.KEY_USER_ID,""));

I searched a lot about it but is unable to get any resolution for the same.

Can anyone help????


回答1:


Uri refrence is always null. Try to store it as string like this:

String uri =  userInfo.getPhotoUrl().toString;

And pass it to preference, then set it like this:

String fbImage = pref.getString("ImageUri", "");

if (fbImage != null && !fbImage.isEmpty()) {
            Picasso.with(getApplicationContext())
                    .load(fbImage)
                    .placeholder(R.drawable.img_circle_placeholder)
                    .resize(avatarSize, avatarSize)
                    .centerCrop()
                    .transform(new CircleTransformation())
                    .into(ImageViewObject);

        }

Make sure you added picasso dependency to your gradle.

 compile 'com.squareup.picasso:picasso:2.3.2'

for emailID:

String email = pref.getString("email", null);

//try to check here
Log.w("email", ""+email);

if(email != null && !email.isEmpty()){
   textEmail.setText(""+email);
}

or you can use intent service this is work for sure:

In you login activity:

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("email_id", email_id);
startActivity(intent);
finish();

and in your mainActivity:

Intent intent = getIntent();
String email = intent.getStringExtra("email_id");

textEmail.setText(""+email);

update-2

there is one firebase method.. and its easiest way to do it.

write this code in your second activity where you want to retrive data..

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
TextView email;

email.setText(""+ user.getEmail());

there is no need of sharedPrefrence or intent service.



来源:https://stackoverflow.com/questions/38843758/firebase-auth-android-unable-to-display-user-email-and-photo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!