Hi,
I have used nifty in my jmonkey game. I have referred to image files (Texture/back.png) in nifty XML files.
<image filename=“Textures/back.png” width=“100%” height=“100%”></image>
It works absolutely fine in desktop application. However when I run in android, these files become inaccessible. Where should I keep these files to make them accessible in android??
You would have this same problem on desktop if you tried to package your application for release. Assets are no longer files once your app has been packaged. They are put into an assets.jar.
Use AssetManager to find your data and open a stream.
Exception:
[java]In MainMenuAppState: Please double check your modules XML files: java.lang.IllegalStateException: No loader registered for type "xml
[/java]
You need to create a class that implements AssetLoader and register it to the .xml extension using assetManager.
Inside the your custom AssetLoader is where you will use the code docBuilder.parse().
-Alternatively- if you dont want to integrate with the assetManager, you can get the URL of the classpath resource, and then turn that in to a File object… which you could then do docBuilder.parse() on that file object…
(however the purpose of the assetManager is to handle all of that file location juggling and management for you, itd probably be worthwhile to learn to use it)
@icamefromspace said:
You need to create a class that implements AssetLoader and register it to the .xml extension using assetManager.
Inside the AssetLoader is where you will use the code docBuilder.parse().
Alternatively if you dont want to integrate with the assetManager, you can get the URL of the classpath resource, and then turn that in to a File… when you could then do docBuilder.parse() on that file object…
(which is essentially what the ClasspathLocator does for you thats regstered by default with the assetManager)
How should I get the URL of the classpath resource on android??
Is it possible through AssetManager???
I would be grateful if you could provide some code…
its not exactly a one liner. You might want to look up java IO stuff. this isn’t really even a jmonkey topic this is a general java topic.
essentially what you’ll be doing (if you opt to not use the assetManager) is something like this: (not actual working code here)
[java]
URL url = getClass().getResource(“blah/blah/blah.xml”);
File f = new FIle(url);
docBulder.parse(f);
[/java]
Just google for topics like “reading files from class path” you’ll probably find a few different ways to do it including various caveats. Also look at the source code of ClasspathLocator.java in the jmonkey project (which you can find on jmonkey’s google code web page)
Implementing the AssetLoader was most simple
Thanks a lot for help!!
Although javadoc suffices, but I still wish if someone from jmonkey can write some high level abstract and big picture workings of how different subsystems work. A pre-read for people who want to dive into code. it could be holy grail for beginners like me.Just giving my feedback…
@pspeed said:
You would have this same problem on desktop if you tried to package your application for release. Assets are no longer files once your app has been packaged. They are put into an assets.jar.
Use AssetManager to find your data and open a stream.
I have some doubts on this::
Is everything in my assets folder copied to assets.jar??
Where is this assets.jar located on android?
Is it possible to read the contents of assets.jar manually using a ZipLocator??
@simar.i3r said:
I have some doubts on this::
1. Is everything in my assets folder copied to assets.jar??
2. Where is this assets.jar located on android?
3. Is it possible to read the contents of assets.jar manually using a ZipLocator??
4. can I write new files in assets.jar?
@normen said:
1 - Yes
2 - In the resources folder
3 - No
4 - No
Then where and how should I create new file which I can read later.
I have seen android specific code on net where we need android context for File IO.
How should I manage that in jMonkey application??
@normen said:
The global android storage folder is part of the assetManager root. Just store your files there (see SaveGame utility / JmeSystem.getStorageFolder())
Btw, using File() is almost always wrong, file access is very different between operating systems.