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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With