Skip to content

Instantly share code, notes, and snippets.

@qhm123
Created December 3, 2012 03:48
Show Gist options
  • Save qhm123/4192553 to your computer and use it in GitHub Desktop.
Save qhm123/4192553 to your computer and use it in GitHub Desktop.
android get drawable int value by drawable name
public static int getImage(String pic) {
if (pic == null || pic.trim().equals("")) {
return R.drawable.icon;
}
Class draw = R.drawable.class;
try {
Field field = draw.getDeclaredField(pic);
return field.getInt(pic);
} catch (SecurityException e) {
return R.drawable.icon;
} catch (NoSuchFieldException e) {
return R.drawable.icon;
} catch (IllegalArgumentException e) {
return R.drawable.icon;
} catch (IllegalAccessException e) {
return R.drawable.icon;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment