读/写在科尔多瓦混​​合Android应用到文件 [英] write/read to file in cordova hybrid android app

查看:118
本文介绍了读/写在科尔多瓦混​​合Android应用到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够读/写在科尔多瓦应用到文件。

我已经做了步骤:结果
 1 ..>科尔多瓦创建的应用程序结果
 2.应用>科尔多瓦平台添加的Andr​​oid结果
 3.应用>科尔多瓦插件添加 https://开头git- wip-us.apache.org/repos/asf/cordova-plugin-file.git 结果
 4. 在这里我添加以下code到项目的,并添加到清单:

 <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>

和添加到config.xml文件:

 <功能名称=文件>
    < PARAM NAME =Android的包值=org.apache.cordova.FileUtils/>
< /功能>

5.app>科尔多瓦构建结果
 6.应用>科尔多瓦运行Android结果
 7.的我的Andr​​oid设备上的输出:

  A。 onready
湾失败:类没有找到。

为什么我得到这个错误在该行:window.requestFileSystem(...)

 <脚本类型=文/ JavaScript的字符集=utf-8SRC =cordova.js>< / SCRIPT>
<脚本类型=文/ JavaScript的字符集=utf-8>//等待设备API库加载
//
document.addEventListener(deviceready,onDeviceReady,FALSE);//设备API可用
//
功能onDeviceReady(){
    警报('onready');
    window.requestFileSystem(LocalFileSystem.PERSISTENT,0,gotFS,失败);
}功能gotFS(文件系统){
    警报('gotFS');
    fileSystem.root.getFile(readme.txt文件,{创建:真实,独家:假},gotFileEntry,失败);
}功能gotFileEntry(FileEntry的){
    fileEntry.createWriter(gotFileWriter,失败);
}功能gotFileWriter(作家){
    writer.onwriteend =功能(EVT){
        的console.log(文件的内容,现在一些示例文本');
        writer.truncate(11);
        writer.onwriteend =功能(EVT){
            的console.log(文件的内容,现在一些样本');
            writer.seek(4);
            writer.write(不同的文本);
            writer.onwriteend =功能(EVT){
                的console.log(文件的内容现在'一些不同的文本');
            }
        };
    };
    writer.write(一些示例文本);
}功能失效(错误){
    警报('失败:'+错误code);
}< / SCRIPT>


解决方案

你可能丢失在 cordova.js 文件 WWW / 。从复制平台/安卓/ platform_www / cordova.js 。为我工作。

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.

这篇关于读/写在科尔多瓦混​​合Android应用到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆