11-11-2012 09:47 AM
I'm trying to use the FileAPI in my app, but I can't get the FileWriter working on BlackBerry. I'm able to create a new file, but the FileWriter keeps hanging at ReadyState 0.
I've tried different ways (that I've already used successfully on MeeGo, Android and Symbian) to get it working, but without any success.
function initFile() {
//get current time and date for filename
timeDate = getTimeAndDate();
if(device.platform == "PlayBook" || device.platform == null || device.platform == "BB10") {
try {
//TODO no permission error
blackberry.io.sandbox = false;
console.log("requesting FS");
window.requestFileSystem(LocalFileSystem.PERSISTEN T, 1024*1024, fileSystemInitialised, reqFileFail);
}
catch (er) {
console.log(er.toString());
}
}
}
function reqFileFail(error)
{
//TODO change
console.log(error.code);
}
function fileSystemInitialised(p_fileSystem) {
console.log("FS initialisiert");
if(device.platform == "PlayBook" || device.platform == "BB10") {
console.log("PB/BB10");
try {
p_fileSystem.root.getFile(blackberry.io.sharedFold er + '/documents/' + datumZeit + ".kml", { create: true, exclusive: false }, initWriter, fileError);
}
catch(er) {
alert(er.toString());
}
}
}
function initWriter(p_fileEntry) {
console.log(p_fileEntry.name);
p_fileEntry.createWriter(writerCreated, fileError);
//p_fileWriter = new FileWriter(p_fileEntry);
//writerCreated(p_fileWriter);
}
function writerCreated(p_fileWriter) {
p_fileWriter.onwriteend = function(e) {
console.log('Write completed.');
};
p_fileWriter.onerror = function(e) {
console.log('Write failed: ' + e.toString());
};
console.log(p_fileWriter.fileName);
console.log("ReadyState: " + p_fileWriter.readyState);
if(p_fileWriter.readyState == 0) {
setTimeout(writerCreated(p_fileWriter), 1000);
}
else {
p_fileWriter.write('<?xml version="1.0" encoding="UTF-8"?>\n' + '<kml xmlns="http://www.opengis.net/kml/2.2">\n' + '<Document>\n' + '<Placemark>\n' + '<name>Alpintouren-Tour</name>\n' + '<description>Tour erstellt mit der Alpintouren-App</description>' + '<visibility>0</visibility>' + '<Style>\n' + '<LineStyle><color>ff00ffff</color><width>4</width ></LineStyle>\n' + '<PolyStyle><color>7f00ff00</color></PolyStyle>\n' + '</Style>\n', false);
}
}
function fileError(p_fileError) {
console.log("Fehler: " + p_fileError.code);
}
11-12-2012 01:39 PM
11-18-2012 08:31 AM - edited 11-18-2012 08:32 AM
That was definitely an issue, thanks for pointing that out. Unfortunately it still doesn't work.
My code now look like this:
function initWriter(p_fileEntry) {
console.log(p_fileEntry.name);
//DOESN'T WORK (p_fileWriter.fileName returns undefined):
//p_fileEntry.createWriter(writerGlobalSpeichern, fileError);
p_fileWriter = new FileWriter(p_fileEntry);
writerGlobalSpeichern(p_fileWriter);
}
function writerCreated(p_fileWriter) {
p_fileWriter.onwriteend = function(e) {
console.log('Write completed.');
};
p_fileWriter.onerror = function(e) {
console.log('Write failed: ' + e.toString());
};
console.log(p_fileWriter.fileName);
console.log("ReadyState: " + p_fileWriter.readyState);
p_fileWriter.write('test', false);
console.log("ReadyState: " + p_fileWriter.readyState);
}In the console of the WebInspector, I get the following:
XHR finished loading: "http://localhost:8472/blackberry.io/sharedFolder". [native code]:1 2012-11-18_14-24-44.kml /accounts/1000/appdata/alpintouren.gYABgOVu8z7kR_6 k1IwmhCOjnsc/shared/documents/2012-11-18_14-24-44. kml ReadyState: 0 Error: Status=2 Message=Class File cannot be found cordova-2.2.0.js:959 Write failed: [object Object] Write completed. ReadyState: 2
11-19-2012 09:02 AM - edited 11-19-2012 09:03 AM
Hi there,
Took a look and everything appeared to look proper on my end. FileAPI can be a little tricky sometimes. We have a great sample that's being worked on by one of my teammates to greatly simplify the HTML5 File API. I'll report back ASAP as to when that'll be ready..
11-25-2012 09:27 AM
12-09-2012 01:00 PM
Any news on the sample?
I have now tried to remove PhoneGap from my app and use only WebWorks with the code from https://developer.blackberry.com/html5/apis/blackb
This works fine for reading a file. But when I tried to change the FileReader
var reader = new FileReader();
reader.onloadend = function (e) {
var txtArea = document.createElement('textarea');
txtArea.value = this.result;
document.body.appendChild(txtArea);
};
reader.readAsText(file);to the FileWriter
writer = new FileWriter(file);
p_fileWriter.onwriteend = function(e) {
console.log('Write completed.');
};
p_fileWriter.onerror = function(e) {
console.log('Write failed: ' + e.toString());
console.log(e);
};
p_fileWriter.write('test', false);I get ReferenceError: Can't find variable: FileWriter
So I wonder whether the FileWriter is even supported in the current OS build or not.
12-10-2012 09:29 AM
Right,
In the current version of PhoneGap the FileAPI isn't supported. I suspect there will be a new build out any time though, as they release pretty much on a monthly basis.
One of my teammates is actually working very close to the PhoneGap project, and is adding more and more API support with each build.
In regards to the FileAPI wrapper sample, I'll follow up on that today again.
12-10-2012 11:01 AM