我正在尝试使用phonegap 将.txt 文件保存在Android 手机的根目录中。
我已经安装了这些插件“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
但是当我尝试在给定位置找到该文件时。它不在那里。我希望这个文件位于我的手机根目录中。