Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 505
Goofy Avatar asked Feb 06 '12 07:02

Goofy


People also ask

How do I find my pictures from the drawable folder on Android?

Then you can see the drawable folder under app —> res folder in the panel below the subview. Copy images from any directory you saved and right-click the drawable folder, click the Paste menu item in the popup menu list then accept all popup dialog. After that, you can see the images added in the drawable folder.

How do I add an image to a Visual Studio drawable folder?

Go to the solution explorer, select Resource--> drawable. Right click --> add--> Existing Item. or (Shift + Alt + A).


2 Answers

You can use getIdentifier()

for (int j = 1; j < 6; j++) {    Drawable drawable = getResources().getDrawable(getResources()                   .getIdentifier("d002_p00"+j, "drawable", getPackageName())); } 
like image 108
Lalit Poptani Avatar answered Oct 19 '22 23:10

Lalit Poptani


You can also use this:

int res = getResources().getIdentifier("<your pakecgename>:drawable/abc", null, null); 
like image 38
Sunil_Suthar Avatar answered Oct 19 '22 22:10

Sunil_Suthar