我有以下代码将捕获的图像存储在SD卡的单独目录中。但是如何检查特定目录是否存在?因为如果存在我想显示该目录中的图像,如果不是我想将捕获的图像保存到新目录中。第二部分是我的代码。但请帮我第一部分。当它进入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);
};
}
答案 0 :(得分: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函数。
您可以为此事件设置不同的错误回调方法。但是它的逻辑可以帮助你实现你想要的东西。