I am eagerly anticipating the day when the file function in Cordova finally starts working for me!
This particular section of code functions perfectly on Chrome (hooray!), but unfortunately not within the Android app:
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();
(For Android, I have added initFs to the deviceready event and replaced "sdcard" with "root" for Chrome.)
So, the question that has been on my mind for the past two weeks is: What could be wrong with my code using the Cordova file API?