Home > Blockchain >  How can I easily download a text file on android using javascript and cordova?
How can I easily download a text file on android using javascript and cordova?

Time:10-18

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:

  1. You need to write to a sandboxed directory (either cordova.file.externalDataDirectory or cordova.file.dataDirectory), as of Android 10/11.
  2. You need to add WRITE_EXTERNAL_STORAGE permission to your config.xml (to add to your Android app's manifest.xml)
  3. 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)

  1. 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.

  • Related