Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: how to load xml file from assets directory?

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

like image 801
freeaks Avatar asked Apr 05 '11 20:04

freeaks


People also ask

How do I open an asset file on Android?

Step 3 – Right click app >> New >> Folder >> Assets folder. Right click on the assets folder, select New >> file (myText. txt) and your text.

How do I use an assets folder?

Step 1: To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder. Step 3: Android Studio will open a dialog box. Keep all the settings default.

What is the use of assets folder in Android?

Assets provide a way to include arbitrary files like text, xml, fonts, music, and video in your application. If you try to include these files as "resources", Android will process them into its resource system and you will not be able to get the raw data.

Where are Android assets stored?

I noticed that , when an android app is installed, It's apk file is copied to "data/app" directory. If I open it from there - just selecting view, I see assets folder which contains my files. That is correct. The APK is copied there during installation, but the asset files are left inside it.


1 Answers

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.

like image 135
GrAnd Avatar answered Oct 08 '22 06:10

GrAnd