• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to retrive all elements from Res/Raw folder in one shot at run time

hi all,
I have some zip files in res/raw folder ,normally i can get them by using

like getResources().openRawResources(R.raw.file name );
by mentioning file name at compile time(static loading) .so that i can un zip them .and now i have so many zip files there in raw folder ,so programatically i need to change the filename according to the requirement .
so if anybody knows this pls give me idea.

thanks and Regards
Murali dhuli
Android developer.
 
Hi Murali,

I can think of one way of doing this. I'm not saying it's the best, but it works.

The following code uses Java reflection to get the members of the R.raw class, retrieves their values, and returns the resource IDs in an array.

Code:
    private int[] getAllRawResources() {
      int[] ids = null ;
      R.raw r = new R.raw() ;
    	
      Field fields[] = R.raw.class.getDeclaredFields() ;
      ids = new int[fields.length] ;
    	
      try {
	for( int i=0; i<fields.length; i++ ) {
	  Field f = fields[i] ;
	  ids[i] = f.getInt(r) ;
	  Log.i(TAG, "R.raw."+f.getName()+" = 0x"+Integer.toHexString(ids[i])) ;
        }
      } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
	e.printStackTrace();
      } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      return ids ;
    }

My R class looks like this:

Code:
public final class R {
. . .
    public static final class raw {
        public static final int levels=0x7f040000;
        public static final int tunnels=0x7f040001;
    }
   . . .
}

And the code generates the following output in the log:

Code:
01-11 21:39:41.719: INFO/TempestActivity(759): R.raw.levels = 0x7f040000
01-11 21:39:55.027: INFO/TempestActivity(759): R.raw.tunnels = 0x7f040001

I hope that helps.
If anyone has a better way, I'd be curious to hear about it. :-)

Regards,

Mark
 
Back
Top Bottom