Writing and reading a file in a telephone stutter

I tried writing/reading file in phonegap+android , here is the setting:

 $(document).ready(function() { document.addEventListener("deviceready", deviceready, true); $(document).bind("deviceready", function(){ //writeFile(); //readFile(); }); }); function deviceready() { writeFile(); readFile(); } // This is just to do this. function readFile() { var d = navigator.file.read('/sdcard/foo.xml', success(), fail()); console.warn(d); } function writeFile() { navigator.file.write('/sdcard/foo.xml', "This is a test of writing to a file", success(), fail()); } 

But on the emulator for Android 2.2 , I got the following error message:

 08-06 14:21:29.428: INFO/Web Console(936): Error in success callback: Network Status1 = TypeError: Result of expression 'navigator.file' [undefined] is not an object. at file:///android_asset/www/phonegap.0.9.6.js:649 

What may be missing and what can I try?

+6
source share
4 answers

Below Android works for me with the phone-1.0.0:

 <script type="text/javascript" charset="utf-8" src="css-js/phonegap-1.0.0.js"></script> <script type="text/javascript" charset="utf-8"> // Wait for PhoneGap to load // document.addEventListener("deviceready", onDeviceReady, false); // PhoneGap is ready // function onDeviceReady() { window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); } function gotFS(fileSystem) { var path = "readme.txt"; fileSystem.root.getFile(path, {create: true, exclusive: false}, gotFileEntry, fail); } function gotFileEntry(fileEntry) { fileEntry.createWriter(gotFileWriter, fail); } function gotFileWriter(writer) { writer.onwrite = function(evt) { console.log("write success"); }; writer.write("some sample text"); </script> 
-1
source

It also works in Android 2.2. You call load (); function from body onLoad and writeFileFromSDCard (string) from some onClick button, passing as a parameter the string that you want to write to the file.

 <script type="text/javascript" charset="utf-8"> // Event listener function load() { document.addEventListener('deviceready', init, false); } // Called when device is ready - Do nothing function init() { } // Called to write file to card function writeFileFromSDCard(param) { var writer = new FileWriter("/sdcard/write.txt"); writer.write(param + "\n", false); alert("file Written to SD Card"); } </script> 
+2
source

I would try using the FileReady and FileWriter APIs.

http://docs.phonegap.com/phonegap_file_file.md.html#FileReader

+1
source

Here is what I came up with based on a few links. I was looking for this too. I used this site as a link http://www.digitalnoiz.com/mobile-development/mobile-file-explorer-with-phonegapcordova-and-jquery-mobile-part-1/ , as well as Phonegap docs api

 function displayMessage(msg) { navigator.notification.alert(msg); } function loadDirectories(fileSystem) { directoryEntry = fileSystem.root; var directoryReader = directoryEntry.createReader(); directoryReader.readEntries(function(entries){ var sOutput = ""; for(var i=0; i < entries.length; i++) { if(!entries[i].isDirectory) { fileSystem.root.getFile(entries[i].name,null,gotFileEntry,fail); } } //displayMessage(sOutput); },fail); } function gotFileEntry(fileEntry) { fileEntry.file(function(file){ var reader = new FileReader(); reader.onloadend = function(evt){ displayMessage(evt.target.result); }; reader.readAsText(file); },fail); } function failFile(evt) { displayMessage(evt.target.error.code); } function fail(error) { displayMessage("Failed to list directory contents: " + error.code); } function onBodyLoad() { document.addEventListener("deviceready", onDeviceReady, false); } function onDeviceReady() { window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, loadDirectories, fail); } 
+1
source

Source: https://habr.com/ru/post/894446/


All Articles