Firebase存储 - 获取图片网址 [英] firebase storage - getting image URL

查看:128
本文介绍了Firebase存储 - 获取图片网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始为后台使用Firebase(Firebase for Web)。我可以很好地处理Firebase实时数据库,但是我不清楚Firebase存储(用于图像,视频等)是如何工作的。

基本上我想在我的网站上有一些新闻列表,每个项目都有一些小图片。所以我上传这些图片到Firebase存储,但是我怎样才能访问他们的URL(需要它为我的< img /> src属性)?

解决方案

Firebase存储在提取数据时的工作方式类似。虽然有不同的方式来检索。最重要的因素之一就是如何保存数据。

这里:

 <$ c $ getDownloadUrl()。addOnSuccessListener(new OnSuccessListener< Uri>(){
@Override
public void onSuccess(Uri uri){
//获取'users / me / profile.png'
}
}的下载网址addOnFailureListener(new OnFailureListener(){
@Override
public void onFailure(@NonNull异常异常){
//处理任何错误
}
});

//另外获得下载URL的方法
storageRef.child(users / me / profile.png)。getDownloadUrl()。getResult();

的位置非常重要,应该是您的登录。



例如在新闻结构中,可以是:

pre $ storageRef.child(newsID / articleimage / image.jpg)

newsID - 每篇文章的唯一ID 。当您创建文章以获取单独的图像时,重要!
,那么image.jpg可以按照下面的说明进行下载:文档。


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

解决方案

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.

这篇关于Firebase存储 - 获取图片网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆