What’s the difference between Assets, res and raw directories on Android?

Asked

Viewed 4,548 times

5

I would like to know the difference between directories /assets and /res present on android, I know that the layout and various images are in the directory /res and are accessed in different ways.

There is also the directory /raw that’s inside the /res, I know that it can contain files and that can be accessed as for example .getIdentifier("arquivo","raw", getPackageName()); where the arquivo is equivalent to res/raw/arquivo.json, i.e., the raw may contain the same files as the assets?

What is the real difference between raw, the assets and the res on Android, and how I could use each of them?

2 answers

6


Before answering the difference, a brief explanation about the Resources:

Some items must be externalized (images, strings) to keep them independent and to support specific settings (device language, screen size, orientation, etc.). The briefcase res, uses several subfolders that join the Resources by type and configuration.

Mark Vasconcelos, in his article on Android - Application structure and organization, made a "summary" on the structuring of the application:

Folders created automatically in the project that will be used by application are:

  • src: That’s where our application’s source code is.

  • res: Folder of Resources application, here will be layout files, images, XML configuration, XML with strings internationalizable, raw files, sounds, etc..

The folders created by default for Resources sane:

  • res/drawable-<screen cfg>: The images should be placed, the division between hdpi (High dpi), mdpi (Medium dpi), ldpi (Low dpi) is to save the images in different resolutions.
  • res/layout: There are Xmls files that represent the layout of our Activities.
  • res/values: Xmls that store strings that can be used in the application.

For default, an archive strings.xml is created in that folder, the values are saved through tags with the following structure <string name="nomeString">Valor String</string>

In the folder res it is possible to add more subfolders to represent more Resources for application or alternatives to Resources, such as a folder xml to save settings information.

  • gen: source folder generated

This folder also stores source code, but it only keeps a single source automatically generated class of the eclipse plugin named "R" in same package we reported in "package name" when we created our project. This class R contains ids for all items we have on briefcase res, and it is through these ids that we can use all the Resources easily in our application.

  • assets: raw files - The difference of putting files raw here than in something like res/raw is that Resources sane accessible by id through class R, files in that folder can be opened by stream within the application as a byte array.

You can still add more folders to the project to create native applications is created a folder named jni who guards the native codes and AndroidMakeFiles than Android NDK (Native Development Kit) uses to create Shared lib for application, such as Android is a Unix distro, Shared lib is a file with extension .only, the files generated by NDK still goes to another folder called libs, this folder contains the Shared libs that can be used in application in Runtime, which can be loaded with a System.loadLibrary(“nomeLib”).

That’s worth reading about reply stackoverflow.

What is the real difference between raw, the assets and the res on Android, and how I could use each of them?

  • res: You should always use the files and folders of Resource to store your application’s values in addition to the images, of course. This way you can maintain and update your code much more easily, and you can define alternatives for each of them, according to specific situations, such as different languages, sizes and screen orientations. The briefcase res/ is the folder of your project that stores all the Resources of its application

  • raw: Save active files instead of saving to assets/directory. The difference is in the way you will access them. These files are processed by aapt and must be referenced from the application that uses a resource identifier in the class R. For example, this is a good place for media like MP3 or Ogg.

  • assets: It is more like a file system and offers more freedom to put any file. Then you can access each of the files on that system. This directory is good for things like game details, dictionaries, ... etc.

Reference:

2

  • Assets/

You can use this folder to store any file. The files you save here are compiled in the .apk and the name of the original file is preserved. You can browse this directory in the same way as a typical file system using Uris and read files as a byte stream using a Assetmanager. It is worth remembering that the class R does not generate IDs for the files placed in that folder, so it is less compatible with some Android classes and methods. Also, it is much slower to access a file stored in this folder since you will need to get an identifier for it based on a String.

Example of use: Storage of game textures and data, and databases sqlite .

  • res/

Contains all resources that are not codes (e. g. .java, . kt), such as XML layouts, UI strings and bitmap images. This folder is divided into subdirectories. Basically, this folder only serves as a grouping of other folders with other resources such as folders nursery, color, drawable, etc. The files contained in the subfolders of this folder are processed by aapt (Tondroid Tosset Packaging Tool), so the files of these sublists should be referenced in the application using a R class ID.

  • raw/

Just like the briefcase Assets/, this folder serves to store any file. The difference is that as this folder is a subfolder of the folder res/, the files contained therein are processed by aapt (Tondroid Tosset Packaging Tool), so they must be referenced in the application using a R class ID.

Example of use: Store media such as MP3 or Ogg files.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.