问题描述
我已经在我的Android应用程序中添加了Firebase Auth,我正在使用Facebook和Google登录。登录是正常的。现在我想从用户的Facebook帐户或Google帐户(无论用户选择登录)检索用户个人资料信息,并在用户个人资料活动中显示信息。我可以检索和显示用户名和用户UID,但是,用户电子邮件和用户照片都没有显示,但已经被检索。检索用户信息(LoginActivity.java):
authStateListener = new FirebaseAuth.AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user!= null){
//用户登录
(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 {
//用户登出
return;
}
}
};
以下是显示用户信息(ProfileActivity.java)的方式: / p>
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,));
我搜索了很多,但无法获得相同的解决方案。
任何人都可以帮助?
Uri refrence总是空
。尝试将其存储为如下所示的字符串:
String uri = userInfo.getPhotoUrl()。toString;
然后将其传递给首选项,然后将其设置为:
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) ;
}
确保你添加毕加索依赖关系到你的毕业生。 p>
compile'com.squareup.picasso:picasso:2.3.2'
for emailID:
String email = pref.getString(电子邮件,null);
//尝试查看这里
Log.w(email,+ email);
if(email!= null&!email.isEmpty()){
textEmail.setText(+ email);
}
或者您可以使用意向服务,这是确定的:
在您登录活动中:
Intent intent = new Intent(this,MainActivity。类);
intent.putExtra(email_id,email_id);
startActivity(intent);
finish();
和你的mainActivity:
Intent intent = getIntent();
String email = intent.getStringExtra(email_id);
textEmail.setText(+ email);
update-2
有一个firebase方法..和它最简单的方法。
在您要恢复数据的第二个活动中写入此代码..
FirebaseUser user = FirebaseAuth.getInstance()。getCurrentUser();
TextView电子邮件;
email.setText(+ user.getEmail());
不需要sharedPrefrence或意向服务。
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????
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.
这篇关于Firebase认证:Android:无法显示用户电子邮件和照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!