简体   繁体   English

自定义cordova插件-获取文件路径

[英]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: 我已经为Android创建了Cordova插件,并将文件放在src文件夹中:

Plugin.xml 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 Studio中)该文件已成功添加到src文件夹中:

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. 现在,我需要能够在MyPlugin.java中获取此文件的路径,但是我不知道如何执行此操作。 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": 在更新问题之前,它是正确的-您应该将资产文件复制到“资产”而不是“ src”:

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

Then you can reference it via the AssetManager : 然后,您可以通过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 . 就文件的“路径”而言,资产在APK中的存储方式与Android项目的构造方式不同,因此文件的“路径”应为file:///android_asset/myfile.ext ,但您最有可能实际上不会从MyPlugin.java引用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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