androidcordovacordova-plugin-file

cordova-plugin-file can't copy file from gallery to cordova.file.dataDirectory


I'm trying to save a video record from the camera to cordova.file.dataDirectory but I get a NOT_FOUND_ERR when I try to copy the newly created file from the gallery.

I'm using cordova-plugin-media-capture to record the video, which then is saved to the user's gallery. But when I get the FileEntry using window.resolveLocalFileSystemURL and try to use entry.copyTo() the NOT_FOUND_ERR error is thrown.

A cut down / messy version of my code is below:

// Capture Video
navigator.device.capture.captureVideo(onCaptureSuccess, onError, { limit: 1 });

onCaptureSuccess(mediaFiles: any[]) {

  // Get video Path
  let videoPath = mediaFiles[0].fullPath;

  // Get the actual video file
  window.resolveLocalFileSystemURL(videoPath, entry => {

    // Get the cordova.file.dataDirectory directory entry
    window.resolveLocalFileSystemURL(cordova.file.dataDirectory, directoryEntry => {

      // Get / Create the 'videos' directory
      directoryEntry.getDirectory('videos', { create: true }, videoDirectoryEntry => {
    
        // Copy video file (entry) to 'videos' directory
        entry.copyTo(videoDirectoryEntry, 'copiedVideo', copiedEntry => {

          // I should now have a file entry of the copied video, but I don't. I get an error.
          console.log(copiedEntry);

        }, err => {

          console.error(err); // This is where I capture the NOT_FOUND_ERR error

        });
    
      }, onError);
    
    }, onError);

  });

}

onError(error: Error) {
  console.error(error.message)
}

Any idea why the file isn't being copied? Do I need to request specific permissions to write to cordova.file.dataDirectory?


Solution

  • After a lot of trial and error, it turns out I needed to request the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions for Android.

    At a future date I'll check if any permissions are needed for iOS, but for now I'm happy that it's working for Android.