When should I use the assets as opposed to raw resources in Android?

Mortalus picture Mortalus · Mar 5, 2012 · Viewed 48.4k times · Source

I'm in the mid of my Android studies, and I just covered the Assets and Raw resources. I'm trying to understand the reason for using Raw resources vs. Assets.

  1. They both provide with an uncompiled resource input stream.

  2. It seems that Assets provide much more flexibility and functionality than Raw resources.

    a. You can create folder structures under assets but not under raw

    b. You can list all resources dynamically in the assets folder but not in the raw folder.

So, why would I use Raw resources in Android?

Answer

user370305 picture user370305 · Mar 5, 2012

The main differences between the raw folder and the assets folder.

  • Since raw is a subfolder of Resources (res), Android will automatically generate an ID for any file located inside it. This ID is then stored in the R class that will act as a reference to a file, meaning it can be easily accessed from other Android classes and methods and even in Android XML files. Using the automatically generated ID is the fastest way to have access to a file in Android.

  • The assets folder is an “appendix” directory. The R class does not generate IDs for the files placed there, which is less compatible with some Android classes and methods. File access in the assets folder is slower since you will need to get a handle to it based on a String. However some operations are more easily done by placing files in this folder, like copying a database file to the system’s memory. There’s no (easy) way to create an Android XML reference to files inside the Assets folder.