从相机胶卷中选择图像和gif

时间:2018-11-14 01:50:45

标签: javascript cordova ionic-framework ionic2 ionic3

我正在使用Angular和Firebase构建一个Ionic应用程序。

我希望能够将图像和gif上传到我的Firebase数据库,但是我只能使image正常工作。而且,我不要视频。

我的代码如下:

takePhoto(sourceType:number) {
    const options: CameraOptions = {
      quality: 40,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      correctOrientation: true,
      sourceType:sourceType,
    }

    this.camera.getPicture(options).then((imageData) => {
      let base64Image = 'data:image/jpeg;base64,' + imageData;
      this.uploadToStorage(base64Image);
    }, (err) => {
      // Handle error
    });
  }

  uploadToStorage(src) {
    this.uploadProgress = true;
    let storageRef = firebase.storage().ref();
    // Create a timestamp as filename
    this.imageFileName = Math.floor(Date.now() / 1000) + "_" + this.userData.uid;
    // Create a reference to 'images/todays-date.jpg'
    const imageRef = storageRef.child('posts/'+this.imageFileName+'.jpg');
    imageRef.putString(src, firebase.storage.StringFormat.DATA_URL).then((snapshot)=> {
      snapshot.ref.getDownloadURL().then(downloadURL => {
        this.imageURL = downloadURL;
        this.uploadProgress = false;
        this.uploadSuccess = true;
        console.log(this.imageURL)
        this.logEvent("Uploaded Image");
      });
    }, (err) => {
      console.log(err)
    });
  }

但这仅允许静止图像。根据{{​​3}}的文档 您可以将mediaType: this.camera.MediaType.PICTURE更改为mediaType: this.camera.MediaType.ALLMEDIA,但这对我不起作用。当我在计算机上进行测试时,它可以工作,但在iOS或Android上却不能。

有什么想法可以允许我选择图片和GIF?谢谢!

1 个答案:

答案 0 :(得分:0)

photos ; 
 OpenGallery(){
      console.log("taktit");
      const options: CameraOptions = {
        quality: 100,
        destinationType: this.camera.DestinationType.DATA_URL,
        sourceType: this.camera.PictureSourceType.SAVEDPHOTOALBUM 
      }

      this.camera.getPicture(options).then((imageData) => {
       // imageData is either a base64 encoded string or a file URI
       // If it's base64:
       console.log("taktit");
       let base64Image = 'data:image/jpeg;base64,' + imageData;
       this.photos.push(base64Image);
       this.photos.reverse();

      }, (err) => {
       // Handle error
      });  else {    }
    }
  • 现在您可以将对象照片上传到Firebase