Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What is the use of assets folder in Android?

Assets provide a way to include arbitrary files like text, xml, fonts, music, and video in your application. If you try to include these files as "resources", Android will process them into its resource system and you will not be able to get the raw data.

What is the difference between RES directory and assets directory?

res and assets directory both can keep assets like images. In res/drawable, we can keep the images. 2. In assets, we can create our own folder, like image, video, textFile etc and can keep more than one assets accordingly.

What is raw resource in Android?

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.

Where is assets folder in Android Studio?

Select app/main folder, Right click and select New => Folder => Asset Folder. It will create 'assets' directory in main.


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.


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.


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.