1
votes

I want to be able to write/read to file in cordova app.

the steps I've did:
1. ..>cordova create app
2. app>cordova platform add android
3. app>cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-file.git
4. here I've added following code to the project and added to the manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

and added to config.xml:

<feature name="File">
    <param name="android-package" value="org.apache.cordova.FileUtils" />
</feature>

5.app>cordova build
6. app>cordova run android
7. output on my android device:

a. onready    
b. fail: Class not found.

Why I get this error in the line: window.requestFileSystem(...)?

<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('onready');
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
    alert('gotFS');
    fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, 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) {
    alert('fail: '+error.code);
}

</script>
1
Did u add file plugin ?Divesh Salian
@Divesh Salian, yep. forgot to write that. i will edit the question31415926
So ur device ready alert gets called rgt ?? and also share ur logcat detailsDivesh Salian

1 Answers

2
votes

You are probably missing the cordova.js file in www/. Copy it from platforms/android/platform_www/cordova.js. Worked for me.