copyTo给出错误代码1

时间:2014-04-03 07:53:23

标签: ios cordova

我无法找到错误。我认为,我正在通过我的代码获得正确的条目和路径,但它仍然会进入addFileToDirectory函数中的movingError。

这是我遇到问题的代码:

function addToFileSystem(entry){
  window.resolveLocalFileSystemURL(entry, onPhotoURISuccess, movingError);
}

function onPhotoURISuccess(entry){
  console.log('1.1');
  fileEntry = entry
  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, createOrSelectDirectory, movingError);
  console.log('1.2');
}

function createOrSelectDirectory(fileSys){
  console.log('2.1');
  var myFolderName = 'photos';
  fileSys.root.getDirectory(myFolderName, {create:true}, addFileToDirectory, movingError);
  console.log('2.2');
}

function addFileToDirectory(directory){
  console.log('3.1');
  var date = new Date();
  var fileName = date.getTime();
  var newFileName = fileName + '.jpg';
  console.log('newFileName:' + newFileName + ', directory: ' + directory.toURL() + ', fileEntry: ' + fileEntry.toURL());
  fileEntry.copyTo(directory, newFileName, OnMoveSuccess, movingError);
  console.log('3.2');
}

function OnMoveSuccess(fileEntry){
  console.log(fileEntry.toURL() + ' moved successfully');
}

function movingError(error) {
  console.log('error occoured while moving file with error code: ' + error.code);
}

控制台中的输出:

2014-04-03 09:33:29.678 HelloWorld[274:60b] 1.1
2014-04-03 09:33:29.683 HelloWorld[274:60b] 1.2
2014-04-03 09:33:29.685 HelloWorld[274:60b] 2.1
2014-04-03 09:33:29.693 HelloWorld[274:60b] 2.2
2014-04-03 09:33:29.694 HelloWorld[274:60b] 3.1
2014-04-03 09:33:29.695 HelloWorld[274:60b] newFileName:1396510409691.jpg, directory: cdvfile://localhost/persistent/photos/, fileEntry: cdvfile://localhost/temporary/cdv_photo_014.jpg
2014-04-03 09:33:29.699 HelloWorld[274:60b] 3.2
2014-04-03 09:33:29.700 HelloWorld[274:60b] error occoured while moving file with error code: 1

我确实试图删除一些movingError来检查它的调用位置,以及它来自fileEntry.copyTo。

我已经安装了文件插件。

这是IOS btw。

1 个答案:

答案 0 :(得分:0)

我重写了一下你的代码。在我们开始之前,并不是说你希望Camera使用NATIVE_URI,而不是FILE_URI:

navigator.camera.getPicture(onCamSuccess, onCamFail, 
{quality:50, 
sourceType:Camera.PictureSourceType.PHOTOLIBRARY,
destinationType:Camera.DestinationType.NATIVE_URI});

好的,所以在进一步说明之前,请注意我对这段代码不满意,因为每次拍照时,都会找到文件系统的钩子,然后是你想要的文件夹。这应该一次,而不是每次都完成。

function onCamSuccess(imgdata) {
    console.log(imgdata);
    $("#entryPicture").val(imgdata);
    $("#imgPreview").attr("src", imgdata);
    //copy the image to global scope
    imageUri = imgdata;
    window.requestFileSystem(window.PERSISTENT, 0, gotFS, onError);
}

现在让我们看看gotFS

function gotFS(fs){
    var myFolderName = 'photos';
    //copy file system to global scope
    fileSys = fs;
    fileSys.root.getDirectory(myFolderName, {create:true}, addFileToDirectory, onError);
}

然后让我们看一下addFileToDirectory

function addFileToDirectory(directory){
    var date = new Date();
    var fileName = date.getTime();
    var newFileName = fileName + '.jpg';

    window.resolveLocalFileSystemURL(imageUri, function(fe) {
        fe.copyTo(directory, newFileName, function(e) {
            console.log('in theory, done');
        }, onError);
    },onError);
}

然后你去了。这对我有用。正如我所说,我认为可以做得更好。