0

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!

user3050534
  • 227
  • 4
  • 17

1 Answers1

0

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.

DaveAlden
  • 30,083
  • 11
  • 93
  • 155
  • Thanks for your reply @DaveAlden, I really need to get the "path" because I need that as input for an API call. Anyway, it's not working unfortunately.. when I try `new File("file:///android_asset/myfile.ext").exists()` it returns `false`. Also doing `new File("file:///android_asset/").exists()` returns `false`. Any idea why? (I added the file back under `assets` folder) – user3050534 May 18 '17 at 13:37
  • `File file = new File("//android_asset/myfile.ext"); Uri uri = Uri.fromFile(file);` – DaveAlden May 18 '17 at 13:46
  • Still no luck, the `uri` is exactly the same as `file:///android_asset/myfile.ext` and will return `false` when trying to do `new File(uri.toString).exists()`. Reason I make a `new File()` and check if it exists is because the API will do that also. Other suggestions? Reason I editted my original post to maybe place the file in the same folder (src) as the java file was so it could potentionally be easier to get the path. If that's possible/easier, what would the path be? – user3050534 May 18 '17 at 14:08
  • You need check where the API you are using expects the file to be located and what syntax the input path should be. In my plugins I use `AssetManager` for all asset file access. Assets are not unpacked from the APK, so are not available via the File API. – DaveAlden May 18 '17 at 14:15
  • It's this API, https://github.com/rmtheis/tess-two/blob/master/tess-two/src/com/googlecode/tesseract/android/TessBaseAPI.java, when trying to call the `init` method (line 299), it returns `"Data path does not exist!"`. I don't think it expects a specific location, as long as it exists. – user3050534 May 18 '17 at 14:27
  • Then I would still bundle the file as an asset within the APK, but copy it out of the APK to sdcard at runtime, making it accessible as a file - see [How to copy files from 'assets' folder to sdcard](http://stackoverflow.com/questions/4447477/how-to-copy-files-from-assets-folder-to-sdcard). Once it's a file on the SD card, you can get its absolute path. – DaveAlden May 18 '17 at 14:35
  • Thanks, I copied it to sdcard and I can use it. I will accept this as the answer since now I can continue. – user3050534 May 19 '17 at 09:26