How to access resources in Android projects

Source: Internet
Author: User

AndroidResource File Classification:

Android resource files can be broadly divided into two types:

The first is a compiled resource file that is stored in the Res directory:

This resource file system will automatically generate the ID of the resource file in R.java, so access to this resource file is relatively simple, through the r.xxx.id can be;

The second is a native resource file stored under the assets directory:

Because the system does not compile the resource files under assets, we cannot access them in r.xxx.id way. Can I access them through the absolute path of the resource? Because APK installation will be placed in the/data/app/**.apk directory, in the form of APK, asset/res and is bound to the APK, and will not be extracted into the/data/data/yourapp directory down, So we can't get the absolute path to the assets directly, because they don't have it at all.

Fortunately the Android system provides us with a Assetmanager tool class.

Viewing the official API, Assetmanager provides access to the application's original resource file; This class provides a low-level API that allows you to open and read the raw resource files that are bound together with the application as a simple byte stream.

Assetmanager class Overview:

Provides access to the application's original resource file; This class provides a low-level API that allows you to open and read the raw resource files that are bound together with the application in the form of simple byte streams. Gets the Assetmanager object through the Getassets () method.

Common methods of the Assetmanager class:

Public Methods

Final string[]

List (String path)

Returns all files and directory names under the specified path.

Final InputStream

Open (String fileName)

Use access_streaming mode to open the specified file under assets.

Final InputStream

Open (String fileName, int accessmode)

Opens the specified file under assets using the access mode shown.

How to access resources in Android projects

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.