Search code examples
androiddrawableandroid-resources

how to get images dynamically from drawable folder


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.


Solution

  • You can use getIdentifier()

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