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
How to Make JSON Data Persistent for Offline Use (Swift 4)
Shared Cookies with Wkprocesspool for Wkwebview in Swift
How to Get a Double Value Up to 2 Decimal Places
Swift Enumeration Order and Comparison
Swiftui, Shadow Only for Container
Iterating Over an Nsorderedset
Calculate Time Difference in Swift 4
How to Use Socket in Swift (Connect, Send and Get Message)
Swift Alternative to Respondstoselector:
Realitykit - How to Edit or Add a Lighting
How to Use a Value Type Object as a Reference Type
Using Custom Cifilter on Calayer Shows No Change to Calayer
How to Create a Nsmutabledictionary in Swift
Can a Subclass Override a Function and Make More Restrictive Return
Search Multiple Words in One String in Swift
How to Duplicate a Sprite in Sprite Kit and Have Them Behave Differently