how to get images dynamically from drawable folder

Goofy picture Goofy · Feb 6, 2012 · Viewed 80k times · Source

I have an array like this.

int image[] = {R.drawable.d002_p001,R.drawable.d002_p002,R.drawable.d002_p003,
                   R.drawable.d002_p004,R.drawable.d002_p005,R.drawable.d002_p006};

Right now I have 6 images so I am statically given the name.

If I have some 50 images I cant give each and every file name in array so it needs to be dynamic how can I achieve this.

Answer

Lalit Poptani picture Lalit Poptani · Feb 6, 2012

You can use getIdentifier()

for (int j = 1; j < 6; j++) {
   Drawable drawable = getResources().getDrawable(getResources()
                  .getIdentifier("d002_p00"+j, "drawable", getPackageName()));
}