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()));
}
Comments
-
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 about 12 yearsits showing me error dude "R.drawable.d002_p00" +i; cannot convert from int to string
-
maysi over 10 yearsthis 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 usingString.valueof()
... -
uniruddh about 10 years
yourDrawableID
is actualID
value that you have specified for element in your layout(xml) file. -
user4500 almost 10 yearsThis 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 almost 9 yearsHow to save from that
drawable
to a String array? -
The Student over 8 yearsRequires API level 21 for getDrawable
-
SleepProgger over 7 years@TomBrito Use ContextCompat.getDrawable(context, res). See stackoverflow.com/a/34132342/4830897
-
Pj Rigor over 7 yearsHow would you do this if you are looking for any file with the extension ".jpg" and for an unknown number of files? @LalitPoptani
-
OneCricketeer almost 7 yearsUsing
"drawable", getPackageName()
instead ofnull, null
is less error prone -
Ramani Hitesh over 6 yearsMr.dilipkaklotar some code wrong so change our here and update your post
-
Admin about 6 yearsHere 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 almost 4 yearsJust a tip use your class name at the place of android. I wasted hours.