質問

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>
役に立ちましたか?

解決

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top