I'm struggling to download a simple text generated by cordova app to device. It is simple in browser, but I can't make it work on Android (see Cordova file plugin not working when I try to download img on android). Is there an easier way to save file on disk? Or maybe get access to database?
I am at my whit's end.
CodePudding user response:
Have you try to use :
window.requestFileSystem
or
window.resolveLocalFileSystemURL
Where is you text generated ? In memory or locally into device ?
CodePudding user response:
It can be done with the File plugin, but there are a few moving parts to get it working properly:
- You need to write to a sandboxed directory (either
cordova.file.externalDataDirectory
orcordova.file.dataDirectory
), as of Android 10/11. - You need to add
WRITE_EXTERNAL_STORAGE
permission to yourconfig.xml
(to add to your Android app'smanifest.xml
) - You need to have the appropriate runtime permission as well if you're accessing external storage.
(see How to get documents in an android directory that PhoneGap will see for code snippets)
- Having done that, you'll be working with the FileWriter to write the text to a Blob.
If you need a leg up, you can take a look at my app (I've got a text writer at about line 3239 of this file: https://github.com/adapt-it/adapt-it-mobile/blob/master/www/js/views/DocumentViews.js). It also handles copying the text to the clipboard using the Cordova-clipboard
plugin, which might be of use.