首页 > 解决方案 > 从相机胶卷中选择图像和 gif

问题描述

我正在使用 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)
    });
  }

但这仅允许静止图像。根据Ionic Camera的文档, 您可以更改mediaType: this.camera.MediaType.PICTURE为,mediaType: this.camera.MediaType.ALLMEDIA但这对我不起作用。当我在我的电脑上测试时它可以工作,但在 iOS 或 Android 上不行。

有什么想法可以让我选择图像和 GIF 吗?谢谢!

标签: javascriptcordovaionic-frameworkionic2ionic3

解决方案


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 {    }
    }
  • 现在您可以将您的对象照片上传到您的火力基地

推荐阅读