28

I have started to use firebase (Firebase for web) for my backend. I can handle Firebase Realtime Database quite well, but I do not clearly understand how Firebase Storage (for images, videos etc.) works.

Basically I would like to have some list of news on my website each item with some small picture. So I uploaded these pictures to Firebase Storage, but how can I access their URL (need it for my <img/> src attribute)?

exoslav
  • 2,094
  • 3
  • 21
  • 26
  • Did you check this page of the documentation? https://firebase.google.com/docs/storage/android/download-files#download_data_via_url – Frank van Puffelen Jul 17 '16 at 17:58
  • Or are you asking how to get a list of all images in your Firebase Storage bucket? Because there is currently no API for that. See http://stackoverflow.com/questions/37335102/how-to-get-an-array-with-all-pictures – Frank van Puffelen Jul 17 '16 at 18:10
  • My idea was to get list of images in specific folder and then get URL of each image and use it for my tag. One more thing, I am using Firebase for web, not for android, I suppose I should wrote it, so sorry about that. – exoslav Jul 17 '16 at 18:17
  • Web docs: https://firebase.google.com/docs/storage/web/download-files#download_data_via_url. There is no API to list images in a folder with Firebase Storage. You should store the URLs in a separate mechanism, such as the Firebase Database. – Frank van Puffelen Jul 17 '16 at 18:54

1 Answers1

14

Firebase Storage works in a similar way when extracting data. Though there are different ways to retrieve. One of the most important factor is how you save your data. According to which you will be able to retrieve them.

Here:

storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        // Got the download URL for 'users/me/profile.png'
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});

// Alternatively way to get download URL
storageRef.child("users/me/profile.png").getDownloadUrl().getResult(); 

The location of child is very crucial and that should be a part of your login.

For example in a news structure, it can be:

storageRef.child("newsID/articleimage/image.jpg")

newsID - Unique id for each article. IMPORTANT! when you create your article in order to get separate images. and that, "image.jpg" can be downloaded various ways as explained here: Documentation.

Tanishq Sharma
  • 538
  • 2
  • 14
  • Thank you, it helped although it is for Android! I am now able to get URL of specific image. But can I get reference to folder with multiple images and then loop the folder and get URL of each image? Is that posible? – exoslav Jul 17 '16 at 18:32
  • Definitely, provided you know each file name. Then you can simply create a logic to download one after another. – Tanishq Sharma Jul 17 '16 at 18:33
  • How do I get the url of the last image uploaded? so for example I upload an image then while the image is uploaded in the success method I can get this reference and store it in another table? – Smac Aug 26 '17 at 16:12
  • 1
    @Smac how this answer isn't in the first paragraph of the google docs is beyond me. Pathetic really. – A.com May 19 '19 at 12:50