Cordova / Phonegap文件功能不起作用

时间:2014-05-02 05:11:14

标签: javascript android cordova

我希望有一天cordova的文件功能对我有用!

这部分代码适用于chrome(hourra!),但不适用于Android应用程序:

function errorHandler(e) {
  var msg = '';

  switch (e.code) {
    case FileError.QUOTA_EXCEEDED_ERR:
      msg = 'QUOTA_EXCEEDED_ERR';
      break;
    case FileError.NOT_FOUND_ERR:
      msg = 'NOT_FOUND_ERR';
      break;
    case FileError.SECURITY_ERR:
      msg = 'SECURITY_ERR';
      break;
    case FileError.INVALID_MODIFICATION_ERR:
      msg = 'INVALID_MODIFICATION_ERR';
      break;
    case FileError.INVALID_STATE_ERR:
      msg = 'INVALID_STATE_ERR';
      break;
    default:
      msg = 'Unknown Error';
      break;
  };

  alert('Error: ' + msg);
}





function InitFs() {
    if ( navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/android/i) ) {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 10*1024*1024, afterInitFs, errorHandler);
    } else {
        window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
        window.LocalFileSystem = window.LocalFileSystem || {PERSISTENT: window.PERSISTENT};
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 10*1024*1024, afterInitFs, errorHandler);
    }
}
function afterInitFs(fs) {
     console.log('Opened file system: ' + fs.name);

  fs.sdcard.getFile('test.txt', {create: true, exclusive: false}, function(fileEntry) {
    fileEntry.createWriter(function(fileWriter) {
      fileWriter.onwriteend = function(e) {
        console.log('Write completed.');
      };
      fileWriter.onerror = function(e) {
        console.log('Write failed: ' + e.toString());
      };
      var blob = new Blob(['lol'], {type: 'text/plain'});
      fileWriter.write(blob);
     console.log(fileEntry.isFile + ' / ' + fileEntry.toURL());
    }, errorHandler);
  }, errorHandler);

}
InitFs();

(对于android,我当然将initF添加到deviceready事件中,并将sdcard替换为root用于chrome。)

所以,我的句子我说自两周以来最多:我的代码与cordova的api文件有什么不对?

1 个答案:

答案 0 :(得分:1)

试试这个。它对我有用:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
function gotFS(fileSystem) {
    fileSystem.root.getDirectory("DO_NOT_DELETE", 
        {create: true, exclusive: false}, 
        gotDirEntry, 
        fail);
}
function gotDirEntry(dirEntry) {
    dir = dirEntry;
    dirEntry.getFile("sample.json", 
        {create: false, exclusive: false}, 
        readSuccess, 
        fileDonotexist);
}
function fileDonotexist(dirEntry) {
    dir.getFile("sample.json", 
        {create: true, exclusive: false}, 
        gotFileEntry, 
        fail);
}
function gotFileEntry(fileEntryWrite) {
    fileEntryWrite.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
    writer.onerror = function(evt) {
    };
    writer.write(localData);
    writer.onwriteend = function(evt) {
        dir.getFile("sample.json", 
            {create: false, exclusive: false}, 
            readSuccess, 
            fail);
    };
}
function readSuccess(fileE) {
    fileE.file(readAsText, fail);
}
function fail(error) {
    console.log("fail");
}
function readAsText(readerDummy) {
    var reader = new FileReader();

    reader.onloadstart = function(evt) {};
    reader.onprogress = function(evt) {};
    reader.onerror = function(evt) {};

    reader.onloadend = function(evt) {
        console.log("read success");
    };
    reader.readAsText(readerDummy);
}