views:

97

answers:

4

In my Android project, I want to loop through the entire collection of Drawable resources. Normally, you can only retrieve a specific resource via its ID using something like:

InputStream is = Resources.getSystem().openRawResource(resourceId)

However, I want to get all Drawable resources where I won't know their ID's beforehand. Is there a collection I can loop through or perhaps a way to get the list of resource ID's given the resources in my project?

Or, is there a way for me in Java to extract all property values from the R.drawable static class?

A: 

Okay, this feels a bit hack-ish, but this is what I came up with via Reflection. (Note that resources is an instance of class android.content.res.Resources.)

final R.drawable drawableResources = new R.drawable();
final Class<R.drawable> c = R.drawable.class;
final Field[] fields = c.getDeclaredFields();

for (int i = 0, max = fields.length; i < max; i++) {
    final int resourceId;
    try {
        resourceId = fields[i].getInt(drawableResources);
    } catch (Exception e) {
        continue;
    }
    /* make use of resourceId for accessing Drawables here */
}

If anyone has a better solution that makes better use of Android calls I might not be aware of, I'd definitely like to see them!

Matt Huggins
A: 

I guess the reflection code will work but I don't understand why you need this.

Resources in Android are static once the application is installed so you can have a list of resources or an array. Something like:

<string-array name="drawables_list">
    <item>drawable1</item>
    <item>drawable2</item>
    <item>drawable3</item>
</string-array>

And from your Activity you can get it by doing:

getResources().getStringArray(R.array.drawables_list);
Macarse
This works, but the problem with it is that every time I add a drawable to the folder, I also have to update this string array. I'm looking for something more automatic.
Matt Huggins
+1  A: 

If you find yourself wanting to do this you're probably misusing the resource system. Take a look at assets and AssetManager if you want to iterate over files included in your .apk.

adamp
Thanks, I'll check that out. I'm attempting to load textures into memory for use in an OpenGL game.
Matt Huggins
This works and is less project-specific (due to class `R` being project-specific), which I like. The only difference was that I has to use the "assets" folder instead of the "res" folder.
Matt Huggins
A: 

i used getResources().getIdentifier to scan through sequentially named images in my resource folders. to be on a safe side, I decided to cache image ids when activity is created first time:

    private void getImagesIdentifiers() {

    int resID=0;        
    int imgnum=1;
    images = new ArrayList<Integer>();

    do {            
        resID=getResources().getIdentifier("img_"+imgnum, "drawable", "com.mobilebabytoys.babyfaces");
        if (resID!=0)
            images.add(resID);
        imgnum++;
    }
    while (resID!=0);

    imageMaxNumber=images.size();
}
mishkin