window.requestFileSystem fails/events firing multiple times in PhoneGap Cordova 3

Posted by ezycheez on Stack Overflow See other posts from Stack Overflow or by ezycheez
Published on 2013-11-12T21:51:33Z Indexed on 2013/11/12 21:52 UTC
Read the original article Hit count: 274

Filed under:
|

cordova 3.1.0-0.2.0
Droid Razr M
Android 4.1.2
Windows 7

New to PhoneGap/Cordova. Running one of the filesystem demos from http://docs.phonegap.com/en/3.1.0/cordova_file_file.md.html#File. Using "cordova run android" to compile the code and run it on the phone attached via USB.

Problem #1: When the app first starts I get alert("1") twice and then nothing else.

Problem #2: When I kick of the code via the button onclick I get the following alert pattern: 1, 2, 2, 3, 3, 4, 6, 5, 7, 4, 6, 5, 7. Some of that makes sense based on the code flow but most of them are firing twice.

I'm suspecting problem #2 is being caused by some asynchronous calls hanging out from the first attempt but no matter how long I wait those events don't fire until I kick of the code via the button.

So why is the requestFileSystem call failing even though it is waiting for deviceready and why is the other code getting intermingled?

Any thoughts are appreciated.

<!DOCTYPE html>
<html>
  <head>
    <title>FileReader Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

        // Wait for device API libraries to load
        //
        document.addEventListener("deviceready", onDeviceReady, false);

        // device APIs are available
        //
        function onDeviceReady() {
            alert("1");
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
        }

        function gotFS(fileSystem) {
            alert("2");
            fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail);
        }

        function gotFileEntry(fileEntry) {
            alert("3");
            fileEntry.file(gotFile, fail);
        }

        function gotFile(file) {
            alert("4");
            readDataUrl(file);
            alert("5");
            readAsText(file);
        }

        function readDataUrl(file) {
            alert("6");
            var reader = new FileReader();
            reader.onloadend = function (evt) {
                console.log("Read as data URL");
                console.log(evt.target.result);
            };
            reader.readAsDataURL(file);
        }

        function readAsText(file) {
            alert("7");
            var reader = new FileReader();
            reader.onloadend = function (evt) {
                console.log("Read as text");
                console.log(evt.target.result);
            };
            reader.readAsText(file);
        }

        function fail(error) {
            alert("8");
            console.log(error.code);
        }

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>Read File</p>
      <button onclick="onDeviceReady();">Read File</button>
  </body>
</html>

© Stack Overflow or respective owner

Related posts about cordova

Related posts about cordova-3