firebase storage - getting image URL

exoslav picture exoslav · Jul 17, 2016 · Viewed 68.6k times · Source

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)?

Answer

Tanishq Sharma picture Tanishq Sharma · Jul 17, 2016

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.