I am trying to save a .txt file to the root directory of an Android mobile phone using a phone splash screen. I installed these plugins "cordova-plugin-file" and "cordova-plugin-file-transfer". In the config.xml file, I add this preference.
<preference name="AndroidPersistentFileLocation" value="Internal" />
Here is my code.
<button class="btn-lg btn-success" onclick="saveFile('hello_world.txt', 'This is Dummy Content')">Write File</button>
The code to handle the click event.
function saveFile(fileName, fileData) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
fileSystem.root.getFile(fileName, {create: true, exclusive: false}, function (entry) {
var myFileUrl = entry.toURL()
entry.createWriter(function (writer) {
writer.onwriteend = function (evt) {
alert("Successfully saved file to " + myFileUrl)
}
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 a successful callback, it returns this file location.
file:///data/data/com.adobe.phonegap.app/files/files/hello_world.txt
But when I try to find this file in this place. He is not there. I want this file in my mobile root directory.
source
share