Differences between raw and assets folders in the #Android

Source: Internet
Author: User

Here is what I get from my experience by tidying up the Internet.

The same point of raw and assets

They will be copied to the APK in their entirety, without being compiled into binary form like other resource files.

Different points of raw and assets
    1. They are in different ways in the code:

      Assets:
      InputStream assets = getAssets().open("xxxx");

      Raw:
      InputStream raw = getResources().openRawResource(R.raw.xxxx)

    2. Assets can create a directory structure, but also can dynamically list all the resources in the assets ( getAssets().list(String path); ), raw file directory does not

    3. The resource under the Raw folder generates a resource under the Id,assets file does not

Usage scenarios for raw and assets
    • Because Raw is a subdirectory of the resources (RES), Android automatically generates an ID for all resource files in this directory, which is stored in the R class as a reference to a file. This means that this resource file can be easily accessed by Android classes and methods, even in the form of an Android XML file you can @raw/ refer to it. In Android, using an ID is the quickest way to access a file. MP3 and Ogg files in this directory are more appropriate.

    • The assets directory is more like an appendix type directory, Android does not generate an ID for the files in this directory and is saved in the R class, so it is less compatible with some classes and methods in Android. At the same time, because you need a string path to get the file descriptor in this directory, the access speed will be slower. But putting some files in this directory can make things easier, such as copying a database file into system memory. Note that you cannot refer to files in the assets directory in the Android XML file, only through Assetmanager to access the files. Database files and game data are better placed in this directory.

Differences between raw and assets folders in the #Android

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.