Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java firestore数据库将图像添加到记录_Java_Blob_Jpeg_Google Cloud Firestore - Fatal编程技术网

Java firestore数据库将图像添加到记录

Java firestore数据库将图像添加到记录,java,blob,jpeg,google-cloud-firestore,Java,Blob,Jpeg,Google Cloud Firestore,我需要将jpeg图像添加到firestore db记录中。我更喜欢的方式是在一个Blob中,就像我在Sqlite记录中使用的那样。我在尝试这个过程中遇到了一些问题。以下是我迄今为止最好的尝试: public Blob getImage() { Uri uri = Uri.fromFile(new File(mCurrentPhotoPath)); File f1 = new File(mCurrentPhotoPath); URL myURL = null; t

我需要将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;
}
我的问题在
blob=fromBase64String(wBlob)

我在向设置记录的类传递blob时也有问题。 意向。额外(“blobImage”,blob);
非常感谢您的帮助。

一般来说,除非您存储的是相当小的图标,否则我会说,“不要这样做。”

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

相反,我会将图像本身存储在Firebase的云存储中,然后将下载URL保存在CloudFireStore中。云存储在存储成本方面大约便宜7倍,在最大文件大小方面有更大的限制,通过获取这些下载URL,您可以利用第三方库,如毕加索和Glide,为您管理图像下载,同时还添加了一些漂亮的功能,如内存或磁盘缓存或显示占位符图形


您可能想查看更多信息。

Hi Todd。这是一个云Firestore问题,而不是RTDB。你的答案是否也代表这一点?我正在为我的一个应用程序将base64编码文件存储在Cloud Firestore中,因为它比Firebase的云存储允许更好的安全规则控制。对不起,刚刚更新了我对Cloud Firestore的回答。(长话短说,我还是建议图像使用云存储。这不是价格问题,而是“您可能会遇到最大文档大小”问题。)谢谢,托德。对于我正在处理的数据,从未超过1Mb的限制。我希望有一天,云存储安全规则能够查询RTDB/Cloud Firestore中存储的值。谢谢你的建议。我目前将图像存储在Firebase存储中,并将路径存储在文档中。它工作得很好。我想如果我能离开博客,它会更紧凑,但我的直觉正是你所建议的。