首页 > 解决方案 > cordova-plugin-file 无法将文件从画廊复制到 cordova.file.dataDirectory

问题描述

我正在尝试将相机中的视频记录保存到,cordova.file.dataDirectory但是NOT_FOUND_ERR当我尝试从图库中复制新创建的文件时,我得到了一个。

cordova-plugin-media-capture用来录制视频,然后将其保存到用户的画廊。但是当我得到FileEntry使用window.resolveLocalFileSystemURL并尝试使用时entry.copyTo()NOT_FOUND_ERR就会抛出错误。

我的代码的精简/混乱版本如下:

// 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)
}

知道为什么文件没有被复制吗?我是否需要请求特定权限才能写入cordova.file.dataDirectory

标签: androidcordovacordova-plugin-file

解决方案


经过大量的试验和错误,事实证明我需要为 Android 请求READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE权限。

在未来的某个日期,我会检查 iOS 是否需要任何权限,但现在我很高兴它适用于 Android。


推荐阅读