Dynamically setting an image name

All your problems with Audio, Video and Images.

Dynamically setting an image name

Postby wrothgar » Wed Mar 31, 2010 4:40 pm

I am playing around with an application and I would like to have it display 1 of 12 possible images. All are named image1 through image12. I would like to have a random number generate a value between 1 and 12, build the image name, and then set a bitmap variable based on the dynamic name. But I am not sure how to get a bitmap based on the generated string??? Can anyone help?

Thanks!
wrothgar
Freshman
Freshman
 
Posts: 7
Joined: Tue Nov 17, 2009 10:12 pm

Top

Postby vik » Thu Apr 01, 2010 2:04 pm

i think you are looking for this
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  getResources().getIdentifier (String name, String defType, String defPackage)
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
User avatar
vik
Senior Developer
Senior Developer
 
Posts: 141
Joined: Wed Sep 09, 2009 7:32 am

Postby wrothgar » Thu Apr 01, 2010 3:27 pm

That is exactly what I needed, thank you very much!!

Here is what the code ended up looking like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. x=(int) (Math.random() * 3);
  3.  
  4. s="image"+x;
  5.  
  6. int resID = getResources().getIdentifier(s, "drawable", "com.android.test");
  7.  
  8. mBitmap=getImage(context, resID);
  9.  
  10.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Love these forums and how helpful everyone is!
wrothgar
Freshman
Freshman
 
Posts: 7
Joined: Tue Nov 17, 2009 10:12 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 13 guests