Android apps can store resources in two directories: res and assets. Where the resources in the Res directory can only be stored in the corresponding
Subdirectories (such as Res/layout, Res/values, and so on), you cannot store resources directly in the RES directory. In the Assets directory
Resources can be stored arbitrarily, or any level of subdirectories can be established. The main differences between RES and assets resources are as follows.
1. Different ways of referencing resources
All resources in the RES directory will generate the corresponding int type variable in the corresponding subclass of the R class, and these variables will need to be used
To reference the resource. For example, Res/layout/main.xml is a layout file resource that generates a name named Main in the R.layout class.
Variable, so you need to use R.layout.main to refer to Main.xml.
Resources in the assets directory need to be referenced directly using the file name, such as Assets/xyz.xml, Assets/abc/test.db is
The two resource files in the assets directory, where ABC is a subdirectory in assets. The two resource files can be referenced using the following code,
and returns the InputStream object corresponding to the resource file, and the remaining operation is the normal Java code input stream operation.
InputStream is1=getresources (). Getassets (). Open ("Xyz.xml");
InputStream is2=getresources (). Getassets (). Open ("abc/test.db");
2. Different Treatment methods
The resource subdirectories in the Res directory are compiled in addition to RAW, which is why the resource files in the other resource directory are
The reason why you cannot directly view the contents of an XML-formatted resource file after extracting the APK file. and the resources in the assets and Res/raw directories
The file does not do any processing, so the resource files in both directories remain intact after the APK is decompressed.
3. Different sub-directories
The Res directory can have only one level of subdirectories, and these subdirectories must be predefined, such as Res/layout, Res/values, and so on.
Legal, and RES/ABC,RES/XYZ is not a legitimate resource directory, in the assets directory can be built at any level of subdirectories (only limited by the operating system)
The difference between Android Res and assets resources