-3

I want to declare a new Bitmap but i have some names for the image, R.raw.XXXXXXX, and i dont want to create a terrible switch with all the posibilities... any idea?

for example: Bitmap bitAnimal = BitmapFactory.decodeResource(getResources(), myString);

thanks!

1 Answers1

0

You can do something like this i suppose:

Bitmap bm = BitmapFactory.decodeResource(getResources(), getId(myString, myClass);

public static int getId(String resourceName, Class<?> c) {
    try {
        Field idField = c.getDeclaredField(resourceName);
        return idField.getInt(idField);
    } catch (Exception e) {
        throw new RuntimeException("No resource ID found for: "
                + resourceName + " / " + c, e);
    }
}
Maantje
  • 1,781
  • 2
  • 20
  • 33