How do I get Download URL from StorageReference - android

In my project, I am able to getDownloadUrl() after uploading a file
ref.putFile(imgUri).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
#Override
public Task<Uri> then(#NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
if(task.getException() != null) {
throw task.getException();
}
}
return ref.getDownloadUrl();
}
}) ...
However, since I am resizing the image with a cloud function and re-uploading it to the bucket, the download url will change, so the only way to know the location of the image is through a StorageReference. How can I getDownloadUrl() from StorageReference? I've tried ref.getDownloadUrl().addOnSuccessListener which doesn't get fired and ref.getDownloadUrl().addOnCompleteListener and the task.getResult() throws an error.

Try using this :
I was trying to store images
You can get the download URL from task.getResult().toString()
final StorageReference ref = FirebaseStorage.getInstance().getReference().child("images").child(imageName)
uploadTask = ref.putBytes(data);
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
#Override
public Task<Uri> then(#NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// 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()) {
Uri downloadUri = task.getResult();
String URL = task.getResult().toString();
} else {
// Handle failures
// ...
}
}
});

Related

What are the two tasks in the code to upload file to firebase storage?

According to the document Upload Files on Android, this is the code to upload file to Firebase Storage and retrieve the download uri:
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
#Override
public Task<Uri> then(#NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// 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()) {
Uri downloadUri = task.getResult();
} else {
// Handle failures
// ...
}
}
});
As you can see from the code, there are two tasks in the code, Task<UploadTask.TaskSnapshot> task on continueWithTask method and Task<Uri> on addOnCompleteListener method. I want to know which task I have to check for success to make sure that file uploaded successfully?
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
#Override
public Task<Uri> then(#NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// 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()) {
-> **This is the Success Case for you**
Uri downloadUri = task.getResult();
} else {
-> **In this case File Uploaded Successfully But You failed to get its URL,
Again send this call again with storage reference. No need to send other
call to upload the file again because its uploaded already on FireStore.**
}
}
});

How to Upload and Retrieve image in latest firebase storage after update

I am trying to upload and retrieve the image and follow all the results available at Stack and tutorials but none of example help me.
how to upload an image on button click in latest firebase storage
I am following this tutorial
https://www.simplifiedcoding.net/firebase-storage-example/
StorageReference reference= storageReference.child(System.currentTimeMillis()+ "."+getFileExtension(filePath));
mUploadTask= reference.putFile(filePath);
Task<Uri> urlTask = mUploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
#Override
public Task<Uri> then(#NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// Continue with the task to get the download URL
return storageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
} else {
// Handle failures
// ...
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
You are doing it right to upload the image, after you return the storageReference.getDownloadUrl(), just set that value into your firebase database.
StorageReference reference= storageReference.child(System.currentTimeMillis()+ "."+getFileExtension(filePath));
mUploadTask= reference.putFile(filePath);
Task<Uri> urlTask = mUploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
#Override
public Task<Uri> then(#NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// Continue with the task to get the download URL
return storageReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
mDatabaseRef.child("images").child("imageUrl").setValue(downloadUri.toString());
} else {
// Handle failures
// ...
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
You will need to request that image url String from firebase and after that, to show the image in any ImageView use Picasso or Glide to load it.

How can I get the download URL in String from firebase storage inside "addOnSuccessListener"

This is my approach to upload an image file and getting the download link
UploadTask uploadTask = profileImageRef.putBytes(data);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String fileUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
Toast.makeText(UserProfile.this, "Uploaded Successfully!", Toast.LENGTH_SHORT).show();
}});
When I logged fileUrl it displays a String like this
com.google.android.gms.tasks.zzu#6ee8ba8
Someone please give me a solution to get the download url.
you can use this,
UploadTask uploadTask = profileImageRef.putBytes(data);
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
#Override
public Task<Uri> then(#NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// Continue with the task to get the download URL
return profileImageRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
} else {
// Handle failures
// ...
}
}
});

Firebase new version getDownloadUrl in picasso not working

saveUrlToCategory(categoryIdSelect,taskSnapshot.getDownloadUrl().toString(),textname, textdescription);
getDownlaodUrl is not working. I need to save this in a firebase database and than display it in picasso. How can I get this working?
UploadTask uploadTask = FirebaseStorage.getInstance().getReference().child(user.getUid()).child("avatar").child(avatarName)
.putFile(uri);
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
#Override
public Task<Uri> then(#NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return FirebaseStorage.getInstance().getReference().child(user.getUid()).child("avatar").child(avatarName).getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
String downloadUrl = downloadUri.toString();
childRef.child("avatar")
.setValue(downloadurl);
// Or in your case it is:
saveUrlToCategory(categoryIdSelect,downloadUrl,textname, textdescription);
} else {
Log.e("FirebaseError:", "Error");
}
}
});

Is StorageReference.getDownloadUrl() declared inside uploadTask.addOnSuccessListener?

As referenced in firebase doc, I have to call getDownloadUrl() on the StorageReference after uploading file. So the task should be declared inside addOnSuccessListener?
final StorageReference ref = storageRef.child("your_REF");
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 {
if (!task.isSuccessful()) {
throw task.getException();
}
// 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()) {
Uri downloadUri = task.getResult();
} else {
// Handle failures
// ...
}
}
});
As far as I know the StorageReference.getDownloadUrl() can be called before the upload has finished, or even has been started. Of course the file at the download URL won't be available until the upload has been completed, so you'll want a flagging mechanism for that in completion listener for the upload task.

Categories

Resources