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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Developer
Posts: 124
Registered: ‎09-06-2010
My Device: Not Specified

Widget readFile API hangs Torch

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;
}
}
);
}

 


 

Developer
Posts: 124
Registered: ‎09-06-2010
My Device: Not Specified

Re: Widget readFile API hangs Torch

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

 

 

Developer
Posts: 124
Registered: ‎09-06-2010
My Device: Not Specified

Re: Widget readFile API hangs Torch

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

 

 

 

 

Retired
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Widget readFile API hangs Torch

Hi jtyberg,

 

Do you know which line of code OS 6.0 hangs on? 

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Developer
Posts: 124
Registered: ‎09-06-2010
My Device: Not Specified

Re: Widget readFile API hangs Torch

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.

 

 

 

 

 

Retired
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Widget readFile API hangs Torch

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.

 

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Developer
Posts: 124
Registered: ‎09-06-2010
My Device: Not Specified

Re: Widget readFile API hangs Torch

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:

bbw-readFile-hang.png

 

with alert() call right before readFile() = goodness:

bbw-readFile-alert.png

 

bbw-readFile-working-with-alert.png

 

Retired
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Widget readFile API hangs Torch

Wow.. now that is super wierd.. by simply adding an alert() before the line it works Smiley Surprised

 

I've forwarded it on to our Dev & Testing group to dig deeper

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Developer
Posts: 185
Registered: ‎08-17-2008
My Device: Red BlackBerry Z10
My Carrier: Vodafone

Re: Widget readFile API hangs Torch

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

and don't works in Torch Smiley Sad

 

Seems that exist a problem reading and saving files in Torch or BlackBerry 6 devices ¿?

 

Thanks in advance,

Josep Maria
Retired
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Widget readFile API hangs Torch

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.

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter