How to dynamically generate the raw resource identifier in android?

Use Resources.getIdentifier() method:

int resId = getResources().getIdentifier("raw/book_name", null, this.getPackageName());

If your code not in activity or application, you need to get Context first.

Context context = getContext(); // or getBaseContext(), or getApplicationContext()
int resId = context.getResources().getIdentifier("raw/book_name", null, context.getPackageName());

Leave a Comment