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
New Contributor
kpho87
Posts: 3
Registered: ‎06-30-2013
My Device: BlackBerry Z10
Accepted Solution

Failed to call LocalFileSystem in Black Berry Z10

Hi,

 

I am developing and deploying an app in my BB Z10. I getting an error message - "Can't find variable - LocalFileSystem".

 

Below is the code where I calling the LocalFileSystem: 

===========================================================================

window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
var fs = null;

 

$(document).ready(function() {
        document.addEventListener("webworksready", function(e) {
                  try
                  {
                          window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(filesystem) {
                                   fs = filesystem;
                           }, errorHandler);
                   }
                  catch(ex)
                  {
                         alert(ex.message)
                  }
        }, false);
});

 

I had study https://developer.blackberry.com/html5/apis/localfilesystem.html and https://developer.blackberry.com/html5/documentation/using_webworks_apis.html and follow those instruction but all APIs that begin with "HTML5" were failed to call (My app was worked well with others API like FileTransfer, IO etc).

 

Can someone please suggest how to resolve?

 

Thanks.

 

Regards,

Kian Ping

 

 

Retired
grahamzibar
Posts: 71
Registered: ‎09-19-2012
My Device: No sleep til BlackBerry 10

Re: Failed to call LocalFileSystem in Black Berry Z10

Looks like you might be requesting a filesystem of zero size.  Try this:

 

window.webkitRequestFileSystem(window.TEMPORARY, 1024 * 1024, receivedFs, onFileError);

 

Where "receivedFs" would be some function like so:

 

function receivedFs(filesystem) {

    // Do something

}

 

and "onFileError" being the appropriate error callback function.

 

Hope that helps!

------------------------------
@grahamzibar
-_- zZZ
New Contributor
kpho87
Posts: 3
Registered: ‎06-30-2013
My Device: BlackBerry Z10

Re: Failed to call LocalFileSystem in Black Berry Z10

Hi grahamzibar,

 

The purpose that I want to access file system is to retrieve directory of shared folder/downloads and check the existence of a pdf file

My strategic:

1) Obtain file system fs from window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(filesystem) { fs = file system; }, function(error){alert(error.message:smileywink:};

2) After that, check the existence of a pdf file by using html5 directory reader and for loop

var pdf_exist = false;

var directoryReader = fs.root.createReader();

 

directoryReader.readEntries(function(entries){

     for(var i=0; i<entries.length; i++){

          if(FileEntry[i].name == "xxx.pdf"){

               pdf_exist = true;

               break;

          }

     }

},function(er){alert( er.message);});

 

3) Then, if pdf_exist = false, then download the pdf file; else open and view the pdf file;

 

Above strategic was worked well in android and iphone. I had tried blackberry.io.file.exists() but this function not supported in BB10.

 

 

Between, I had also tried this: window.webkitRequestFileSystem(window.TEMPORARY, 1024 * 1024, function(filesystem) { fs = file system; }, function(error){alert(error.message:smileywink:});. It return a null value (fs = null)

 

Any other way that can verify/check the existence of pdf file which located in file:///accounts/1000/shared/downloads/?

 

Thanks in advance.

 

Regards,

Kian Ping

 

  

Retired
grahamzibar
Posts: 71
Registered: ‎09-19-2012
My Device: No sleep til BlackBerry 10

Re: Failed to call LocalFileSystem in Black Berry Z10

The below function worked perfectly for me on a Z10 running OS 10.1.  Let me know if this works for you too :smileyhappy:  You would need to wait for the webworks ready event and have all the necessary config.xml settings (such as the permission to access the shared directory as well as adding the blackberry.io feature).  Let me know if you have any more questions!

 

function checkPDF() {
    blackberry.io.sandbox = false;
    var hasPDF = false;
    window.webkitRequestFileSystem(
        window.TEMPORARY,
        1024 * 1024,
        function(fs) {
            console.log('harr', blackberry.io.sharedFolder + '/downloads');
            fs.root.getDirectory(
                blackberry.io.sharedFolder + '/downloads',
                { create: false },
                function(folder) {
                    console.log('folder!!', folder.fullPath);
                    folder.createReader().readEntries(
                        function(entries) {
                            console.log('ENTRIES:', entries); // CONSOLE SANITY CHECK :smileyhappy:
                            for (var i = 0; i < entries.length; i++) {
                                if (entries[i].name.indexOf('.pdf') != -1)
                                    hasPDF = true;
                            }
                            alert(hasPDF); // HERE'S YOUR ANSWER
                        },
                        fsError
                    );
                },
                fsError
            );
        },
        fsError
    );
};

------------------------------
@grahamzibar
-_- zZZ
New Contributor
kpho87
Posts: 3
Registered: ‎06-30-2013
My Device: BlackBerry Z10

Re: Failed to call LocalFileSystem in Black Berry Z10

Hi,

 

Yes,I found out the reason why I getting null value when I calling window.webkitRequestFileSystem. This is because I missed this 1 - blackberry.io.sandbox = false;

 

Now I able to obtain fs value and the value I obtained is filesystem:local:///temporary/ . But, now I facing another problem: HTML getDirectory() doesn't work - no trigger success callback, no getting error message, no pop up any exception.

 

Below is the code where I calling getDirectory():

try{
    fs.root.getDirectory(blackberry.io.sharedFolder + "/downloads", {create: false},
        function (folder) {
            alert("downloads folder is found");
        },
        function (error) {
            alert("error with getting directory.");
        }
    );
}
catch(ex)
{
    alert(ex.message);
}

 

This 1 is 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" id="org.apache.cordova.example">

  <name>FingerTec</name>

  <author href="http://www.fingertec.com" email="support@fingertec.com">
		FingerTec WorldWide
  </author> 

  <description>
       FingerTec Mobile Application
  </description>
    
  <license href="http://opensource.org/licenses/alphabetical">
  </license>

  <preference name="permissions"                value="none"/>

  <!-- Customize your app and platform with the preference element. -->
  <preference name="phonegap-version"           value="2.9.0" />          <!-- all: current version of PhoneGap -->
  <preference name="orientation"                value="default" />        <!-- all: default means both landscape and portrait are enabled -->
  <preference name="target-device"              value="universal" />      <!-- all: possible values handset, tablet, or universal -->
  <preference name="fullscreen"                 value="true" />           <!-- all: hides the status bar at the top of the screen -->
  <preference name="disable-cursor"             value="false" />          <!-- blackberry: prevents a mouse-icon/cursor from being displayed on the app -->
  
  <!-- Cordova API -->
  <feature id="org.apache.cordova" required="true" version="1.0.0" />
  <feature id="blackberry.find" required="true" version="1.0.0.0" />
  <feature id="blackberry.identity" required="true" version="1.0.0.0" />
  <feature id="blackberry.identity.phone" required="true" version="1.0.0.0" />
  <feature id="blackberry.pim.Address" required="true" version="1.0.0.0" />
  <feature id="blackberry.pim.Contact" required="true" version="1.0.0.0" />
  <feature id="blackberry.utils" required="true" version="1.0.0.0" />
  <feature id="blackberry.io.dir" required="true" version="1.0.0.0" />
  <feature id="blackberry.app" required="true" version="1.0.0.0" />
  <feature id="blackberry.app.event" required="true" version="1.0.0.0" />
  <feature id="blackberry.system.event" required="true" version="1.0.0.0"/>
  <feature id="blackberry.widgetcache" required="true" version="1.0.0.0"/>
  <feature id="blackberry.media.camera" />
  <feature id="blackberry.ui.dialog" />
  <feature id="blackberry.connection" />
  <feature id="blackberry.bbm.platform" />
  <feature id="blackberry.invoke.card" />
  <feature id="blackberry.pim.contacts" />
  <feature id="blackberry.ui.contextmenu" />
  <feature id="blackberry.system" required="true" version="1.0.0.0" />
  <feature id="blackberry.io.file" required="true" version="1.0.0.0" />
  <feature id="blackberry.io.filetransfer" required="true" version="1.0.0.0"/>
  <feature id="blackberry.io" required="true" version="1.0.0.0"/>
  <feature id="blackberry.invoke" required="true" version="1.0.0.0"/>
  <feature id="blackberry.invoked" />
  <feature id="blackberry.push" />
  <feature id="blackberry.media.microphone" required="true" version="1.0.0.0"/>
  
  <!-- Cordova API -->
  <access subdomains="true" uri="file:///accounts/" />  
  <access subdomains="true" uri="file:///accounts/1000/shared/downloads/" />  
	
  <!-- Expose access to all URIs, including the file and http protocols -->
  <access subdomains="true" uri="*" />

  <icon rim:hover="false" src="images/appicon.png" />
  <icon rim:hover="true" src="images/appicon.png" />

  <rim:loadingScreen backgroundColor="#CFCFCF"
                     foregroundImage="images/Default.png"
		     onFirstLaunch="true">
    <rim:transitionEffect type="fadeOut" />
  </rim:loadingScreen>

  <content src="local:///index.html"/>

  <rim:permissions>
    <rim:permit>use_camera</rim:permit>
    <rim:permit>read_device_identifying_information</rim:permit>
    <rim:permit>access_shared</rim:permit>
    <rim:permit>read_geolocation</rim:permit>
    <rim:permit>record_audio</rim:permit> 
    <rim:permit>access_pimdomain_contacts</rim:permit> 
  </rim:permissions>

</widget>

 

I ...

1) had set the permission to access and declare the feature in config.xml and declare those needed plugin in plugins.xml

2) able to fires the webworks ready event and able to obtain file system

3) had un-sandbox the file system to access shared folder

 

Have I missed something out? Or which part I did wrong?

 

Regards,

Kian Ping