【问题标题】:Image not saved as jpeg in firebase storage图像未在 Firebase 存储中保存为 jpeg
【发布时间】:2020-12-04 09:57:39
【问题描述】:

我目前正在从事一个使用 cordova 相机的离子项目。我可以拍摄图像,存储在离子存储中,也可以上传到 Firebase 存储。但是,图像不会保存为jpeg,而是保存为application/octet-stream

  takePicture() {
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    }

    this.camera.getPicture(options).then((imageData) => {
      console.log(imageData)
      // Add new photo to gallery
      this.photos.unshift({
        data: 'data:image/jpeg;base64,' + imageData
      });

      // Save all photos for later viewing
      this.storage.set('photos', this.photos);
      //save to firebase storage
      const storageRef = firebase
        .storage()
        .ref('photos/img.jpeg')
      storageRef.putString(imageData, 'base64'), {
        contentType: 'image/jpeg'
      }


    }, (err) => {
      // Handle error
      console.log("Camera issue: " + err);
    });
  }

【问题讨论】:

    标签: javascript ionic-framework base64 cordova-plugins firebase-storage


    【解决方案1】:

    根据 API 文档,putString() 采用三个参数:

    参数

    • 数据:字符串

    要上传的字符串。

    • 可选格式:StringFormat

    要上传的字符串的格式。

    • 可选元数据:UploadMetadata

    新上传对象的元数据。

    第三个参数是您指定内容类型的位置。现在,您正在传递两个参数,而内容类型不是其中的一部分。看起来你的右括号放错了地方:

    storageRef.putString(imageData, 'base64', {
        contentType: 'image/jpeg'
    });
    

    【讨论】:

    • 谢谢,Doug,正如你所提到的,错误是由于右括号造成的。由于三个参数中的两个是可选的,因此我能够摆脱我的两个参数。
    猜你喜欢
    • 2018-06-05
    • 2017-08-10
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    相关资源
    最近更新 更多