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

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

问题描述

我需要将jpeg图像添加到Firestore数据库记录中.我首选的方式是在Sqlite记录中使用的Blob.我在尝试这种方法时遇到了一些问题.以下是到目前为止我最好的尝试:

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,您可以利用Picasa和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天全站免登陆