Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 355
android developer Avatar asked Jan 10 '18 18:01

android developer


People also ask

What is the difference between RES directory and assets directory?

Use assets like a filesystem to dump any kind of files. And use res to store what it is made for, layouts, images, values.

What would be a benefit of using assets instead of resources in an app?

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.

What is a raw folder?

The raw (res/raw) folder is one of the most important folders and it plays a very important role during the development of android projects in android studio. The raw folder in Android is used to keep mp3, mp4, sfb files, etc. The raw folder is created inside the res folder: main/res/raw.

What should you store files in the assets directory?

In Android one can store the raw asset file like JSON, Text, mp3, HTML, pdf, etc in two possible locations: assets. res/raw folder.


1 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

like image 120
Jitesh Mohite Avatar answered Nov 04 '22 02:11

Jitesh Mohite