firestore数据库添加图像进行记录

ros*_*sey 5 java jpeg blob 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;
    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;
}
Run Code Online (Sandbox Code Playgroud)

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

将blob传递给设置记录的类也有问题.intent.putExtra("blobImage",blob); 我感谢任何帮助.

Tod*_*man 23

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

虽然你可以在云存储的FireStore原始二进制数据(你并不真正需要的base64编码图像),你要对一些在数据库中的极限跑起来 - 特别是,一个单一的文件不能大于1MB.

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

您可能需要查看此视频以获取更多信息.