pick video using ionic 3 native camera plugin

2020-03-30 02:40发布

问题:

this.camera.getPicture({
      quality: 50,
      destinationType: this.camera.DestinationType.DATA_URL,
      mediaType: this.camera.MediaType.VIDEO,
      sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
    }).then((videoData) => {

    console.log('video data', videoData);

I can't send the video data to server.

The very first thing I'm struggling with here is that how is destinationType affecting the returned result because no matter what I set (either DATA_URL or File_Uri) it always returns me some url of this structure /storage/emulated/0/DCIM/Camera/VID_20180312_210545.mp4 . (FYI: I am currently testing this on android platform). I am able to preview the video by simply putting it in video src but I am unable to send this video to the server.

This is the approach that I am using to get the video file from the returned /storage like URL and then to send the video to server.

return this.file.resolveLocalFilesystemUrl(data).then((entry:FileEntry)=>{
      debugger;
      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);
        })
      })
    })

Where data parameter being passed to the resolveLocalFilesystemUrl is that same url (/storage/0..) that I mentioned earlier.

But this throws an error with error code 5 & error message ENCODING_ERR

I am not passing the encoding type here on purpose as that is for image files.

Important Note: if I add 'file://' to the data and then pass it to resolveLocalFilesystemUrl() like this this.file.resolveLocalFilesystemUrl('file://'+data).then(()) then I am able to create file entry which I've sent to the server and server has successfully saved the video. But I wanted to use more of a cross platform approach that will work both on Android & iOS

回答1:

Working with Android you need to correct your URL.

You should use the resolveNativePath when you choose a video from the library.

More docs you can find here:

https://ionicframework.com/docs/native/file-path/