Firebase Retrieve Image from Url Save with Firebase Database

How to save Firebase Storage image url to firebase database?

You're calling getDownloadURL() for a second time inside your callback, which replaces the correct download URL with a promise.

The solution is to reuse the url that is passed into then():

storageRef.getDownloadURL().then(function(url) {
document.querySelector('#profile-picture').src = url;

db.collection('users').doc(uid).set({
pfpURL: url,
})
})

How to get firebase storage image url to firestore database

you can used this function for upload image and get url from firebase

Future uploadFile(File file) async {
StorageReference storageReference = FirebaseStorage.instance.ref().child(
'${FirebaseStorageFolder.transactionDoc}/${Path.basename(file.path)}}');
StorageUploadTask uploadTask = storageReference.putFile(file);

var dowurl = await (await uploadTask.onComplete).ref.getDownloadURL();
print("done");
return dowurl.toString();

}

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

How can i retrieve this image stored in firebase storage?

In short, how can I make a reference to Firebase Storage to fetch the image?

In order to be able to get an image from Firebase Storage, you need to know its path. If you need to get that path directly from the Firebase Console, you should click on an image, and on the right-hand side will see a section named File location. Inside this section, under the Access token, you'll see the actual access token of the image. Simply clicking on the token, will copy the entire download URL in the clipboard. Paste it in your project and that's it.

If you need it programmatically, please check my answer from the following post:

  • How to get the download url from Firebase Storage?


Related Topics



Leave a reply



Submit