how to get images dynamically from drawable folder

84,619

Solution 1

You can use getIdentifier()

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

Solution 2

You can also use this:

int res = getResources().getIdentifier("<your pakecgename>:drawable/abc", null, null);

Solution 3

Something like this could work

Field[] drawables = android.R.drawable.class.getFields();
for (Field f : drawables) {
    try {
        System.out.println("R.drawable." + f.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Solution 4

Use the following line for getting drawable dynamically:

Drawable drawable = this.getResources().getDrawable(R.drawable.yourDrawableID);

This will give you the desired Drawable.

Solution 5

public static Drawable getImage(Context context, String name) {
        return context.getResources().getDrawable(context.getResources().getIdentifier(name, "drawable", context.getPackageName()));
}
Share:
84,619
Goofy
Author by

Goofy

Begineer as Andriod Developer

Updated on July 11, 2021

Comments

  • Goofy
    Goofy almost 3 years

    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.

  • Goofy
    Goofy about 12 years
    its showing me error dude "R.drawable.d002_p00" +i; cannot convert from int to string
  • maysi
    maysi over 10 years
    this answer is really bad... have you learned java?? A String can not be assigned to a int variable... and a int cannot be added to an string without using String.valueof()...
  • uniruddh
    uniruddh about 10 years
    yourDrawableID is actual ID value that you have specified for element in your layout(xml) file.
  • user4500
    user4500 almost 10 years
    This matched for me to get all drawables. I put all in a Hashmap for easier access later: KEY=f.getInt(f.getName()) + VALUE=f.getName()
  • Srujan Barai
    Srujan Barai almost 9 years
    How to save from that drawable to a String array?
  • The Student
    The Student over 8 years
    Requires API level 21 for getDrawable
  • SleepProgger
    SleepProgger over 7 years
    @TomBrito Use ContextCompat.getDrawable(context, res). See stackoverflow.com/a/34132342/4830897
  • Pj Rigor
    Pj Rigor over 7 years
    How would you do this if you are looking for any file with the extension ".jpg" and for an unknown number of files? @LalitPoptani
  • OneCricketeer
    OneCricketeer almost 7 years
    Using "drawable", getPackageName() instead of null, null is less error prone
  • Ramani Hitesh
    Ramani Hitesh over 6 years
    Mr.dilipkaklotar some code wrong so change our here and update your post
  • Admin
    Admin about 6 years
    Here i have created array dices for images in drawable folder.And i have accessed images randomly by the generation of random variable titled as randnum( from the code).Here i am keep on changing already existing image(id is imageView2 from the code) for each random number.Whenever we tap a buttom images will be randomly generated
  • Prvt_Yadav
    Prvt_Yadav almost 4 years
    Just a tip use your class name at the place of android. I wasted hours.