0
votes

I am trying to create a file on Android using:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
    fileSystem.root.getFile("example.txt", {
        create: true
    }, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
    fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
    writer.onwriteend = function(evt) {
        console.log("contents of file now 'some sample text'");
        writer.truncate(11);
        writer.onwriteend = function(evt) {
            console.log("contents of file now 'some sample'");
            writer.seek(4);
            writer.write(" different text");
            writer.onwriteend = function(evt) {
                console.log("contents of file now 'some different text'");
            }
        };
    };
    writer.write("some sample text");
}

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

This code works fine on iOS and I can see the file getting created inside Documents folder. However on Android I do not see any file.

I have added all the plugins. Also I have taken the following steps in order

  1. Add plugins cordova plugin add org.apache.cordova.file cordova plugin add org.apache.file-transfer

  2. Compile and build android from within my app cordova compile android cordova build android

Also my config file has following entries:

<name>Hello Cordova</name>

<description>
    A sample Apache Cordova application that responds to the deviceready event.
</description>



<access origin="*"/>


<content src="index.html" />

<!-- Preferences for iOS -->
<preference name="AllowInlineMediaPlayback" value="false" />
<preference name="AutoHideSplashScreen" value="true" />
<preference name="BackupWebStorage" value="cloud" />
<preference name="DisallowOverscroll" value="false" />
<preference name="EnableLocation" value="false" /><!-- DEPRECATED -->
<preference name="EnableViewportScale" value="false" />
<preference name="FadeSplashScreen" value="true" />
<preference name="FadeSplashScreenDuration" value=".25" />
<preference name="HideKeyboardFormAccessoryBar" value="false" />
<preference name="KeyboardDisplayRequiresUserAction" value="true" />
<preference name="KeyboardShrinksView" value="false" />
<preference name="MediaPlaybackRequiresUserAction" value="false" />
<preference name="ShowSplashScreenSpinner" value="true" />
<preference name="SuppressesIncrementalRendering" value="false" />
<preference name="TopActivityIndicator" value="gray" />
<preference name="iosPersistentFileLocation" value="Library" />


<feature name="Geolocation">
  <param name="ios-package" value="CDVLocation"/>
</feature>
<feature name="Device">
  <param name="ios-package" value="CDVDevice"/>
</feature>
<feature name="Accelerometer">
  <param name="ios-package" value="CDVAccelerometer"/>
</feature>
<feature name="Compass">
  <param name="ios-package" value="CDVLocation"/>
</feature>
<feature name="Media">
  <param name="ios-package" value="CDVSound"/>
</feature>
<feature name="Camera">
  <param name="ios-package" value="CDVCamera"/>
</feature>
<feature name="Contacts">
  <param name="ios-package" value="CDVContacts"/>
</feature>
<feature name="File">
  <param name="ios-package"  value="CDVFile"/>
</feature>
<feature name="NetworkStatus">
  <param name="ios-package" value="CDVConnection"/>
</feature>
<feature name="Notification">
  <param name="ios-package" value="CDVNotification"/>
</feature>
<feature name="FileTransfer">
  <param name="ios-package" value="CDVFileTransfer"/>
</feature>
<feature name="Capture">
  <param name="ios-package" value="CDVCapture"/>
</feature>
<feature name="Battery">
  <param name="ios-package" value="CDVBattery"/>
</feature>
<feature name="SplashScreen">
  <param name="ios-package" value="CDVSplashScreen"/>
</feature>
<feature name="Echo">
  <param name="ios-package" value="CDVEcho"/>
</feature>
<feature name="Globalization">
  <param name="ios-package" value="CDVGlobalization"/>
</feature>
<feature name="InAppBrowser">
  <param name="ios-package" value="CDVInAppBrowser"/>
</feature>
<feature name="Logger">
  <param name="ios-package" value="CDVLogger"/>
</feature>
<feature name="LocalStorage">
    <param name="ios-package" value="CDVLocalStorage"/>
</feature>
<feature name="File">
    <param name="android-package" value="org.apache.cordova.file.FileUtils" />
</feature>
<feature name="FileTransfer">
    <param name="android-package" value="org.apache.cordova.filetransfer.FileTransfer" />
</feature>
<!-- Deprecated plugins element. REmove in 3.0 -->
<plugins>

</plugins>

I have in my Android manifest file the necessary permissions:

The following is my version of Cordova: 3.5.0-0.2.6

Everything works fine on iOS. On Android Logcat gives the following error:

"Uncaught ReferenceError: LocalFileSystem is not defined", source: file??android_asset/www/app.js

2

2 Answers

0
votes

You cannot mix up between iOS and android in the same config.xml file. Make sure you setup cordova plugin in separate config.xml

iOS

enter image description here

android

enter image description here

0
votes

It was basically in the way plugins are being added. I find it really confusing the documents given on phonegap and cordova websites. I probably had to follow them both to make it work. To add the necessary plugins info in plugins.xml you need to follow this:

http://docs.phonegap.com/en/2.0.0/cordova_file_file.md.html#File

And for making changes to the config file you need to follow this:

http://cordova.apache.org/docs/en/3.3.0/cordova_file_file.md.html#File

and then compile and run. I am not sure if there is any place where I can find a unified document. Please let me know if you know of any unified document which combines it all. I am using Phonegap 3.5 and cordova 3.5 version and looking at old documents to figure out a way