无法将用户邮件和个人资料照片检索到个人资料片段中

时间:2019-06-28 04:13:06

标签: android firebase-realtime-database google-signin

在应用中,用户登录后,用户的邮件和个人资料照片应显示在个人资料片段中。但是,它不起作用

我们正在使用SharedPreferences来获取详细信息

//这里是ProfileFragment.java

public class ProfileFragment extends Fragment {

Button signOut;

private TextView nameTextView;
private ImageView profileImageView;

String name;
String profilePicURL;
//FirebaseAuth mAuth;

PersistentDeviceStorage persistentDeviceStorage;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_profile, container, false);

    persistentDeviceStorage = PersistentDeviceStorage.getInstance(getContext());

    profilePicURL = persistentDeviceStorage.getProfileUrl ();
    name = persistentDeviceStorage.getName();
    signOut = (Button) view.findViewById(R.id.signOut);

    profileImageView = (ImageView) view.findViewById(R.id.profile_pic);

    nameTextView = (TextView) view.findViewById(R.id.username);


    nameTextView.setText(name);


    setImage();

    signOut.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {
            persistentDeviceStorage.setEmail("null");
            persistentDeviceStorage.setToken("null");
            persistentDeviceStorage.setProfileUrl("null");
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);
            if (getActivity() != null) {
                getActivity().finish();
            }
        }
    });

    return view;
}

private void setImage() {
    if (Utils.hasActiveInternetConnection(getContext()) && profilePicURL != null && (profilePicURL.contains(".com") || profilePicURL.contains(".net"))) {
        Utils.loadImage(profilePicURL, profileImageView, getContext());
    } else {
        TextDrawable drawable = TextDrawable.builder()
                .buildRound(String.valueOf(name.toCharArray()[0]).toUpperCase(), getResources().getColor(R.color.colorPrimary));
        profileImageView.setImageDrawable(drawable);
        profileImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        profileImageView.setBackground(null);
    }
}
}

PersistentDeviceStrorage.java似乎可以正常运行,但更改未反映在应用程序中。

//这里是PersistentDeviceStorage.java

public class PersistentDeviceStorage{

private static PersistentDeviceStorage persistStorage = null;

private SharedPreferences sharedPreferences;

public synchronized static PersistentDeviceStorage getInstance(Context context) {
    if (persistStorage == null) {
        persistStorage = new PersistentDeviceStorage(context);
    }
    return persistStorage;
}

private PersistentDeviceStorage(Context context) {
   // sharedPreferences= Activity.getApplicationContext().getSharedPreferences(
            //sharedPreferences,0);
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
}




public String getEmail()
{
    return sharedPreferences.getString(Constants.EMAIL_ID, "null");
}

public void setEmail(String email) {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(Constants.EMAIL_ID, email);
    editor.apply();
    editor.commit();
}

public void setName(String name) {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(Constants.NAME, name);
    editor.apply();
    editor.commit();
}

public String getName() {

    return sharedPreferences.getString(Constants.NAME, "NoName");
}


public void setToken(String token) {
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(Constants.TOKEN, token);
    editor.apply();
    editor.commit();
}

public String getToken() {
    return sharedPreferences.getString(Constants.TOKEN, "null");
}

public void setProfileUrl(String url) {
    if(url!=null) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(Constants.PROFILE_URL, url);
        editor.apply();
        editor.commit();
    }
}

public String getProfileUrl() {
    return sharedPreferences.getString(Constants.PROFILE_URL, "null");
}

}

我尝试使用以下代码设置profileimageURL和用户名

//In profile fragment,
GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(getActivity());
    if (acct != null) {
        //String personName = acct.getDisplayName();
        personGivenName = acct.getGivenName();
        personEmail = acct.getEmail();
        personPhoto = acct.getPhotoUrl();
    }

    persistentDeviceStorage.setName(personGivenName);
    persistentDeviceStorage.setProfileUrl(personPhoto);

但是,我无法将Uri转换为字符串,因为共享首选项编辑器需要一个字符串

很少编辑,     在MainActivity.java

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

    if (requestCode == RC_SIGN_IN) {
        IdpResponse response = IdpResponse.fromResultIntent(data);
        assert response != null;
        System.out.println(response.toString());
        if (resultCode == RESULT_OK) {
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            if (user != null) {
                String email = user.getEmail();
                String name = user.getDisplayName();
                //Uri profileUri = user.getPhotoUrl();
                /*for (UserInfo userInfo : user.getProviderData()) {
                    if (name == null && userInfo.getDisplayName() != null) {
                        name = userInfo.getDisplayName();
                    }
                    if (profileUri == null && userInfo.getPhotoUrl() != null) {
                        profileUri = userInfo.getPhotoUrl();
                    }
                }*/
                Log.d("position"," "+name);
                persistentDeviceStorage.setEmail(email);
                persistentDeviceStorage.setName(name);
                gotoHomeActivity();
            } else {
                Toast.makeText(this, "Please try again", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "Please try again", Toast.LENGTH_SHORT).show();
        }
    }
   }

但是,日志消息显示name = null

0 个答案:

没有答案