I am trying to upload an image to Firebase when creating a new user and get the image downloadUrl
and post it to my realtime database as part of the users details. Sometimes the downloadUrl
gets posted, sometimes it doesn't get posted but the image always uploads successfully. This is my code, am I doing something wrong? Please help...
upload image method
private void postImage() {
String path = "userProfiles/" + UUID.randomUUID() + ".jpeg";
StorageReference userProfilesRef = storage.getReference(path);
userProfilesRef.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
downloadUrl = taskSnapshot.getDownloadUrl().toString();
//if the upload is successfull
//hiding the progress dialog
}
});
}
registering user method
private void registerUser(){
postImage();
//getting email and password from edit texts
String email = editTextEmail.getText().toString().trim();
final String username = editTextUsername.getText().toString().trim();
final String password = editTextPassword.getText().toString().trim();
//checking if email and passwords are empty
if(TextUtils.isEmpty(email)){
Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
return;
}
//if the email and password are not empty
//displaying a progress dialog
progressDialog.setMessage("Registering Please Wait...");
progressDialog.show();
//creating a new user
firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
//checking if success
if(task.isSuccessful()){
String user_id = firebaseAuth.getCurrentUser().getUid();
UserInformation userInformation = new UserInformation(username, halls, levels, downloadUrl);
databaseReference.child(sexs).child(user_id).setValue(userInformation);
Toast.makeText(MainActivity.this,"Successfully registered",Toast.LENGTH_LONG).show();
//finish();
//startActivity(new Intent(getApplicationContext(), Users.class));
}else{
//display some message here
Toast.makeText(MainActivity.this,"Registration Error",Toast.LENGTH_LONG).show();
}
progressDialog.dismiss();
}
});
}