0

I have an entire folder that I would like to be in the R.raw folder so that it looks like this:

raw/thor/myfirstfile.jpg   
raw/thor/mysecondfile.m4a
raw/thor/mythirdfile.json
raw/batman/myfirstfile.jpg
raw/batman/mysecondfile.m4a
raw/batman/mythirdfile.json

So that I could copy the folders to:

/externalSD/.../themes/thor/myfirstfile.jpg
/externalSD/.../themes/thor/mysecondfile.m4a
/externalSD/.../themes/thor/mythirdfile.json
/externalSD/.../themes/batman/myfirstfile.jpg
/externalSD/.../themes/batman/mysecondfile.m4a
/externalSD/.../themes/batman/mythirdfile.json

I cannot put the folder into R.assets because the project containing these assets is a library and I need to access the files from the application project.

It's also not an option to simple put all the files into the raw/assets/drawable/etc directory because the themes are essentially templates designed to search a directory on the SD card, the idea is that more themes can be downloaded.

At present my solution is packaging the folders into zip files as such:

raw/thor.zip
raw/batman.zip

Accessing them using:

InputStream fileStream = context.getResources().openRawResource(rawResource);

and then unzipping on the first run of the app onto the SD card - but it is painfully slow. It would (hopefully) be much quicker to simply copy a folder rather than unzip one.

My question is how can I copy an entire folder out of the R.raw resources?

shredder
  • 1,438
  • 13
  • 12
  • Tou cannot make subfolders in raw. Well you can create them in the ide but all files in it are unreachable by your app. If you want subfolders then use assets directory. – greenapps Oct 09 '14 at 12:09

1 Answers1

0

I have an entire folder that I would like to be in the R.raw folder so that it looks like this

That is not supported. You cannot have subdirectories in res/raw/. assets/ would work as an alternative to res/raw/.

I cannot put the folder into R.assets because the project containing these assets is a library and I need to access the files from the application project.

Android library projects support assets nowadays, at least on Android Studio/Gradle for Android. I haven't tried them on Eclipse recently.

My question is how can I copy an entire folder out of the R.raw resources?

Since you cannot put a whole folder into res/raw/, you cannot copy it out of there.

You are welcome to invent your own name-mangling algorithm (e.g., raw/theme___thor___myfirstfile.jpg), so long as you stick to Java data member naming rules. You can then convert those filenames into relative paths when writing them out. You would need to iterate over the static data members of R.raw using reflection to find out the names and their corresponding resource ID values.

But, I would suggest that you experiment with assets first, as it may work better than you expect. Or, if this library is purely for your own use (i.e., not for public distribution), perhaps switch to Android Studio and use product flavors instead of an application and a separate library.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • There is some useful info and links [here](http://stackoverflow.com/questions/6346889/how-to-reference-an-asset-in-a-library-project) but you're correct. It's time to move to Android Studio and Gradle, Flavors sounds exactly like what I need. – shredder Oct 09 '14 at 14:05
  • As an aside, I managed to link the assets folder and copy the assets instead of unzipping - turns out copying out of the apk seems to be the bottleneck - not unzipping. So even with the files unzipped in the linked assets folder, it still takes a long time to copy them onto the SD card or local file system. – shredder Oct 09 '14 at 14:09
  • @shredder: That's not terribly shocking. Disk writes on flash are generally slow, and many Android devices have... flash of dubious quality. :-) Note, though, that this is one area you really want to measure on hardware, as the emulator will tend to be faster at disk I/O than will production devices, as your development computer's hard drive or SSD is usually a lot faster than flash. – CommonsWare Oct 09 '14 at 16:10
  • Fair point, I tend to use Genymotion for development where I can - and it's very fast. So you're right that you need to use a real device. Also the apk itself is a zip file so taking files out of the apk is probably an unzip process anyway. Thanks for your help @CommonsWare. – shredder Oct 10 '14 at 11:02