166

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?

magnussn
  • 19
  • 5
Mortalus
  • 10,574
  • 11
  • 67
  • 117
  • Possible duplicate of [Difference between /res and /assets directories](http://stackoverflow.com/questions/5583608/difference-between-res-and-assets-directories) – rds Apr 07 '16 at 11:26

4 Answers4

198

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.

magnussn
  • 19
  • 5
user370305
  • 108,599
  • 23
  • 164
  • 151
  • And still i could use the Asset folder. i understand it will be "Harder" to access it from Android Classes and not possible to reference to it from Android XML but still, it's confusing, what are the use cases for each of theme ? – Mortalus Mar 05 '12 at 08:18
  • 1
    Use case, Suppose you have to use preloaded database file with your app then /asset is better, and you have to use preloaded xml file which you can parse in your application through code then using /raw you can directly access it. – user370305 Mar 05 '12 at 08:21
  • so to the bottomline, which files do you put in raw, and which do you put in assets? – josephus Mar 05 '12 at 08:23
  • 1
    If you want to access some files in other android class like view, then for easily and fast retrieving put those file (.xml,.wav,.mp3,.3gp,etc..) in /raw directory, if you want to use only onetime or (may be 2) then use /asset for it(like database file, video files for copying to other location,etc..). – user370305 Mar 05 '12 at 08:34
  • 4
    @user370305 Hi, is the 1MB limitation still there for new Android OS now? I can't find any documentation that talk about this. Do you have any idea where can I find it? – Sam YC Nov 22 '12 at 03:35
  • 6
    @GMsoF - There is no any specific document for this. But I have some useful links for you. 1. http://blog.rossgreenhalf.com/2010/12/13/android-file-size-limits/ 2. http://stackoverflow.com/questions/11276112/is-it-possible-to-add-specific-files-uncompressed-to-a-android-apk-using-build-x 3. http://stackoverflow.com/questions/6809651/documentation-for-aapt-element-in-ant-script 4. http://elinux.org/Android_aapt and 5. http://android-developers.blogspot.in/2012/03/android-apps-break-50mb-barrier.html Just go through as per sr no. – user370305 Nov 22 '12 at 06:10
  • 2
    @GMsoF - There are many tricks to put the file size larger than 1 mb in `/asset` directory. – user370305 Nov 22 '12 at 06:13
  • @user370305 Thank, is the res/raw folder limited as 1mb also? – Sam YC Nov 22 '12 at 06:22
  • 1
    @GMsoF - I am accessing 5mb size png file from `/res/drawable-xhdpi` directory. – user370305 Nov 22 '12 at 06:33
  • 10
    i never had a restriction of file size for the assets folder . are you sure there is such a thing? – android developer Feb 02 '13 at 15:21
  • Me neither, the file size restriction has probably been removed in one of the Android version. I had no restriction on 2.2 and later, I don't know for older versions. – L. G. Mar 27 '13 at 09:31
  • But is there any difference when accessing a large content in these folders. Is one consuming more memory than the other ? – Snicolas May 29 '13 at 05:32
  • 2
    Yes, prior to Android 2.2 or 2.3, any compressed asset file with an uncompressed size of over 1 MB cannot be read from the APK. Which is mentioned here https://groups.google.com/forum/#!topic/android-developers/lguGFJD-JRs – user370305 May 27 '14 at 17:49
42

From the Android documentation, the raw/ directory is used for:

Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.

However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.


In one line, the files in the raw/ directory are not compiled by the platform, are assigned a resource ID and cannot be grouped into sub-folders whereas if you want the otherwise use the assets/ directory.

Community
  • 1
  • 1
Prince
  • 20,353
  • 6
  • 39
  • 59
  • 1
    Are raw files the best option if you want to copy a large file to storage folder? – live-love Aug 06 '15 at 20:44
  • @40-Love There's no restriction on file size for Android 2.3 and above [unless you're supporting earlier versions.](http://ponystyle.com/blog/2010/03/26/dealing-with-asset-compression-in-android-apps/) You may choose any storage folder depending on your requirement. A major distinction is the way these files are accessed. – Prince Aug 10 '15 at 07:56
3

Adding to the answers given above...

/res/strings,/res/layout,/res/xml files etc all gets compiled into binary format. But if you place files, including XML files, in the /res/raw/ directory instead, they don’t get compiled into binary format.

One big advantage of using assets over raw resources is the file:///android_asset/ Uri prefix.This is useful for loading an asset into a WebView. For example, for accessing an asset located in assets/foo/index.html within your project, you can call loadUrl("file:///android_asset/foo/index.html") loading that HTML into the WebView.

Shubhamhackz
  • 7,333
  • 7
  • 50
  • 71
0

you use assest file when you need access to the original file names and file hierarchy,like using audio ,videos, text files directly.