除非我尝试打开res/raw
文件夹中的文件。
InputStream in = Resources.getSystem().openRawResource(R.raw.eng_sample);
例外情况是:
android.content.res.Resources$NotFoundException: Resource ID #0X89890
我可以在 res/raw/eng_sample.txt
的 apk 中看到文件eng_sample.txt
。
最重要的是,当我做R.raw.
并按Ctrl-space
文件时,eng_sample
作为建议给出。
那么问题出在哪里呢?我该如何解决这个问题?
提前感谢。
附言
我还尝试将文件eng_sample.txt
放入main/assets
。然后使用
InputStream in = context.getAssets().open("file:///android_asset/eng_sample.txt
但现在我得到一个 IO 异常。
也许有人可以告诉我如何使这种方法而不是上面的方法起作用?
我一直在出于同样的目的使用此代码。你可以做这样的事情
BufferedReader mReader = null;
try {
mReader = new BufferedReader(
new InputStreamReader(getAssets().open("eng_sample.txt")));
String mLine;
while ((mLine = mReader.readLine()) != null) {
//do your stuff with line
...
}
} catch (IOException e) {
//print stack trace
} finally {
if (mReader != null) {
try {
mReader.close();
} catch (IOException e) {
//print stack trace
}
}
}
希望对您有所帮助。
Resources.getSystem()
返回一个共享的全局资源对象,该对象仅提供对系统资源(无应用程序资源)的访问。
此外,context.getAssets().open(fileName/*not path*/)
用于从 assets 目录中获取文件,您应该只提供文件名,而不是整个路径。
要从原始文件打开流,请使用:
context.getResources().openRawResource(R.raw.eng_sample);