J
J
justyork2014-05-27 00:59:32
Android
justyork, 2014-05-27 00:59:32

How to get image resource ID by name?

Hello everyone, the task is this: to replace the picture in the Activity, having only the file name from the database.
I can't figure out how to get the resource id from the file name. I found a lot of options, but they don’t work, or I’m doing something wrong.
There was such an option

int imgRes = R.drawable.class.getField(itemImage).getInt(getResources());

itemImage is a string variable with the file name.
Maybe I'm not digging there at all and there is an easier option?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bimeg, 2014-05-27
@justyork

More or less like this. Maybe I messed up the settings.

int id = context.getResources().getIdentifier(itemImage, "drawable", context.getPackageName());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question