List all drawable resources

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

List all drawable resources

Postby Hachaso » Sat Jan 02, 2010 7:22 pm

Hi!

I was wondering if it is possible to list with some Android API all the resources located in the res/drawable folder in the Android project.
If I have 10 PNG images in the drawable folder. Can I somehow list then, find them using some Android API?
I would like to have some getDrawables that saves all the resouces in an array.

Please help
Hachaso
Developer
Developer
 
Posts: 43
Joined: Mon Jan 26, 2009 11:44 am

Top

Postby jwei512 » Sat Jan 02, 2010 7:36 pm

What do you mean "list" them? In your project, you can always "find" your drawables by doing something like:

getResources().getDrawable(R.drawable.your_drawable);

In your activity...

This returns an instance of the drawable as a Drawable object which can be used in various places.

Not sure if this was what you're getting at... you might want to explain what you're trying to do and why you need an array of them
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby Hachaso » Sat Jan 02, 2010 9:04 pm

I would like to loop through all the resources in the drawable folder. Finding them one by one using some API.
I know that I could figure it out and hard code them when I create my project. But It would be better if I somehow could list them and loop through them one by one.

for instance If I have a photo album. I would like to find all the photos in the drawable folder located in the res folder. This is just an example. Something like that. Or maybe I have all my different icons there.. and I want to list them all.

Please help.
Hachaso
Developer
Developer
 
Posts: 43
Joined: Mon Jan 26, 2009 11:44 am

Postby sing1e » Sun Jan 03, 2010 2:52 am

I think you can use java reflection, and then get the drawable by the name.
first:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. static Class dClass;
  3.  
  4.         static{
  5.  
  6.                 try {
  7.  
  8.                         dClass = Class.forName("your.package.R$drawable");
  9.  
  10.                 } catch (ClassNotFoundException e) {
  11.  
  12.                         e.printStackTrace();
  13.  
  14.                 } catch (SecurityException e) {
  15.  
  16.                         e.printStackTrace();
  17.  
  18.                 } catch (IllegalArgumentException e) {
  19.  
  20.                         e.printStackTrace();
  21.  
  22.                 }
  23.  
  24.         }
  25.  
  26.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


and the you can put all the drawable into a hashmap:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. HashMap<String,Bitmap> allBitmaps = new HashMap<String,Bitmap>();
  3.  
  4. Resources res = context.getResources();
  5.  
  6.                 try {
  7.  
  8.                         Field[] fields = dClass.getDeclaredFields();
  9.  
  10.                         for (Field f : fields) {
  11.  
  12.                                 String fName = f.getName();
  13.  
  14.                                 Object fp = f.get(dClass);
  15.  
  16.                                 if (fp instanceof Integer) {
  17.  
  18.                                         allBitmaps.put(fName, ((BitmapDrawable)res.getDrawable(((Integer)fp).intValue())).getBitmap());
  19.  
  20.                                 }
  21.  
  22.                         }
  23.  
  24.                 } catch (SecurityException e) {
  25.  
  26.                         e.printStackTrace();
  27.  
  28.                 } catch (IllegalArgumentException e) {
  29.  
  30.                         e.printStackTrace();
  31.  
  32.                 } catch (IllegalAccessException e) {
  33.  
  34.                         e.printStackTrace();
  35.  
  36.                 }
  37.  
  38.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


I think use this method, you can loop through them one by one and by the name(String).
Maybe this will help you!
sing1e
Freshman
Freshman
 
Posts: 9
Joined: Sat Dec 12, 2009 10:55 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 2 guests