I'm trying to save user information in Firestore. Basically I save the image in Storage, then I save the information like name, email and URL of the image in Firestore. The main problem I'm facing: if inside uploadTask/taskSnapshot I use it only to get the URL of the image and assign it to a String, and outside of uploadTask/taskSnapshot I save all information at once, the String imageURL is null, it only receives the value of uri.tostring inside the uploadTask/taskSnapshot block. Outside, it doesn't matter where I put String imageURL, locally or globally, it always gets null. That way, I can save all the user information, but with a null image. The best way would be is below, that inside uploadTask/taskSnapshot I already saved all the information. At first I had problems doing it this way, because the user was created through FirebaseAuth, the image was uploaded to Storage, but the information was not saved to the firestore. After so many changes it worked perfectly, I thought it was problem solved. But now I'm adding other activities and this one of registering a user stopped working again. Having the same problem of not saving the information in Firestore, it just creates the user through FirebaseAuth and saves the image.
That way it doesn't save the information in Firestore:
private void SalveDataUser() {
loading(true);
userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
DocumentReference documentReference = db.collection(
Constants.KEY_COLLECTION_USERS).document(String.valueOf(userID));
if (imageUri != null) {
String filename = UUID.randomUUID().toString();
final StorageReference reference = FirebaseStorage.getInstance().getReference("/images/" + filename);
final UploadTask uploadTask = reference.putFile(imageUri);
uploadTask.addOnSuccessListener(taskSnapshot -> {
taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(uri -> {
String imageUrl = uri.toString();
HashMap<String, Object> users = new HashMap<>();
users.put(Constants.KEY_NAME, binding.etName.getText().toString());
users.put(Constants.KEY_IMAGE, imageUrl);
users.put(Constants.KEY_EMAIL, binding.etEmail.getText().toString());
documentReference.set(users)
.addOnSuccessListener(unused -> {
loading(false);
preferenceManager.putBoolean(Constants.KEY_IS_SIGNED_IN, true);
preferenceManager.putString(Constants.KEY_USER_ID, String.valueOf(userID));
preferenceManager.putString(Constants.KEY_IMAGE, imageUrl);
preferenceManager.putString(Constants.KEY_NAME, binding.etName.getText().toString());
Intent intent = new Intent(getApplicationContext(), Profile.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
})
.addOnFailureListener(e -> {
showToast("Erro ao cadastrar usuário: " + e);
});
}).addOnFailureListener(e -> {
showToast("Error: " + e);
});
}).addOnFailureListener(e -> {
showToast("Error: " + e);
});
}
}
This way it saves in Firestore, but the image is null:
private void SalveDataUser() {
loading(true);
userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
DocumentReference documentReference = db.collection(
Constants.KEY_COLLECTION_USERS).document(String.valueOf(userID));
if (imageUri != null) {
String filename = UUID.randomUUID().toString();
final StorageReference reference = FirebaseStorage.getInstance().getReference("/images/" + filename);
final UploadTask uploadTask = reference.putFile(imageUri);
final String[] imageUrl = new String[1];
uploadTask.addOnSuccessListener(taskSnapshot -> {
taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(uri -> {
imageUrl[0] = uri.toString();
}).addOnFailureListener(e -> {
showToast("Error: " + e);
});
}).addOnFailureListener(e -> {
showToast("Error: " + e);
});
HashMap<String, Object> users = new HashMap<>();
users.put(Constants.KEY_NAME, binding.etName.getText().toString());
users.put(Constants.KEY_IMAGE, imageUrl[0]);
users.put(Constants.KEY_EMAIL, binding.etEmail.getText().toString());
documentReference.set(users)
.addOnSuccessListener(unused -> {
loading(false);
preferenceManager.putBoolean(Constants.KEY_IS_SIGNED_IN, true);
preferenceManager.putString(Constants.KEY_USER_ID, String.valueOf(userID));
preferenceManager.putString(Constants.KEY_IMAGE, imageUrl[0]);
preferenceManager.putString(Constants.KEY_NAME, binding.etName.getText().toString());
Intent intent = new Intent(getApplicationContext(), Profile.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
})
.addOnFailureListener(e -> {
showToast("Erro ao cadastrar usuário: " + e);
});
}
}
IMAGE PRINTSCREEN: This way it saves in Firestore, but the image is null