我有一个android应用程序,我正在尝试获取SAX解析器的文件路径。我有以下结构:
assets:(Where my xml file is)
src(same level as assets)
com
msi
androidrss(The calling java file is in here)我尝试了几种不同的版本:
InputSource is = new InputSource("file://../../../../assets/Rss.xml");但我总是能拿到FNF Exception
有什么建议吗?
发布于 2011-03-11 08:08:39
当您从APK运行时,不能使用File API来获取assets目录中的数据。您必须使用Android - AssetManager类。从Context对象中获取AssetManager类的一个实例。请参阅Context object
发布于 2011-03-11 08:12:14
将xml放在/res/raw,并使用以下命令打开它:
getResources().openRawResource(R.raw.xmlName);发布于 2011-03-11 11:31:06
要详细说明@typo.pl的答案:
InputStream in = getContext().getAssets().open("Rss.xml");
// test that in is not null
try {
// set up your SAX parser and handler
parser.parse(in, handler);
} finally {
try { in.close(); }
catch (IOException ignored) {}
}https://stackoverflow.com/questions/5267483
复制相似问题