The difference between Android Res and assets resources

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.