Home » Android » android – Are there any major differences or restrictions when using assets or res/raw folders?

android – Are there any major differences or restrictions when using assets or res/raw folders?

Posted by: admin May 14, 2020 Leave a comment

Questions:

Background

Some files of the app can only be stored in res/raw or assets folders.

Each of those folders work in a very similar way to the other. res/raw folder allows to access files easier, with all the other benefits of resource files, while assets folder allows to access them no matter the file name and structure (including folders and sub folders).

The main idea of loading files is about the same for both of them. You just have a choice of ease-of-use, depends on your needs.

The problem

I remember that a very long time ago, I’ve found some special behavior of both of those folders:

  1. Each folder within the assets folder had a max number of files. I think it was about 500, but not sure. I’ve noticed this behavior a very long time ago,

  2. Some said that files in the assets folder have a max size for files (example here). I never saw such a restriction. Not even on Android 2.3 at the time.

  3. Some said (example here), and it’s still believed even today (example here), that if you load a file from res/raw, it could take much more memory than if you took it from assets folder.

What I’ve tried

For #1, I never had to use more files anyway after the project I worked on, and at the time I worked on it, we simply split the files into more folders.

For #2 , as I wrote, I never noticed it anyway. I used much larger files sizes.

For #3, I tried to make a sample project that compares the memory usage between the 2 methods. I didn’t notice any difference (memory usage or time to load) between the 2 methods. Especially not a major one. Sadly I have only one device (Nexus 5x), and it has quite a new Android version (8.1). It might be that starting from specific Android version there is no difference between the 2 methods. Another reason for this is that it’s harder to measure memory usage on Java, because of the GC, and I’ve already noticed that on Android 8.x, memory works a bit differently than before (written about it here).

I tried to read about the differences and restrictions of the above, but all I’ve found are very old articles, so I think things might have changed ever since.

The questions

Actually it’s just one question, but I’d like to split it in case the answer is complex:

  1. Are there any major or unique limitations or differences between using res/raw and assets folders?

  2. Does reading a file from the assets folder (by creating an input stream from it) really take less memory than using the res/raw? So much that even one of the most appreciated developers (here) decides to choose it, even nowadays?

  3. Have the above restrictions existed up to specific Android versions, and then they became identical in terms of no restrictions whatsoever (except of course files naming for res/raw, but that’s just how it works) ?

  4. If so, from which Android version do they work about the same?

How to&Answers:

Are there any major or unique limitations or differences between using res/raw and assets folders?

Now, In android we don’t have any restriction on max limit size for any file in assets or in raw.

Android Documentation:

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/ aren’t given a resource ID, so
you can read them only using AssetManager.

Does reading a file from the assets folder (by creating an input stream from it) really take less memory than using the res/raw? So much that even one of the most appreciated developers (here) decides to choose it, even nowadays?

No, I have not found any differences between memory usage. It is one of biggest mess that android is having right now, Also we don’t have any official documentation about their memory limitation.

Have the above restrictions existed up to specific Android versions, and then they became identical in terms of no restrictions whatsoever (except of course files naming for res/raw, but that’s just how it works) ?

Before android 2.3 we had memory restriction for asset folder, which is 1 MB. Please refer link.

If so, from which Android version do they work about the same?

From android 2.3, We don’t have any memory related restriction, which they launched in December, 2010