我试图使用phonegap将.txt文件保存在android mobile的根目录中.
我已经安装了这些插件’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
但是当我尝试在给定位置找到此文件时.它不在那里.我希望这个文件在我的移动根目录中.
解决方法:
查看包含工作样本的link.