android - 在 cordova 混合 android 应用程序中写入/读取文件

标签 android cordova io hybrid-mobile-app

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

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

<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. app>cordova 运行 android
7. 在我的 android 设备上输出:

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>

最佳答案

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

关于android - 在 cordova 混合 android 应用程序中写入/读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22170064/

相关文章:

java - 为什么缓冲区大小对此文件读取操作没有影响

C++从二进制文件写入和读取 double

android - ContentResolver.openAssetFileDescriptor() 在 Android 12 中需要很长时间

java - 使用 DownloadManager 将文件下载到真正的 SD 卡,而不是模拟存储

cordova - 为什么我用 Apache Cordova 构建的 Windows 10 通用应用程序的任务栏图标的大小比其他应用程序小

javascript - Mobile Safari - "touchend"事件在最后一次触摸被移除时没有触发?

java - 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

java - 保存/加载控制数据的推荐读/写实践

android - Firebase 动态链接和 android 导航架构组件

facebook - Cordova /Phonegap : Can't get Facebook Phonegap plugin to work