So, I have that method, that I want to use to get the download url for an image stored in the Firebase Storage, that's the method:
private void getUrlAsync (final StorageReference ref, Uri file){
UploadTask uploadTask = ref.putFile(file);
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
// Continue with the task to get the download URL
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
mDownloadUri = Objects.requireNonNull(task.getResult()).toString();
Log.d(TAG, "Get Url " + mDownloadUri);
} else {
// Handle failures
// ...
}
}
});
}
mDownloadUri is a global variable. Good, the Log.d in this method tells me that mDownloadUri = https://firebasestorage.googleapis.com/v0/b/lapitchat-4c76f.appspot.com/o/profile_images%2FCYUpqZy6AOOhLjvc7PG9JvnoY2p1.jpg?alt=media&token=6bd1b103-209d-4fad-b51e-9907a43f098d, that's ok. If I call this method this way, mDownloadUri is null:
final Uri resultUri = result.getUri();
final String current_user_id = mCurrentUser.getUid();
final StorageReference filepath = mImageStorage.child("profile_images").child(current_user_id + ".jpg");
filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if(task.isSuccessful()) {
mProgressDialog.dismiss();
getUrlAsync(filepath, resultUri);
Log.d(TAG, "Put File " + mDownloadUri);
}
}
For context, result is an image that comes back from another activity, where I crop an image. What can I do? Why outside the getUrlAsync method the mDownloadUri is null?