Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large amount of images to reference, don't want to hardcode support for each image

Tags:

android

I have about 650 small icons I need to reference in my android project. The total size of all of them is about 70 k/b's, so size isn't an issue.

The app I'm building simply lists all 650 of these icons along with a label for each. The labels are stored in a String array, and each of these icons has the labels index in it's file name for easy reference (so index 0 in the labels array co-ordinates with file p0.png in the drawable folder of my android project).

This works fine, but the problem is that I can reference these images without using the R.class. The R class sets up the variables and everything, but it's messy as hell to reference the the images through the R class when I can just loop through them and use their files names as indexes.

Is there a way to access these images without using the R.class? I'd also like to avoid using the SD card for this, as these images will be quite annoying to see in the SD card.

Suggestions?

like image 503
Vizirship Avatar asked Jan 19 '23 22:01

Vizirship


2 Answers

Put the icons into the /assets directory and use AssetManager to access them. Resources in the assets directory won't be indexed in the R file.

like image 135
shihpeng Avatar answered May 16 '23 04:05

shihpeng


The assets approach will certainly work but comes with the disadvantage that you cannot separate your images into separate folders. Using the provided function will enable you to iterate through your drawable list passing in your index as a string value.

public int getId(String name) {
        Class<R.drawable> class = R.drawable.class;
        Field field;
        int i = 0;
        try {
            field = class.getField(name);
            i = field.getInt(field);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  i;
    }

Edit

Apologies for answering accepted question, accepted as i was posting.

like image 43
Joe Avatar answered May 16 '23 04:05

Joe