Firebase Storage 如何存储和检索图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13955813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:35:57  来源:igfitidea点击:

Firebase Storage How to store and Retrieve images

firebasefirebase-realtime-databasefirebase-storage

提问by dogger

How to store and view images on firebase?

如何在firebase上存储和查看图像?

回答by Michael Lehenbauer

Update (20160519): Firebase just released a new feature called Firebase Storage. This allows you to upload images and other non-JSON data to a dedicated storage service. We highly recommend that you use this for storing images, instead of storing them as base64 encoded data in the JSON database.

更新 (20160519)Firebase 刚刚发布了一项名为Firebase Storage的新功能。这允许您将图像和其他非 JSON 数据上传到专用存储服务。我们强烈建议您使用它来存储图像,而不是将它们作为 base64 编码数据存储在 JSON 数据库中。

You certainly can! Depending on how big your images are, you have a couple options:

你当然可以!根据您的图像有多大,您有几个选择:

1. For smaller images (under 10mb)

1.对于较小的图像(10mb以下)

We have an example project that does that here: https://github.com/firebase/firepano

我们在这里有一个示例项目:https: //github.com/firebase/firepano

The general approach is to load the file locally (using FileReader) so you can then store it in Firebase just as you would any other data. Since images are binary files, you'll want to get the base64-encoded contents so you can store it as a string. Or even more convenient, you can store it as a data: urlwhich is then ready to plop in as the src of an img tag (this is what the example does)!

一般的方法是在本地加载文件(使用 FileReader),这样您就可以像存储任何其他数据一样将其存储在 Firebase 中。由于图像是二进制文件,因此您需要获取 base64 编码的内容,以便将其存储为字符串。或者更方便的是,您可以将它存储为data: url,然后准备作为 img 标签的 src 插入(这就是示例所做的)!

2. For larger images

2.对于更大的图像

Firebase does have a 10mb (of utf8-encoded string data) limit. If your image is bigger, you'll have to break it into 10mb chunks. You're right though that Firebase is more optimized for small strings that change frequently rather than multi-megabyte strings. If you have lots of large static data, I'd definitely recommend S3 or a CDN instead.

Firebase 确实有 10mb(utf8 编码的字符串数据)限制。如果您的图像更大,则必须将其分成 10 mb 的块。您是对的,尽管 Firebase 更针对频繁更改的小字符串而不是多兆字节字符串进行了优化。如果您有大量大型静态数据,我肯定会推荐 S3 或 CDN。

回答by user15163

Yes, you can store and view images in Firebase. You can use a filepicker to get the image file. Then you can host the image however you want, I prefer Amazon s3. Once the image is hosted you can display the image using the URL generated for the image.

是的,您可以在 Firebase 中存储和查看图像。您可以使用文件选择器来获取图像文件。然后您可以随意托管图像,我更喜欢 Amazon s3。托管图像后,您可以使用为图像生成的 URL 显示图像。

Hope this helps.

希望这可以帮助。

回答by Grendel2501

I ended up storing the images in base64 format myself. I translate them from their base64 value when called back from firebase.

我最终自己以 base64 格式存储图像。当从 firebase 回调时,我从它们的 base64 值转换它们。

回答by Felice

You can also use a service called Filepickerwhich will store your image to their servers and Filepickerwhich is now called Filestack, will provide you with a url to the image. You can than store the url to Firebase.

你也可以使用一个名为服务Filepicker,将您的图像保存到他们的服务器和Filepicker现在被称为Filestack,将为您提供一个网址的形象。您可以将 url 存储到 Firebase。

回答by Jakob Hartman

There are a couple of ways of doing I first did the way Grendal2501 did it. I then did it similar to user15163, you can store the image URL in the firebase and host the image on your firebase host or also Amazon S3;

有几种方法可以做我首先做的方式 Grendal2501 做的。然后我做了类似于 user15163 的操作,您可以将图像 URL 存储在 firebase 中,并将图像托管在您的 firebase 主机或 Amazon S3 上;

回答by Asthme

Using Base64 string in JSON will be very heavy. The parser has to do a lot of heavy lifting. Currently, Fresco only supports base supports Base64. Better you put something on Amazon Cloud or Firebase Cloud. And get an image as a URL. So that you can use Picasso or Glide for caching.

在 JSON 中使用 Base64 字符串将非常繁重。解析器必须做很多繁重的工作。目前,Fresco 仅支持 Base64。最好在 Amazon Cloud 或 Firebase Cloud 上放一些东西。并获取图像作为 URL。这样您就可以使用 Picasso 或 Glide 进行缓存。