Search code examples
angulartypescriptcordova-pluginsionic3ionic-native

Is it possible copy file in the private path to document directory?


This is my code. I used captureVideo() from ionic native mediacapture plugin.

And get this file path

/private/var/mobile/Containers/Data/Application/95DB5A64-700B-4E3D-9C2C-562C46520BEC/tmp/52515367181__BBC39D42-69EC-4384-A36F-7A512E109329.MOV

I want to copy this file to document directory.

CreateVide(){

      //SET CAPTUREVIDOE OPTION
      let videooption:CaptureVideoOptions = {limit:1};

      //CAPTURE VIDEO AND GET MEDIAFILE
      this.mediaCapture.captureVideo().then((videoData:Array<MediaFile>)=>{

        //SAVE AS FILE
          let filedir = this.file.documentsDirectory ;
          //Create directory
          this.file.createDir(filedir,'recordvideo',false);
          //SAVE FILE
          // this.file.writeFile(filedir + 'recordvideo', 'sample.mov', videoData[0]. );
          console.log('success:'+ videoData[0].fullPath);
          var ind = (videoData[0].fullPath.lastIndexOf('/')+1);
          var orgFilename = videoData[0].fullPath.substring(ind);
          var orgFilePath = videoData[0].fullPath.substring(0,ind);

          console.log(orgFilename, orgFilePath,filedir + 'recordvideo');
          this.file.copyFile(orgFilePath, orgFilename, filedir + 'recordvideo','sample.mov');
      });
}

Is it possible? Any help will be appreciated.


Solution

  • CreateVideo()
      {
          //SET CAPTUREVIDOE OPTION
          let videooption:CaptureVideoOptions = {limit:1};
    
          //CAPTURE VIDEO AND GET MEDIAFILE
          this.mediaCapture.captureVideo().then((videoData:Array<MediaFile>)=>{
    
            //SAVE AS FILE
            let filedir = this.file.documentsDirectory ;
            //Create directory
            this.file.createDir(filedir,'recordvideo',false);
    
    
            //CHANGE PATH
            var path = videoData[0].fullPath.replace('/private','file://');
            var ind = (path.lastIndexOf('/')+1);
            var orgFilename = path.substring(ind);
            var orgFilePath = path.substring(0,ind);
    
            //SAVE FILE                    
            this.file.copyFile(orgFilePath, orgFilename, filedir + 'recordvideo','sample.mov');
    
          });
      }
    

    I solved this program by replacing file path. Better answer will be appreciated.