firestore 数据库将图像添加到记录 [英] firestore database add image to record

查看:35
本文介绍了firestore 数据库将图像添加到记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 jpeg 图像添加到 firestore db 记录中.我的首选方式是在 Blob 中,就像我在 Sqlite 记录中使用的那样.我在尝试这个时遇到了一些问题.以下是我迄今为止最好的尝试:

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:

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;
}

我的问题出在
blob = fromBase64String(wBlob);

My problem is in the
blob = fromBase64String(wBlob);

我在将 blob 传递给设置记录的类时也遇到了问题.intent.putExtra("blobImage", blob );感谢您的帮助.

I also have a problem with passing a blob to the class that sets the record. intent.putExtra("blobImage", blob ); I appreciate any help.

推荐答案

作为一般规则,除非您存储相当小的图标,否则我会说不要这样做".

As a general rule, unless you're storing fairly small icons, I'd say, "Don't do this."

虽然您可以在 Cloud Firestore 中存储本机二进制数据(您实际上不需要对图像进行 base64 编码),但您将遇到数据库中的一些限制——特别是单个文档不能超过 1MB.

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 Storage for Firebase 中,然后将下载 URL 保留在 Cloud Firestore 中.Cloud Storage 在存储成本方面大约便宜 7 倍,在最大文件大小方面有更大的限制,通过抓取这些下载 URL,您可以利用像 Picasso 和 Glide 这样可以管理图像下载的第三方库您,同时还添加了一些漂亮的功能,例如内存或磁盘缓存或显示占位符图形.

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.

您可能想要查看此视频以了解更多信息.

You might want to check out this video for more information.

这篇关于firestore 数据库将图像添加到记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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