Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Contributor
Gerii
Posts: 33
Registered: ‎04-30-2012
My Carrier: 3AT

Cordova/HTML5 FileAPI on DevAlpha

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.PERSISTENT, 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.sharedFolder + '/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);
}

 

 

Please use plain text.
BlackBerry Development Advisor
haahmad
Posts: 6
Registered: ‎10-08-2012
My Carrier: Telus

Re: Cordova/HTML5 FileAPI on DevAlpha

Hi Gerri,

It looks like the problem is that every time you hit the writerCreated function, you call writerCreated again, with no change in state.

This is because the readyState is still 0 (which is INIT), and you haven't given the writer a chance to start writing before you read the readyState again.

The writerCreated function can never enter the else block because on the first entry into this function the readyState is 0, so you hit setTimeout and exit the function. After the timeout has expired writerCreated is fired again but the writer's readyState has not changed.

Try writing the file without the setTimeout logic and see if that helps you.
Please use plain text.
Contributor
Gerii
Posts: 33
Registered: ‎04-30-2012
My Carrier: 3AT

Re: Cordova/HTML5 FileAPI on DevAlpha

[ Edited ]

 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_6k1IwmhCOjnsc/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

 

Please use plain text.
BlackBerry Development Advisor
chadtatro
Posts: 604
Registered: ‎10-01-2009
My Carrier: Bell

Re: Cordova/HTML5 FileAPI on DevAlpha

[ Edited ]

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..

Chad Tetreault | App Development Consultant | BlackBerry Developer Relations | developer.blackberry.com | @chadtatro
Please use plain text.
Contributor
Gerii
Posts: 33
Registered: ‎04-30-2012
My Carrier: 3AT

Re: Cordova/HTML5 FileAPI on DevAlpha

Thanks, looking forward to it.
Please use plain text.
Contributor
Gerii
Posts: 33
Registered: ‎04-30-2012
My Carrier: 3AT

Re: Cordova/HTML5 FileAPI on DevAlpha

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/blackberry.io.html

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.

 

 

 

Please use plain text.
BlackBerry Development Advisor
chadtatro
Posts: 604
Registered: ‎10-01-2009
My Carrier: Bell

Re: Cordova/HTML5 FileAPI on DevAlpha

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.

Chad Tetreault | App Development Consultant | BlackBerry Developer Relations | developer.blackberry.com | @chadtatro
Please use plain text.
Contributor
Gerii
Posts: 33
Registered: ‎04-30-2012
My Carrier: 3AT

Re: Cordova/HTML5 FileAPI on DevAlpha

Thanks for checking.
Even if I create a new project without PhoneGap, it fails to find the FileWriter. FileReader works fine though.
Please use plain text.