11-07-2010 11:13 PM
Trying to use the blackberry.io.file.readFile API to read a file from the BlackBerry file system. Code is below.
Works wonderfully on BB OS 5.0 (9550 Simulator).
Hangs Torch 9800 simulator and device. Not cool.
Tried both Widget SDK 1.0 and 1.5, using UTF-8 and BASE64 encodings. Doesn't matter. Torch ends up dead.
var result = null; // encoded file contents
var error = null; // error
// if the file exists
if (blackberry.io.file.exists(fileName)) {
// read it
blackberry.io.file.readFile(fileName,
// success callback
function(filePath, blobData) {
// encode file contents
try {
result = blackberry.utils.blobToString(blobData, encoding);
} catch(e) {
// encoding error
error = new FileError();
error.code = error.ENCODING_ERR;
try {
errorCallback(error);
} catch (e) {
console.log('Error invoking callback: ' + e);
}
return;
}
}
);
}
11-07-2010 11:27 PM
Here's my console output when debugging the 9800 simulator. It's doing something, but seems to trip over itself.
FileIO:fileinfo start 5f1 FileIO:info by name complete 5f1 FileIO:fileinfo start 5f2 FileIO:info by name complete 5f2 FileIO:fileinfo start 5f3 FileIO:info by name complete 5f3 FileIO:fileinfo start 5f4 FileIO:info by name complete 5f4 FileIO:fileinfo start 5f5 FileIO:info by name complete 5f5 FileIO:open start 5f6 FileIO:open complete 5f6 FileIO:open start 5f7 FileIO:open complete 5f7 FileIO:fileinfo start 5f8 FileIO:info by name complete 5f8 FileIO:fileinfo start 5f9 FileIO:info by name complete 5f9 FileIO:read start 5fa FileIO:read complete 5fa VM:PISVt=0,h=3099,id=9a15b00dba73bc69 VM:IGCSc=0 VM:+GC(f)w=9 VM:-GCt=7,b=1,r=0,g=f,w=9 VM:QUOT t=1 VM:+CR VM:-CR=4 VM:IGCSc=0 VM:+GC(f)w=10 VM:-GCt=7,b=1,r=0,g=f,w=10 VM:+CR VM:-CR=0 FD Back On Expired; VM:PISVt=0,h=3099,id=9a15b00dba73bc69
11-08-2010 08:28 PM
I believe I have confirmed this as a bug on OS 6.0.
Can someone at RIM please try this simple test case on a 9800 sim? Works fine on 9550.
index.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" id="viewport" content="initial-scale=1.0,user-scalable=no"> <title>ReadFile Test</title> <script type="text/javascript"> function readFile(fileName) { // if the file exists if (blackberry.io.file.exists(fileName)) { // read it blackberry.io.file.readFile(fileName, // successCallback will be passed the full pathname of the file // and a Blob containing the file's contents function(filePath, blobData) { // encode file contents try { // BlackBerry devices support the following character encodings: // ISO-8859-1, UTF-8, UTF-16BE, US-ASCII, and BASE64 var contents = blackberry.utils.blobToString(blobData, "UTF-8"); alert('file contents: ' + contents); } catch(e) { // encoding error alert('encoding error'); } } ); } // file not found else { alert('file not found: ' + fileName); } }; function readFileFromStore() { readFile('file:///store/home/user/phonegap.txt'); }; function readFileFromSDCard() { readFile('file:///SDCard/phonegap.txt'); }; </script> </head> <body> <h3>FileReader</h3> <input type="button" value="Read File (Store)" onclick="readFileFromStore();return false;" /> <input type="button" value="Read File (SDCard)" onclick="readFileFromSDCard();return false;" /> </body> </html>
config.xml:
<?xml version="1.0" encoding="UTF-8"?> <widget xmlns="http://www.w3.org/ns/widgets" xmlns:rim="http://www.blackberry.com/ns/widgets" version="1.0.0.0"> <name>FileTest</name> <feature id="blackberry.io.file" required="true" version="1.0.0.0"/> <feature id="blackberry.utils" required="true" version="1.0.0.0"/> <access subdomains="true" uri="file:///store/home"/> <access subdomains="true" uri="file:///SDCard"/> <content src="index.html"/> </widget>
phongegap.txt (nothing special inside):
Android? iOS? Blah. BB rules!!
11-09-2010 07:20 AM
Hi jtyberg,
Do you know which line of code OS 6.0 hangs on?
11-09-2010 08:50 AM
I do indeed, Tim.
// read it
blackberry.io.file.readFile(fileName,
This is a threading issue.
I've been testing with the file on the SDCard. (I have to add the SDCard to the sim every time). If the file exists, the button press on the HTML file never completes (freezes in the pressed position). Device freezes.
However, when I add an alert call right before readFile:
// read it
alert('before readFile');
blackberry.io.file.readFile(fileName,
It works.
11-09-2010 09:06 AM
I'm curious on another point..
Have you tried to pass in your callback function like the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" id="viewport" content="initial-scale=1.0,user-scalable=no"> <title>ReadFile Test</title> <script type="text/javascript"> function readFile(fileName) { // if the file exists if (blackberry.io.file.exists(fileName)) { // read it blackberry.io.file.readFile(fileName,doCallback); } // file not found else { alert('file not found: ' + fileName); } }; // successCallback will be passed the full pathname of the file // and a Blob containing the file's contents function doCallback(filePath, blobData) { // encode file contents try { // BlackBerry devices support the following character encodings: // ISO-8859-1, UTF-8, UTF-16BE, US-ASCII, and BASE64 var contents = blackberry.utils.blobToString(blobData, "UTF-8"); alert('file contents: ' + contents); } catch(e) { // encoding error alert('encoding error'); } } function readFileFromStore() { readFile('file:///store/home/user/phonegap.txt'); }; function readFileFromSDCard() { readFile('file:///SDCard/phonegap.txt'); }; </script> </head> <body> <h3>FileReader</h3> <input type="button" value="Read File (Store)" onclick="readFileFromStore();return false;" /> <input type="button" value="Read File (SDCard)" onclick="readFileFromSDCard();return false;" /> </body> </html>
I'm wondering if there may be something different on how callback function pointers are handled in the 6.0 vs 5.0 JavaScript engines when interacting with Java.
11-09-2010 10:42 AM
Same behavior when removing the JS closure and putting the success callback outside. (Phew! Having that misbehave would be terrible).
without the alert() before readFile() = frozen:
with alert() call right before readFile() = goodness:
11-09-2010 11:35 AM
Wow.. now that is super wierd.. by simply adding an alert() before the line it works ![]()
I've forwarded it on to our Dev & Testing group to dig deeper
11-17-2010 02:37 PM
Some notice about this error?
I am having similar problem with a Widget running on Torch, on Savefile appears an error, on 9700 running good. (both real devices)
I did it a copy past of this code http://www.blackberry.com/developers/docs/widgetap
and don't works in Torch ![]()
Seems that exist a problem reading and saving files in Torch or BlackBerry 6 devices ¿?
Thanks in advance,
11-25-2010 10:55 AM
Hi Everyone,
We have found that this is a Threading issue on the Torch. We are currently in the process of bringing all the existing JavaScript APIs up off of the BlackBerry OS where they are currently distributed as binaries and instead moving them into the open source project.
Our development team has confirmed that this threading issue is now resolved as part of this process. We are working to try and get this updated software released as soon as possible.
The big benefit of bringing up the APIs off of the BlackBerry OS is that we will be independent of OS releases. So fixes like this don't have to wait for OS service packs. The full source code for everything that makes up BlackBerry WebWorks/Widgets will belong to the open source community.