java firestore 数据库将图像添加到记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49265931/
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
firestore database add image to record
提问by rosey
I need to add jpeg images to firestore db records. My preferred way would be in a Blob as I have used in Sqlite records. I have run into some problems in trying this. The following is my best try so far:
我需要将 jpeg 图像添加到 firestore db 记录中。我的首选方式是在 Blob 中,就像我在 Sqlite 记录中使用的那样。我在尝试这个时遇到了一些问题。以下是我迄今为止最好的尝试:
public Blob getImage() {
Uri uri = Uri.fromFile(new
File(mCurrentPhotoPath));
File f1 = new File(mCurrentPhotoPath);
URL myURL = null;
try {
myURL = f1.toURI().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
Bitmap bitmap = null;
BitmapFactory.Options options = new
BitmapFactory.Options();
options.inPreferredConfig =
Bitmap.Config.ARGB_8888;
try {
if (myURL != null) {
bitmap =
BitmapFactory.decodeStream(
myURL.openConnection().getInputStream());
String wBlob =
encodeToBase64(bitmap,
Bitmap.CompressFormat.JPEG, 100);
blob = fromBase64String(wBlob);
}
} catch (java.io.IOException e) {
e.printStackTrace();
}
return blob;
}
My problem is in the
blob = fromBase64String(wBlob);
我的问题是在
blob = fromBase64String(wBlob);
I also have a problem with passing a blob to the class that sets the record. intent.putExtra("blobImage", blob ); I appreciate any help.
我也有将 blob 传递给设置记录的类的问题。intent.putExtra("blobImage", blob ); 我很感激任何帮助。
回答by Todd Kerpelman
As a general rule, unless you're storing fairly small icons, I'd say, "Don't do this."
作为一般规则,除非您存储相当小的图标,否则我会说“不要这样做”。
While you can store native binary data in Cloud Firestore (you don't really need to base64-encode your image), you're going to run up against some of the limits in the database -- specifically, a single document can't be more than 1MB large.
虽然您可以在 Cloud Firestore 中存储本机二进制数据(您实际上不需要对图像进行 base64 编码),但您将遇到数据库中的一些限制——特别是单个文档不能大于 1MB。
Instead, I would store the images themselves in Cloud Storage for Firebase and then just keep the download URLs in Cloud Firestore. Cloud Storage is approximately 7x cheaper in terms of storage costs, has much larger limits in terms of maximum file size, and by grabbing these download URLs, you can take advantage of third-party libraries like Picasso and Glide that can manage the image downloading for you, while also adding nifty features like memory or disk caching or showing placeholder graphics.
相反,我会将图像本身存储在 Cloud Storage for Firebase 中,然后将下载 URL 保留在 Cloud Firestore 中。Cloud Storage 在存储成本方面大约便宜 7 倍,在最大文件大小方面有更大的限制,通过抓取这些下载 URL,您可以利用像 Picasso 和 Glide 这样可以管理图像下载的第三方库您,同时还添加了漂亮的功能,如内存或磁盘缓存或显示占位符图形。
You might want to check out this videofor more information.
您可能想查看此视频以了解更多信息。