How to refer asset files in android?

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??

I’m not able to access any such files in android, which I can access using path relative to root project directory

eg
[java] DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("./assets/test_forms/module" + mId + “.xml”));
[/java]

How to should I create a file to a modules.xml in my assets directory in jmonkey??

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.

pspeed, I got your point but assetmanager wont load my xml files.
Just to make sure I tried and I got the following exception in my log

Code:
[java] File f = (File)app.getAssetManager().loadAsset(“assets/modules.xml”);
Document doc = docBuilder.parse(f );
[/java]

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)

Enough of hints… Thanks a lot
I was already looking into classpathlocator.java

http://hub.jmonkeyengine.org/javadoc/com/jme3/asset/AssetManager.html#locateAsset(com.jme3.asset.AssetKey)
http://hub.jmonkeyengine.org/javadoc/com/jme3/asset/AssetInfo.html#openStream()

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::

  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?
@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?
1 - Yes 2 - In the resources folder 3 - No 4 - No
@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??

[java]private void save(String filename, String data)
{
try
{
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(data.getBytes());
fos.close();
toast(“File successfully saved.”);
}
catch (Exception ex)
{
toast("Error saving file: " + ex.getLocalizedMessage());
}
}[/java]

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.

@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.

Thanks a lot.
It works awesome…