简体   繁体   中英

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM