【问题标题】:How to write to a text file in a specific path in android device如何在android设备的特定路径中写入文本文件
【发布时间】:2016-01-09 20:48:32
【问题描述】:

我正在使用 Visual Studio 创建 Cordova 移动应用程序,并且我正在使用 Cordova-plugin-file 在设备上进行文件操作。 如何在android设备的特定路径中写入文本文件, 以便能够从固定位置获取它。

【问题讨论】:

  • 阅读this
  • 您觉得我的回答有用吗?如果您觉得答案有用,您可以通过点击答案附近的向上箭头和复选标记来投票并接受它:)

标签: android file cordova mobile


【解决方案1】:

你可以试试下面的代码。您还可以在 Cordova Example: Writing to a fileExploring the FileSystem APIs 中找到代码的很好解释。

作为一个非常简短的解释:你应该安装cordova文件插件,在设备准备好后,你首先会要求一个文件系统,然后在一个特定的路径中创建文件(在我的例子中:cordova.file.dataDirectory),最后会写入它。

document.addEventListener('deviceready', function () {
           window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (dirEntry) {
               dirEntry.getFile("log.txt", { create: true }, function (fileEntry) {
                    fileEntry.createWriter(function (fileWriter) {

                        fileWriter.onwriteend = function (e) {
                            console.log('Write completed.');
                        };

                        fileWriter.onerror = function (e) {
                            console.log('Write failed: ' + e.toString());
                        };

                        // Create a new Blob and write it to log.txt.
                        var blob = new Blob(['Lorem Ipsum'], { type: 'text/plain' });

                        fileWriter.write(blob);

                    }, errorHandler);
                });
            });

 }, false);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    相关资源
    最近更新 更多