代码之家  ›  专栏  ›  技术社区  ›  Simon

从相机卷中选择图像和gif

  •  0
  • Simon  · 技术社区  · 6 年前

    我正在建立一个离子应用程序与角度和firebase。

    我想把图片和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上不行。

    有什么办法可以让我的图片和礼品被选中吗?谢谢您!

    1 回复  |  直到 6 年前
        1
  •  0
  •   Youssef Dahmane Jdid    6 年前
    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