我试图允许新用户上传个人资料图片,但是每当我尝试注册时,应用程序就会崩溃。在调试应用程序后,我发现它总是由于行175具有空用户ID而发生。第175行是:DatabaseReference myRef = firebaseDatabase.getReference(firebaseAuth.getUid());
我一直在关注本YouTube教程,https://www.youtube.com/watch?v=-v1O9rxienU,即使我们使用相同的代码,他上传的图像也没有问题。我该如何解决?
private EditText userName, userPassword, userEmail, userGrade;
private Button regButton;
private TextView userLogin;
private FirebaseAuth firebaseAuth;
private ImageView userProfilePic;
String email, name, grade, password;
private FirebaseStorage firebaseStorage;
private static int PICK_IMAGE = 123;
Uri imagePath;
private StorageReference storageReference;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE && resultCode == RESULT_OK && data.getData() != null){
imagePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imagePath);
userProfilePic.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register__form);
setupUiViews();
firebaseAuth = FirebaseAuth.getInstance();
firebaseStorage = FirebaseStorage.getInstance();
final StorageReference storageReference = firebaseStorage.getReference();
userProfilePic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent galleryIntent = new Intent();
// galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// galleryIntent.setType("image/*");
// startActivityForResult(galleryIntent, Gallery_Pick);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image"), PICK_IMAGE);
}
});
regButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(validate()){
//upload to database
//StorageReference myRef1 = storageReference.child(firebaseAuth.getUid());
String user_email = userEmail.getText().toString().trim();
String user_password = userPassword.getText().toString().trim();
firebaseAuth.createUserWithEmailAndPassword(user_email, user_password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
sendUserData();
firebaseAuth.signOut();
Toast.makeText(Register_Form.this, "Registration Successful", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Register_Form.this, login_screen.class));
}else{
Toast.makeText(Register_Form.this, "Registration Failed", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
userLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Register_Form.this, login_screen.class ));
}
});
}
private void setupUiViews(){
userName = (EditText)findViewById(R.id.etName);
userEmail = (EditText)findViewById(R.id.etEmail);
userPassword = (EditText)findViewById(R.id.etPass);
regButton = (Button)findViewById(R.id.btnRegister);
// regButton.setOnClickListener();
userLogin = (TextView)findViewById(R.id.tvSIgn_In);
userGrade = (EditText)findViewById(R.id.etGrade);
userProfilePic =(ImageView)findViewById(R.id.ivProfile);
}
private Boolean validate(){
Boolean result = false;
name = userName.getText().toString();
password = userPassword.getText().toString();
email = userEmail.getText().toString();
grade = userGrade.getText().toString();
if(name.isEmpty() || password.isEmpty() || email.isEmpty() || grade.isEmpty() || imagePath == null ) {
Toast.makeText(this, "Please enter all details", Toast.LENGTH_SHORT).show();
}else{
result = true;
}
return result;
}
private void sendUserData(){
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference myRef = firebaseDatabase.getReference(firebaseAuth.getUid());
StorageReference imageReference = storageReference.child(firebaseAuth.getUid()).child("Images").child("Profile Pic"); //User id/Images/Profile Pic.jpg
UploadTask uploadTask = imageReference.putFile(imagePath);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Register_Form.this, "Upload failed!", Toast.LENGTH_SHORT).show();
}
}).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
Toast.makeText(Register_Form.this, "Upload successful!", Toast.LENGTH_SHORT).show();
}
});
UserProfile userProfile = new UserProfile(grade, email, name);
myRef.setValue(userProfile);
}
}
创建/登录用户后,FirebaseAuth.getInstance().getCurrentUser()
可能不会立即更新。为了防止出现竞争状况,您需要将user from the AuthResult
传递到AuthResult
。
sendUserData