Can't Get Download Url from Firebase Storge in Android

Why can't this retieve the download URL of an image on Firebase Storage?

The call to getDownloadURL makes a call to the server, and thus is executed asynchronously. This means that right now in your code, the Toast.makeText(ContactCreation.this, url runs before the url = uri.toString() ever runs, which explains why it logs no value.

The solution is always the same: any code that needs the value, needs to be inside the onSuccess that is called with the value, or it needs to be called from there.

So:

task.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
url = uri.toString();
Toast.makeText(ContactCreation.this, url, Toast.LENGTH_SHORT).show();
}
});

If you're new to such asynchronous behavior, I recommend reading some more about it. For example with:

  • URL generated by Firebase Storage does not allow access to the file, which shows some chaining of Task listeners.
  • How to get the download url from Firebase Storage?
  • Hello, I have problem to upload my image to my firebase storage

Can't get actual download Url of an image uploaded in firebase storage

Calling getDownloadUrl() starts an asynchronous operation, which may take some time to complete. Because of this, your main code continues to run while the download URL is being retrieved to prevent blocking the user. Then when the download URL is available, your onSuccess gets called. Because of this, by the time your intent.putExtra("imageURL", imageUrl) now runs, the imageUrl = uri.toString() hasn't run yet.

To see that this indeed true, I recommend putting some logging in the code to just show the flow, or running it in a debugger and setting breakpoints on the lines I indicated above.

To fix it, any code that needs the download URL, needs to be inside the onSuccess, or be called from there. This applies not just here, but to all code that runs asynchronously, which includes most modern cloud APIs. So I recommend spending some time now studying this behavior, so that you're more comfortable with it going forward.

In your code:

final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("images").child(imageName);

final UploadTask uploadTask = storageReference.putBytes(data);

uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Toast.makeText(CreateSnapActivity.this, "Upload Failed", Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
Task<Uri> urlTask = storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
imageUrl = uri.toString();
Toast.makeText(CreateSnapActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();

Intent intent = new Intent(CreateSnapActivity.this, ChooseUserActivity.class);
intent.putExtra("imageName", imageName);
intent.putExtra("imageURL", imageUrl);
intent.putExtra("message", captionEditText.getText().toString());
startActivity(intent);

}
});
}
});

Also see:

  • URL generated by Firebase Storage does not allow access to the file
  • How to get download url for firebase storage and update firestore document?

Android Studio can't get Storage download URL

Could you try this:

fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Download URL
String url = uri.toString();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Errors
}
});

How to get download url for firebase storage and update firestore document?

Determining the download URL requires a call to the servers, which means it happens asynchronously. For this reason, any code that needs the download URL needs to be inside the onSuccess of getDownloadURL() or be called from there.

So:

private void getItemImageUrl(StorageReference reference) {
reference.getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
itemImageUrl = uri.toString();

... here you can write itemImageUrl to the database
}
});

}

Also see:

  • How to get the download url from Firebase Storage?

I cannot get download Url after upload a file to Firebase Cloud Storage in Android

task.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
UserModel usermodel = new UserModel();
usermodel.uesrName = name.getText().toString();
usermodel.ProfileImageURL = uri.toString();

Toast.makeText(RegisterActivity.this,uri.toString()
FirebaseDatabase.getInstance().getReference().child("user").child(uid).setValue(usermodel);
}
});

add on success listener to download url

Problem in download Imageuri in Firebase Storage Android

in uploadToFirebase method inside

reference.getDownloadUrl().addOnSuccessListener(uri -> {.....

instead of mImageUri.toString() put uri.toString() as per bellow.So you will get actual url and will solve your problem.

private void uploadToFirebase(Uri mImageUri) {
if (mImageUri != null) {
final ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setTitle("Uploading...");
progressDialog.show();

StorageReference reference = storageReference.child(System.currentTimeMillis()
+ "." + getFileExtension(mImageUri));
reference.putFile(mImageUri)
.addOnSuccessListener(taskSnapshot -> {
progressDialog.dismiss();
Toast.makeText(getActivity(), "Saved Succesfully", Toast.LENGTH_SHORT).show();
reference.getDownloadUrl()
.addOnSuccessListener(uri -> {
Upload upload = new Upload(uri.toString());
String uploadId = databaseReference.push().getKey();
databaseReference.child(uploadId).setValue(upload);
});
})
.addOnProgressListener(snapshot -> {
double progress = (100.0 * snapshot.getBytesTransferred() / snapshot
.getTotalByteCount());
progressDialog.setMessage("Uploaded " + (int) progress + "%");
})
.addOnFailureListener(e -> {
progressDialog.dismiss();
Toast.makeText(getActivity(), "Error Ocurred" + e.getMessage(), Toast.LENGTH_SHORT).show();
});

}
}

Firebase Storage getDownloadUrl() method can't be resolved

The Firebase API has changed.

May 23, 2018

Cloud Storage version 16.0.1

Removed the deprecated StorageMetadata.getDownloadUrl() and UploadTask.TaskSnapshot.getDownloadUrl() methods. To get a current download URL, use StorageReference.getDownloadUr().

UploadTask.TaskSnapshot has a method named getMetadata() which returns a StorageMetadata object.

This StorageMetadata object contains a method named getReference() which returns a StorageReference object.

That StorageReference object contains the getDownloadUrl() method, which now returns a Task object instead of an Uri object.

This Task must then be listened upon in order to obtain the Uri, which can be done asynchronously or in a blocking manner; see the Tasks API for that.



Related Topics



Leave a reply



Submit