【问题标题】:write/read to file in cordova hybrid android app在科尔多瓦混合安卓应用程序中写入/读取文件
【发布时间】:2014-04-05 21:02:14
【问题描述】:

我希望能够在 cordova 应用程序中写入/读取文件。

我做过的步骤:
1. ..>cordova 创建应用程序
2.app>cordova平台添加android
3.app>cordova插件添加https://git-wip-us.apache.org/repos/asf/cordova-plugin-file.git
4. 这里我已将以下代码添加到项目中并添加到清单中:

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

并添加到 config.xml:

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

5.app>cordova 构建
6. 应用>cordova 运行安卓
7. 在我的安卓设备上输出:

a. onready    
b. fail: Class not found.

为什么我在以下行中收到此错误: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>

【问题讨论】:

  • 你添加文件插件了吗?
  • @Divesh Salian,是的。忘了写那个。我将编辑问题
  • 所以你的设备就绪警报被称为 rgt ??并分享你的 logcat 详细信息

标签: android cordova io hybrid-mobile-app


【解决方案1】:

您可能缺少www/ 中的cordova.js 文件。从platforms/android/platform_www/cordova.js 复制它。为我工作。

【讨论】:

  • 不记得问题出在哪里(这是 2 个月前),但我认为您是对的。
猜你喜欢
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
  • 2019-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多