I am trying to create a method that will return the download URL of the uploaded image to Firebase Storage as a String, so that I can use that String and insert it into the Firebase real-time database using a HashMap when I call that method. This is what I have done, but it does not work. What could be the problem? Any help will be appreciated.
String getImageDownloadUrlFromUploadedImages(){
final String[] myDownloadUrl = new String[1];
if (resultUri !=null){
final StorageReference fileReference;
fileReference = storageReference.child(System.currentTimeMillis() + "." + getFileExtension(resultUri));
uploadTask = fileReference.putFile(resultUri);
uploadTask.continueWithTask(new Continuation(){
@Override
public Object then(@NonNull Task task) throws Exception {
if (!task.isComplete()){
throw Objects.requireNonNull(task.getException());
}
return fileReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task <Uri> task) {
if (task.isSuccessful()){
Uri downloadUri = task.getResult();
myDownloadUrl[0] = downloadUri.toString();
loader.dismiss();
startActivity(new Intent(AskAQuestionActivity.this, MainActivity.class));
finish();
}else {
String error = Objects.requireNonNull(task.getException()).toString();
Toast.makeText(AskAQuestionActivity.this, "Failed" + error, Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(AskAQuestionActivity.this, "Question could not be posted." , Toast.LENGTH_SHORT).show();
}
});
}
else {
Toast.makeText(this, "No image is selected", Toast.LENGTH_SHORT).show();
}
return myDownloadUrl[0];
}