我如何从我的库中加载视频文件?离子3

时间:2018-04-26 15:56:33

标签: ionic-framework ionic3 ionic-native ionic-view

我发现Ionic View不再支持原生文件see list here

我正在尝试使用Native Camera访问视频,从我的库中获取视频。它可以为我的视频返回3种不同格式的路径(DATA_URL,FILE_URI和NATIVE_URI)。reference to Native Camera here

我目前正在使用this post中建议的FILE_URI。它返回类似" /storage/emulated/0/DCIM/Camera/VID_20180312_210545.mp4"

请查看下面的代码。为了更好地理解,当前行为通过评论突出显示" // **评论***" :

addVideoToOffer(){        
    this.platform.ready().then(() =>{
        const options: CameraOptions = {
          sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
          destinationType: this.camera.DestinationType.FILE_URI,
          mediaType: this.camera.MediaType.VIDEO,
        }
        this.camera.getPicture(options).then((data_uri) => {       
          this.readVideoFileasGeneral(data_uri);
       });
    });

  }

  readVideoFileasGeneral(data_uri) {  
      if(!data_uri.includes('file://')) {
        data_uri = 'file://' + data_uri;  
      }             
      return this.file.resolveLocalFilesystemUrl(data_uri)
            .then((entry: FileEntry) => {
            //***it does not get in here***
            this.presentQuickToastMessage(data_uri); 
            return new Promise((resolve)=>{//, reject) => { 
                        entry.file((file) => {    
                                let fileReader = new FileReader();  
                                fileReader.onloadend = () => {
                                      let blob = new Blob([fileReader.result], {type: file.type});
                                      resolve({blob: blob, file: file});    
                                };
                                fileReader.readAsArrayBuffer(file); 
                              });  
                        })
            })
            .catch((error) => { 
              this.presentQuickToastMessage(error); 
              //***it presents "plugin_not_installed" here***
            });
  }

我知道我收到此消息,因为不再支持本机文件(可能是plugin_not_installed消息的原因)。但是,我仍然需要完成这项任务。所以,如果有人知道我可以使用什么来将所选视频放在blob中,那就太棒了!

感谢阅读,直到这里, 干杯, Roger A L

1 个答案:

答案 0 :(得分:0)

 makeFileIntoBlob(uri) {
    // get the correct path for resolve device file system
    let pathIndex = uri.indexOf('var');
    let correctPath = uri.slice(+pathIndex);

    this.file
        .resolveLocalFilesystemUrl((this.platform.is('ios') ? 'file:///' : '') + correctPath)
        .then(entry => (<FileEntry>entry).file(file => this.readFile(file)))
        .catch(err => console.log('ERROR: ', err));
}

readFile(file) {
    if(file) {
        const reader = new FileReader();
        reader.onloadend = () => {
            const blob: any  = new Blob([reader.result], { type: file.type });
            blob.name = file.name;
            console.log(blob);

            return blob;
        };
        reader.readAsArrayBuffer(file);
    }
}

您需要摆脱/ private /并保留file:///,以便路径类似于file:/// var /