【发布时间】:2017-01-05 07:33:02
【问题描述】:
我正在尝试使用 phonegap 在 android 手机的根目录中保存一个 .txt 文件。 我已经安装了这些插件“cordova-plugin-file”和“cordova-plugin-file-transfer”。在 config.xml 文件中,我添加了此首选项。
<preference name="AndroidPersistentFileLocation" value="Internal" />
这是我的代码。
<button class="btn-lg btn-success" onclick="saveFile('hello_world.txt', 'This is Dummy Content')">Write File</button>
处理点击事件的代码。
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)
})
}
成功回调后返回此文件位置。
file:///data/data/com.adobe.phonegap.app/files/files/hello_world.txt
但是当我尝试在给定位置找到这个文件时。它不在那里。我想要这个文件在我的手机根目录中。
【问题讨论】:
-
您查看了我在回答中发布的工作示例吗?
标签: android cordova phonegap-plugins phonegap-build