tags:

views:

18

answers:

1

I'm working in a group right now, writing an app that needs to be able to reference a file which is not written at runtime. Essentially, the file stores data which the app (a game) uses at certain points.

I don't have any trouble saving files at runtime and then referencing those files, but for some reason, I'm having trouble referencing files using a filepath.

The code which is giving me problems is this:

    File abilityFile = new File("/assets/xml/abilities.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document doc = builder.parse(abilityFile);

On the last line in that block, I get an IOError. I'm assuming that either the way that I'm storing the file or the way I'm referencing it doesn't work on Android phones, but I don't know.

If I'm right, is there a way to store a file (which isn't written and saved at runtime) so it can be accessed by the app? How would I do that?

If I'm wrong, what am I doing wrong? It's probably something simple that I'm missing, but I've tried looking for solutions, and wasn't able to find much about this specific problem. I found a lot of information on writing files, or referencing files stored externally, but not on referencing files which are meant to be stored in a directory with the app.

Thank you.

+1  A: 

I recommend you place the xml file in resources/xml instead of resources/assets. See this: http://developer.android.com/guide/topics/resources/providing-resources.html

If you are set on using the assets directory, you need to use the AssetManager class to retrieve that file, instead of creating a File object. http://developer.android.com/reference/android/content/res/AssetManager.html

mbaird