我正在尝试读取 cordova 中的文本文件。为此,我创建了一个assets 文件夹(请参阅cordova 文件夹结构 图片)并将我的文本文件放入其中,但我收到错误消息java.lang。 io.FileNotFoundException: json.txt
.
注意: 当我在 Android native 中尝试时,我能够读取文件...
代码
String yourData = LoadData("json.txt");
public String LoadData(String inFile) {
String tContents = "";
try {
InputStream stream = context.getAssets().open(inFile);
int size = stream.available();
byte[] buffer = new byte[size];
stream.read(buffer);
stream.close();
tContents = new String(buffer);
} catch (IOException e) {
e.printStackTrace();
Log.e("catch", e + "");
}
return tContents;
}
Cordova 文件夹结构
我是不是将 assets 文件夹放在了错误的位置,还是我犯了其他任何错误?
最佳答案
实际问题是,plugin.xml
中没有提到 assets 文件夹。提到它后一切正常
plugin.xml 条目
<source-file src="json.txt" target-dir="assets"/>
并使用问题中相同的方法和结构
关于android - cordova中的 Assets 文件夹结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48336584/