cordova,android app如何创建子文件夹

时间:2013-09-16 11:10:02

标签: android directory cordova-3

我正在使用cordova,我尝试在设备上创建sd卡根目录的文件夹。 我使用以下代码创建文件夹并在其中添加文件'login.txt':

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

function gotFS(fileSystem) {
   fileSystem.root.getDirectory("citylook", {create: true}, gotDir);
}

function gotDir(dirEntry) {
    dirEntry.getFile("login.txt", {create: true, exclusive: true}, gotFile);
}

function gotFile(fileEntry) {
    // Do something with fileEntry here
    fileEntry.createWriter(gotFileWriter, fail);
}


function gotFileWriter(writer) {
    writer.onwriteend = function(evt) {
        console.log("contents of file now 'some sample text'");
        writer.truncate(11);
        writer.onwriteend = function(evt) {
            writer.seek(0);
            writer.write(testo);
            writer.onwriteend = function(evt){
                console.log("contents of file now '"+testo+"'");
            }
        };
    };
    writer.write("some sample text");
}

function fail(error) {
    console.log(error.code);
}

现在,我需要在“citylook”文件夹中创建一个文件夹,我试试这个:

function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, null);
}

function onSuccess() {
    fileSystem.root.getDirectory("citylook", {create: true, exclusive: false}, onGetDirectoryWin, onGetDirectoryFail);
    fileSystem.root.getDirectory("citylook/nuovo", {create: true, exclusive: false}, onGetDirectoryWin2, onGetDirectoryFail2);
}

....

但是我无法创建子文件夹....我的代码中出了什么问题?

三江源

解决:

fileSystem.root.getDirectory("citylook", {create: true}, gotDir);
fileSystem.root.getDirectory("citylook/subfolder", {create: true}, gotDir);

2 个答案:

答案 0 :(得分:0)

var type = window.PERSISTENT;
   var size = 5*1024*1024;

   window.requestFileSystem(type, 0, successCallback, errorCallback);
   function successCallback(fs) {
       var dir=fs.root;
       alert('root   -'+dir.fullPath);// O/P:-root   -/
      dir.getDirectory('Albums', {create: true}, function(dirEntry) {
          alert('Albums Path:'+dirEntry.fullPath);// O/P:-Allbums Path: /Albums/
         dirEntry.getDirectory('Images',{create:true},function(subDir){
                              alert('Hello');
                              alert('SubDirPath='+subDir.fullPath);//output:-SubDirPath=/Albums/Images/
             //subDir.getFile('Log.txt',{create: true, exclusive: false}, function(fileEntry) {

        //writeFile(fileEntry, null, isAppend);
                 //alert('File Path'+fileEntry.fullPath);

    }, onErrorCallback);

         },errorCallback);
          dirEntry.getDirectory('Audio',{create:true},function(subDir){
                              alert('Hello');
                              alert('SubDirPath='+subDir.fullPath);//output:-SubDirPath=/Albums/Audio/
         },errorCallback);
      }, errorCallback);
   }
        function errorCallback(error) {
      alert("ERROR: " + error.code)
   }

此代码有助于在root中创建文件夹和子文件夹。 /相册是主文件夹。 /专辑/图像/和/专辑/音频/是子文件夹(图像和音频)

答案 1 :(得分:0)

window.resolveLocalFileSystemURL(
        cordova.file.externalDataDirectory,
        function(entry) {
          entry.getDirectory(
            "myNewDirectory",
            { create: true, exclusive: false },
            function(result) {
              alert(JSON.stringify(result));
            },
            onError
          );
        }
      );

我正在使用cordova为Android / data // files创建一个文件夹成功。