0

我有以下代码将捕获的图像存储在 sdcard 的单独目录中。但是如何检查特定目录是否存在?因为如果存在,我想显示该目录中的图像,如果不存在,我想将捕获的图像保存到新目录中。第二部分在我的代码中。但是请帮助我完成第一部分。当它进入我想检查的 ondeviceready 事件时,目录是否存在???

function getImageURI(imageURI) {
        capturedImgId++;
        var imgId = "img"+capturedImgId;
        document.getElementById(imgId).src =  imageURI;
        document.getElementById(imgId).style.display = 'block';
        createButton(capturedImgId);
        var gotFileEntry = function(fileEntry) {
            alert("got image file entry: " + fileEntry.fullPath);
            var gotFileSystem = function(fileSystem) {

                fileSystem.root.getDirectory("BangaloreFolder", {
                    create : true
                }, function(dataDir) {

                    // copy the file
                    dataDir.getDirectory(today+"-T"+teamNo, {
                      create : true
                    },function(dataDir1) {
                      fileEntry.moveTo(dataDir1, capturedImgId+".jpg", null, fsFail);//try out with capturedImgId.jpg 
                    });

                }, dirFail);

            };
            // get file system to copy or move image file to
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystem,
                    fsFail);
        };
        // resolve file system for image
        window.resolveLocalFileSystemURI(imageURI, gotFileEntry, fsFail);

        // file system fail
        var fsFail = function(error) {
            alert("failed with error code: " + error.code);

        };

        var dirFail = function(error) {
            alert("Directory error code: " + error.code);

        };
    }
4

1 回答 1

1

我认为您的以下代码应该能够帮助您

 fileSystem.root.getDirectory("BangaloreFolder", {
                create : true
            }, function(dataDir) {

                // copy the file
                dataDir.getDirectory(today+"-T"+teamNo, {
                  create : false
                },function(dataDir1) {
                  fileEntry.moveTo(dataDir1, capturedImgId+".jpg", null, fsFail);//try out with capturedImgId.jpg 
                });

            }, dirFail);

注意:我已替换create:false,因此如果目录不存在,它将调用 dirFail 函数。

您可以为此事件使用不同的错误回调方法。但是更多的逻辑可以帮助你实现你正在寻找的东西。

于 2013-05-01T08:53:32.923 回答