Phonegap-如何将.txt文件保存在android mobile的根目录中 [英] Phonegap - How to save .txt file in root directory of android mobile

查看:111
本文介绍了Phonegap-如何将.txt文件保存在android mobile的根目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用phonegap将.txt文件保存在android mobile的根目录中. 我已经安装了这些插件"cordova-plugin-file"和"cordova-plugin-file-transfer".在config.xml文件中,我添加了此首选项.

I m trying to save a .txt file in root directory of android mobile using phonegap. I have installed these plugins 'cordova-plugin-file' and 'cordova-plugin-file-transfer'. In config.xml file i add this preference.

<preference name="AndroidPersistentFileLocation" value="Internal" />

这是我的代码.

<button class="btn-lg btn-success" onclick="saveFile('hello_world.txt', 'This is Dummy Content')">Write File</button>

用于处理点击事件的代码.

Code to handle on click event.

function saveFile(fileName, fileData) {
    // Get access to the file system
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
        // Create the file.
        fileSystem.root.getFile(fileName, {create: true, exclusive: false}, function (entry) {
            // After you save the file, you can access it with this URL
            var myFileUrl = entry.toURL()
            entry.createWriter(function (writer) {
                writer.onwriteend = function (evt) {
                    alert("Successfully saved file to " + myFileUrl)
                }
                // Write to the file
                writer.write(fileData)
            }, function (error) {
                alert("Error: Could not create file writer, " + error.code)
            })
        }, function (error) {
            alert("Error: Could not create file, " + error.code)
        })
    }, function (evt) {
        alert("Error: Could not access file system, " + evt.target.error.code)
    })
}

成功回调后,它将返回此文件位置.

After success callback it return this file location.

file:///data/data/com.adobe.phonegap.app/files/files/hello_world.txt

但是当我尝试在给定位置查找此文件时.它不在那里.我希望此文件位于我的手机根目录中.

But when i try to find this file in given location. Its not there. I want this file in my root directory of mobile.

推荐答案

查看此链接包含工作样本.

该链接包含用于文件下载,保存和打开的示例cordova应用程序.还支持文件夹删除选项.

The link contains sample cordova app for file download, save and open. Supports folder deletion option as well.

查看自述文件部分.希望能帮助到你. 您也可以查看 SO Post 以获得更多信息.

Check out the readme section. Hope it helps. Also you can check out this SO Post for more info on this.

这篇关于Phonegap-如何将.txt文件保存在android mobile的根目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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