Search code examples
androidcordovapathcordova-pluginsfilepath

Custom cordova plugin - Get path to file


I've created a Cordova plugin for Android, and I've put a file in the src folder:

Plugin.xml

<source-file src="myfile.ext" target-dir="src/com/example"/>

I also can see (in Android Studio) that the file was succesfully added in the src folder:

android
 |-- /src
      |-- com
           |-- example
                |-- myfile.ext
                |-- MyPlugin.java

Now I need to be able to get the path to this file in MyPlugin.java, but I've no clue how to do this. Can anybody help me with this? Thanks!


Solution

  • Before you updated your question, it was correct - you should copy the asset file to "assets" not "src":

    <source-file src="myfile.ext" target-dir="assets"/>
    

    Then you can reference it via the AssetManager:

    AssetManager assetManager = this.cordova.getActivity().getAssets();
    InputStream inputStream = assetManager.open("myfile.ext");
    

    In terms of "path" to the file, assets are stored in the APK differently from how your Android project is constructed, so the "path" to your file would be file:///android_asset/myfile.ext, but you most likely wouldn't actually be referencing it like this from MyPlugin.java.