This question already has an answer here:
getDownloadURL isn't inputting the link i need
(1 answer)
Closed 3 years ago.
I am trying to access the actual URL of uploaded image on firebase, but not getting.
Look at my code below:
String ActualImageUrl = null;
taskSnapshot.getMetadata().getReference().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
{
#Override
public void onSuccess(Uri uri)
{
ActualImageUrl = uri.toString();
Log.d("okok","url inside: "+ActualImageUrl);
}
});
Log.d("okok","url ouside: "+ActualImageUrl);
okok: url inside: ACTUAL URL HERE
okok: url outside: null
From Firebase Official guide use this code to get download url
final StorageReference ref = storageRef.child("images/mountains.jpg");
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
// ...
}
}
});
Related
This question already has an answer here:
How to get the download url from Firebase Storage?
(1 answer)
Closed 4 years ago.
Try to get URL of image uploaded in Firebase Storage, using this docs, but there's no info about how to return downloadUri value from Task.continueWithTask method.
Even method documentation doesn't helped to understand.
Quote from docs, there's no explanation how to get this URL:
After uploading a file, you can get a URL to download the file by calling the getDownloadUrl() method on the StorageReference:
final StorageReference ref = storageRef.child("images/mountains.jpg");
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
// ...
}
}
});
I haven't used Firebase for a while, but the way I used to do it is to assign either a SuccessListener or OnCompleteListener after calling ref.getDownloadUrl()
Example
ref.getDownloadUrl().addOnSuccessListener(url -> {
...
});
To retrieve the URL of the image or the data you have stored in firestore you should use OnSuccessListener
Like
firepath.putFile(resultUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
firepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
final String url = uri.toString();
}});});
Here string URL will have the link of the image or the video
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
// ...
}
}
});
getDownloadUrl() method is deprecated after firebase update. What is the alternative code for this method?
public void createOrUpdatePostWithImage(Uri imageUri, final OnPostCreatedListener onPostCreatedListener, final Post post) {
// Register observers to listen for when the download is done or if it fails
DatabaseHelper databaseHelper = ApplicationHelper.getDatabaseHelper();
if (post.getId() == null) {
post.setId(generatePostId());
}
final String imageTitle = ImageUtil.generateImageTitle(UploadImagePrefix.POST, post.getId());
UploadTask uploadTask = databaseHelper.uploadImage(imageUri, imageTitle);
if (uploadTask != null) {
uploadTask.addOnFailureListener(exception -> {
// Handle unsuccessful uploads
onPostCreatedListener.onPostSaved(false);
}).addOnSuccessListener(taskSnapshot -> {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Uri downloadUrl = taskSnapshot.getDownloadUrl();
LogUtil.logDebug(TAG, "successful upload image, image url: " + String.valueOf(downloadUrl));
post.setImagePath(String.valueOf(downloadUrl));
post.setImageTitle(imageTitle);
createOrUpdatePost(post);
onPostCreatedListener.onPostSaved(true);
});
}
}
Use this code I to get the ImagesUrl.
I hope it works!
I also got a repository on github for this, here is the link.
https://github.com/BVLD/Firebase-Storage-upload-and-retreive-images-android-studio
ImagesPath.putFile(mainImageURI).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 ImagesPath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downUri = task.getResult();
downUri.toString();
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.
This question already has answers here:
Firebase Storage "retrieves a long lived download URL" using getDownloadUrl() now deprecated
(3 answers)
Closed 4 years ago.
I am trying to get download url from firebase but it gives me some another link like "com.google.android.gms.tasks.zzu#3689a168" instead of the earlier link which it used to provide me before Firebase upgrade.This issue was not there before upgrading firebase, I am trying to get a link like the following "https://firebasestorage.googleapis.com/v0/b/lbsny-1a3d9.appspot.com/o/pOa47ktSz7ZRFVeykiz63bsm1Hu2%2F1523658820692.jpg?alt=media&token=c2fe1008-bb4a-47f2-9da7-82a3298a55da"
My code for upload image
mStorageRef = FirebaseStorage.getInstance().getReference().child(current_uid).child(System.currentTimeMillis() + ".jpg" );
mStorageRef.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
#Override
public void onComplete(#NonNull Task<UploadTask.TaskSnapshot> task) {
Task<Uri> downloadUri=mStorageRef.getDownloadUrl();
String imageUrl = mStorageRef.getDownloadUrl().toString();
mDataRef = FirebaseDatabase.getInstance().getReference();
Map map = new HashMap();
map.put("imageUrl",imageUrl);
map.put("price",TempImageName);
map.put("caption",TempImageCaption);
map.put("time", ServerValue.TIMESTAMP);
ItemUploadInfo itemUploadInfo = new ItemUploadInfo(TempImageCaption,TempImageName,mStorageRef.getDownloadUrl().toString());
String ImageUploadId = mDataRef.push().getKey();
mDataRef.child("Items").child(current_uid).child(ImageUploadId).setValue(map);
}
});
According to the docs, After uploading a file, you can get a URL to download the file by calling the getDownloadUrl() method on the StorageReference:
final StorageReference ref = storageRef.child("images/mountains.jpg");
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
// ...
}
}
});