我正在尝试在创建新用户时将图像上传到Firebase并获取图像downloadUrl
并将其作为用户详细信息的一部分发布到我的实时数据库中。有时downloadUrl
会被发布,有时它不会被发布但图像总是会成功上传。这是我的代码,我做错了吗?请帮忙......
上传图片方法
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
}
});
}
注册用户方法
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();
}
});
}