Skip to content Skip to sidebar Skip to footer

Dynamic Naming Of Resources In Android Without Getidentifier

I'm using OpenGL ES to make a game in Android. I got some code from a tutorial and I'm trying to change it to suit my app but I'm having a problem. I want to dynamically get an ima

Solution 1:

yes u can Suppose u have images stored in drawable with naming img1,img2,img3,img4,img5,img6,img7 than first make an array like

String[] imgarray={"img1","img2","img3","img4","img5","img6","img7"};
publicstaticString PACKAGE_NAME ;
PACKAGE_NAME=getApplicationContext().getPackageName();
Random r = new Random();
int n=r.nextInt(imgarray.length());
int resID = getResources().getIdentifier( PACKAGE_NAME+":drawable/" +imgarray[n] , null, null);  
imageview.setImageResource(resID);

if want bitmap image than just add below line

Bitmapbm= BitmapFactory.decodeResource(getResources(),resID);

if u want other way with less coding than see accepted answer at Other Example

Post a Comment for "Dynamic Naming Of Resources In Android Without Getidentifier"