Harish
Harish

Reputation: 61

Image not saved as jpeg in firebase storage

I am currently working on an ionic project which uses the camera from cordova. I am able to take the image, store in ionic storage and also upload to firebase storage. However, the image is not saved as jpeg and instead in 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);
    });
  }

Upvotes: 1

Views: 573

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317322

According to the API documentation, putString() takes three arguments:

Parameters

  • data: string

The string to upload.

  • Optional format: StringFormat

The format of the string to upload.

  • Optional metadata: UploadMetadata

Metadata for the newly uploaded object.

The third argument is where you specify the content type. Right now, you are passing two arguments, and the content type isn't part of that. It looks like you have the closing parenthesis in the wrong place:

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

Upvotes: 1

Related Questions