How To Upload Image Url Uploaded To Fire Storage And Save It In Firestore At The Same Time Using Java
Here's my code where i can retrieve the image Url in Toast message where it is unable to save it in firestore please tell me if there is another method to do it or is the problem w
Solution 1:
This process is called AsyncTask, which is intended to perform a process before a process.
It works to upload the image and the data at the same time, so the image link does not store because it did not finish uploading the image, and this is in less than a split of a second.
You can use this method when the image is finished uploading, the data is stored in FireStore :
privatevoidregisterUser(final String email, final String password) {
auth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(register.this, newOnCompleteListener<AuthResult>() {
@OverridepublicvoidonComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
upload ();
}
});
Here be sure to upload the image first and then add ImageUrl to the fireStore :
publicvoid upload (){
if (imageUri != null){
final StorageReference fileRef = FirebaseStorage.getInstance().getReference(System.currentTimeMillis()+getFileExtension(imageUri) );
fileRef.putFile(imageUri)
.addOnCompleteListener(newOnCompleteListener<UploadTask.TaskSnapshot>() {
@OverridepublicvoidonComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
fileRef.getDownloadUrl().addOnSuccessListener(newOnSuccessListener<Uri>() {
@OverridepublicvoidonSuccess(Uri uri) {
url = uri;
Map<String , Object> users = newHashMap<>();
users.put("Email", emails.getText().toString());
users.put("Password", passwords.getText().toString());
users.put("Country", country.getText().toString());
users.put("AGe", ages.getText().toString());
users.put("Image", url.toString());
auth1 = FirebaseAuth.getInstance().getCurrentUser();
db.collection("Users").document(auth1.getUid()).set(users).addOnCompleteListener(newOnCompleteListener<Void>() {
@OverridepublicvoidonComplete(@NonNull Task<Void> task) {
Toast.makeText(register.this,"Upload Sucess" + url.toString(),Toast.LENGTH_SHORT).show();
}
});
}
});
}
});
}
}
Post a Comment for "How To Upload Image Url Uploaded To Fire Storage And Save It In Firestore At The Same Time Using Java"