1. Background Introduction
In application development, various resource files will be used, such as strings, pictures, audio and video media, color values, themes, etc. Strings sometimes involve internationalization aspects, while color pictures may be related to theme switching.
Those who have done Android development know that there are three kinds of resources in Android. One is the resources that participate in the compilation and are finally accessed through the R file. They are placed under the "res" directory and are used to store the resources of the application program, including icons, GUI layouts, etc. These resources will be directly processed by aapt (Android Asset Packaging Tool) during packaging and mapped to the R file of the Android project to generate the IDs corresponding to the R.resource files. Usually, they can be accessed through resource IDs. Another one is "raw", which is under "res/raw" and is used to store original and unprocessed resource files, such as audio files, video files, and plain text files. These files will not be processed into other formats during compilation but will be retained as they are. The last one is "assets", which is used to store static files that need to be packaged into the application program for deployment to the device. The files in the "assets" folder will not be compiled into binary files but will be copied to the device as they are.
HarmonyOS Next also provides similar resource directories, which store resources such as colors, fonts, spacings, and icons. In different devices or configurations, the values of these resources may be different. The following first introduces the resource directories of HarmonyOS Next.
2. Introduction to Resource Directories
The directories of HarmonyOS Next are divided into two types:
- Resource directories: including the "base" directory, qualifier directories, "rawfile" directory, and "resfile" directory.
- Resource group directories: including "element", "media", and "profile". The resource structure in the project is shown in the following figure:
The functions of each directory are introduced as follows:
- "base" directory: The "base" directory is a default existing directory. Its secondary sub-directory "element" is used to store basic elements such as strings, colors, and Boolean values. "media" and "profile" store resource files such as media, animations, and layouts. The resource files in the directory will be compiled into binary files and assigned resource file IDs. They can be accessed by specifying the resource type (type) and resource name (name). It is similar to the "res" directory in Android.
- Qualifier directories: "en_US" and "zh_CN" are two default qualifier directories. The rest of the qualifier directories need to be created by developers according to development needs. The secondary sub-directories "element", "media", and "profile" are used to store basic elements such as strings, colors, and Boolean values, as well as resource files such as media, animations, and layouts. The resource files in the directory will be compiled into binary files and assigned resource file IDs. They can be accessed by specifying the resource type (type) and resource name (name). Qualifiers include the following types:
- Mobile Country Code and Mobile Network Code
- Language
- Script
- Country or Region
- Portrait or Landscape Screen
- Device Type
- Color Mode
- Screen Density
- "rawfile" directory: It supports the creation of multi-layer sub-directories. The names of sub-directories can be customized, and various resource files can be freely placed in the folder. The resource files in the directory will be directly packaged into the application without compilation and will not be assigned resource file IDs. They can be accessed by specifying the file path and file name.
- "resfile" directory: It supports the creation of multi-layer sub-directories. The names of sub-directories can be customized, and various resource files can be freely placed in the folder. The resource files in the directory will be directly packaged into the application without compilation and will not be assigned resource file IDs. After the application is installed, the "resfile" resources will be decompressed to the application sandbox path. After obtaining the "resfile" resource directory through the "resourceDir" property of the Context, the resources can be accessed through the file path.
3. Resource Access
Generally, within the har package, resources can be accessed through "$r" or "$rawfile":
- For resources of types such as "color", "float", "string", "plural", "media", "profile", they can be accessed in the form of "$r('app.type.name')". Here, "app" refers to the resources defined in the "resources" directory; "type" refers to the resource type or the storage location of the resource; "name" refers to the resource name, which is determined by the developer when defining the resource.
- For resources in the "rawfile" directory, they can be accessed in the form of "$rawfile('filename')". Here, "filename" refers to the relative path of the file under the "rawfile" directory. The file name needs to include the suffix, and the path should not start with "/".
- After obtaining the ResourceManager through the context of the application, different resource management interfaces can be called to access different resources. For example, getContext().resourceManager.getStringByNameSync('test') can obtain string resources; getContext().resourceManager.getRawFd('rawfilepath') can obtain the descriptor information of the hap package where the Rawfile is located. When accessing rawfile files, {fd, offset, length} need to be used together.
There are two points to note here:
- For the situation where multiple placeholders are used in "string.json", it can be accessed in the form of $r('app.string.label','aaa','bbb',444).
- Accessing resources through resource IDs has better performance than directly accessing resources.
For accessing application resources across HAP/HSP packages, the context of different modules in the same application can be created through the createModuleContext(context, moduleName) interface. After obtaining the resourceManager object, different resource management interfaces can be called to access different resources. Resources can be accessed through "$r" or "$rawfile".
4. API Introduction
The resource access system provides the "@ohos.resourceManager" interface. First, obtain the ResourceManager through resourceManager.getResourceManager. Then, interfaces such as getStringSync and getMediaContent can be used. The provided interfaces are shown in the following figures:
5. Reference Materials
6. Summary
HarmonyOS Next provides resource directories similar to those in Android, including the "base", qualifier, and "rawfile" directories. The "base" directory contains compiled binary files, while the qualifier directories store corresponding resources according to different languages or device types. The "rawfile" directory directly packages original files without compilation. Resources can be accessed through "$r" or "$rawfile", or obtained through the ResourceManager interface.
Top comments (0)