tags:

views:

681

answers:

2

Hi Friends,

I'm working on an app for Android with a few other people, and the primary content is specified by our designers as text files in a certain file format, which we then parse, process, and serve in the app. We're currently storing them in res/raw.

This makes things great for the designers because when they want to add content, they simply add a file to res/raw. This is annoying as a developer, however, since we developers then need to add R.raw.the_new_file to an array in the code specifying which files to process at startup.

Is there a way to access the resource ID's of res/raw programatically? Ideally when the application starts, we could make a call to see which files are in res/raw, process all of them, so we could eliminate the small overhead with matching up the contents of res/raw with that of our array in the code.

The most promising path I've seen is getAssets from Resources, which would let me call list(String) on the AssetManager, but I've had trouble getting this to work, especially since you can't directly call "res/raw" as your filepath (or at least, it hasn't worked when I've tried.

Suggestions? Thanks ^_^

A: 

You could use

Context.getResources().getIdentifier("filename", "raw", "packagename");

to get the appropriate identifier. however i don't understand why you implement your solution that complicated? what are the advantages for the designer? why they don't just put the files in drawable folder?

Roflcoptr
This is actually not for graphics, but for levels in a game. Each level has a static map, a variable number of dynamic objects, locations for those objects, level-specific notifications, a number of configurations, etc. Rather than have the level designers edit our codebase directly, we wrote a parser to allow them to specify the levels declaratively in text files; and we chose to put them in res/raw.Thanks for the tip! I'll give a try and let you know how it goes ^_^
paul.meier
+2  A: 

You could use reflection.


ArrayList<Integer> list = new ArrayList<Integer>();
Field[] fields = R.raw.class.getFields();
for(Field f : fields)
try {
        list.add(f.getInt(null));
    } catch (IllegalArgumentException e) {
    } catch (IllegalAccessException e) { }

After that list holds all resource IDs in the res/raw folder. If you need the name of the resource... f.getName() returns it.

Padde
Thanks, late to accept but I'd forgotten that I hadn't. Worked like a charm!
paul.meier