首页 > 解决方案 > Ionic Camera 生成的字符串 URL 太大而无法在 Firebase 中存储(作为字符串)

问题描述

我正在使用离子相机插件https://ionicframework.com/docs/native/camera来捕获图像并存储到 Firebase 中,但是当我上传大小大于 2 mb 的图像文件时,同时存储在 Firebase 中它会给字符串长度超过 1048487 字节的错误。任何建议我如何缩短字符串 URL 大小或任何其他存储任何蚂蚁大小图片的方式。

我的代码:

takePicture()
 {

   const options: CameraOptions = {
   quality: 30,
   destinationType: this.camera.DestinationType.DATA_URL,
   encodingType: this.camera.EncodingType.JPEG,
   mediaType: this.camera.MediaType.PICTURE,
   allowEdit: true,
   sourceType: 1,
   saveToPhotoAlbum: false,
};
   this.camera.getPicture(options).then((imageData) => {
   this.cardopen = !this.cardopen;
   this.takephoto = 'data:image/jpeg;base64,' + imageData;
console.log(this.takephoto);
}, (err) => {
  console.log(err);
});
}

歌剧 DOM:

core.js:4197 ERROR Error: Uncaught (in promise): FirebaseError: [code=invalid-argument]: The value of 
 property "ImageUrl" is longer than 1048487 bytes.
FirebaseError: The value of property "ImageUrl" is longer than 1048487 bytes.

字符串图像

标签: ionic-frameworkionic-native

解决方案


您应该先将图像上传到 Firebase 存储。之后,您将下载 url 保存在您的 firestore 文档中以显示图像。
上传到 Firebase 存储

使用 Firebase 存储,您对数据大小没有限制。缺点是图像加载缓慢,但您可以创建一个 firebase 函数来执行一些图像处理以生成缩略图。

即使为这个 firebase 创建了一个扩展来调整图像大小。


推荐阅读