问题
i have trouble loading an xml file from assets directory. using the same line of code (just changing the path) i get different results ( either ok or NPE / file corrupted ) the file "castle1.tmx" (it's an xml file) is copied in two locations:
- res/xml/castle1.tmx
- assets/level/castle1.tmx
with this line, it works:
XmlResourceParser xrp = ctx.getAssets().openXmlResourceParser("res/xml/castle1.tmx");
while with this line it doesn't:
XmlResourceParser xrp = ctx.getAssets().openXmlResourceParser("assets/level/castle1.tmx");
i get the following result:
04-05 21:46:40.940: WARN/ResourceType(29056): Bad XML block: header size 28024 or total size 1702240364 is larger than data size 70441
04-05 21:46:40.940: ERROR/TestParser(29056): Unable to read resource file
04-05 21:46:40.940: WARN/System.err(29056): java.io.FileNotFoundException: Corrupt XML binary file
04-05 21:46:40.940: WARN/System.err(29056): at android.content.res.AssetManager.openXmlAssetNative(Native Method)
04-05 21:46:40.944: WARN/System.err(29056): at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:485)
04-05 21:46:40.944: WARN/System.err(29056): at android.content.res.AssetManager.openXmlResourceParser(AssetManager.java:453)
04-05 21:46:40.944: WARN/System.err(29056): at android.content.res.AssetManager.openXmlResourceParser(AssetManager.java:442)
04-05 21:46:40.944: WARN/System.err(29056): at game.test.MapLoader.<init>(MapLoader.java:73)
file is found in both case... it's just that i cannot seem to be able to read it from asset dir using that method..
any ideas how can i load my xml file from assets directory ?
tnx
回答1:
In res/
folder all xml files are precompiled, whereas in assets/
folder they are not. So, you can't use openXmlResourceParser()
with non-precompiled resources. Instead use open()
and read file through InputStream
.
回答2:
i succeded at loading and parsing my xml file from assets directory (assets/level/castle1.tmx)
here's what i did:
replaced this:
XmlResourceParser xrp = ctx.getResources().getXml(ctx.getResources().getIdentifier(name, "xml", ctx.getPackageName()));
by this:
InputStream istr = context.getAssets().open("level/"+name+".tmx");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
xrp = factory.newPullParser();
xrp.setInput(istr, "UTF-8");
then all i had to do was editing some getAttributeIntValue() lines:
int a = xrp.getAttributeIntValue(null, "width",0));
into this:
int a = Integer.parseInt(xrp.getAttributeValue(null, "width"));
and all the rest worked without modifications :) ..this class is for parsing tiled xml/map files to build my game levels. before, it worked using res/ but i wanted to try to put all my files into assets/ instead. so now it works :)
thanks for the help
回答3:
The reason is because you are trying to load a binary XML file (your error is java.io.FileNotFoundException: Corrupt XML binary file
).
All of the Android XML files (layouts, strings etc) stored in res
are automatically compiled to binary XML when your project is compiled. XML files in assets are treated as standard XML files and so are not compiled to binary XML.
In summary: Android XML files must be in the res
folder. You can only store plain-text XML in the assets folder (not layout files and such-like).
回答4:
Have a look at
https://github.com/pilhuhn/TurtleCar/blob/master/src/de/bsd/turtlecar/Board.java#L30 which is called from https://github.com/pilhuhn/TurtleCar/blob/master/src/de/bsd/turtlecar/SampleView.java#L45
for an example.
Basically you need to ask the AssetManager
for the file:
AssetManager assetManager = context.getAssets();
try {
InputStream is = assetManager.open("1.xml");
....
来源:https://stackoverflow.com/questions/5557885/android-how-to-load-xml-file-from-assets-directory