Search code examples
androidcordovacordova-plugins

How to load data file in Cordova plugin?


I am developing a Cordova plugin that comes with some data files. I need these files to

  1. Be copied to the mobile device, and
  2. Be loaded the files in the runtime.

What I am currently doing is to define these files in plugin.xml as resource files, like:

<resource-file src="assets/data.xml" target="assets/data.xml"/>

However, I do not know how to open these files in my Android Java code. If I use a File class to open the file, what path should I use?

Any help is appreciated.


Solution

  • Since no one has answered this question after a few days, and I have not found a solution elsewhere, I am going to post my own workaround here.

    1. Package all the data files with directory in a zip file.
    2. Put zip file in the Cordova app.
    3. When the app starts up for the 1st time, the app uploads the zip file to the plug-in, using a FileTransfer plug-in.
    4. The plug-in unzips the file in the application's sandbox.

    I have to admit that this is not a decent solution. It makes the plug-in depend on the app. However, if there is no better solution, this one just works.

    Still, if there is a better solution, please post share it here.