# @ohos.resourceManager (Resource Manager) The Resource Manager module provides APIs to obtain information about application resources based on the current configuration, including the language, region, screen direction, MCC/MNC, as well as device capability and density. > **NOTE** > > The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. ## Modules to Import ```js import resourceManager from '@ohos.resourceManager'; ``` ## Instruction Since API version 9, the stage model allows an application to obtain a **ResourceManager** object based on **context** and call its resource management APIs without first importing the required bundle. This approach, however, is not applicable to the FA model. For the FA model, you need to import the required bundle and then call the [getResourceManager](#resourcemanagergetresourcemanager) API to obtain a **ResourceManager** object. For details about how to reference context in the stage model, see [Context in the Stage Model](../..//application-models/application-context-stage.md). ```ts import Ability from '@ohos.application.Ability'; class MainAbility extends Ability { onWindowStageCreate(windowStage) { let context = this.context; let resourceManager = context.resourceManager; } } ``` ## resourceManager.getResourceManager getResourceManager(callback: AsyncCallback<ResourceManager>): void Obtains the **ResourceManager** object of this application. This API uses an asynchronous callback to return the result. **Model restriction**: This API can be used only in the FA model. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | ----------------------------- | | callback | AsyncCallback<[ResourceManager](#resourcemanager)> | Yes | Callback used to return the result.| **Example** ```js resourceManager.getResourceManager((error, mgr) => { if (error != null) { console.log("error is " + error); return; } mgr.getString(0x1000000, (error, value) => { if (error != null) { console.log("error is " + error); } else { let str = value; } }); }); ``` > **NOTE**
In the sample code, **0x1000000** indicates the resource ID, which can be found in the compiled **ResourceTable.txt** file. ## resourceManager.getResourceManager getResourceManager(bundleName: string, callback: AsyncCallback<ResourceManager>): void Obtains the **ResourceManager** object of an application based on the specified bundle name. This API uses an asynchronous callback to return the result. **Model restriction**: This API can be used only in the FA model. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ---------- | ---------------------------------------- | ---- | ----------------------------- | | bundleName | string | Yes | Bundle name of the application. | | callback | AsyncCallback<[ResourceManager](#resourcemanager)> | Yes | Callback used to return the result.| **Example** ```js resourceManager.getResourceManager("com.example.myapplication", (error, mgr) => { }); ``` ## resourceManager.getResourceManager getResourceManager(): Promise<ResourceManager> Obtains the **ResourceManager** object of this application. This API uses a promise to return the result. **Model restriction**: This API can be used only in the FA model. **System capability**: SystemCapability.Global.ResourceManager **Return value** | Type | Description | | ---------------------------------------- | ----------------- | | Promise<[ResourceManager](#resourcemanager)> | Promise used to return the result.| **Example** ```js resourceManager.getResourceManager().then(mgr => { mgr.getString(0x1000000, (error, value) => { if (error != null) { console.log("error is " + error); } else { let str = value; } }); }).catch(error => { console.log("error is " + error); }); ``` > **NOTE**
In the sample code, **0x1000000** indicates the resource ID, which can be found in the compiled **ResourceTable.txt** file. ## resourceManager.getResourceManager getResourceManager(bundleName: string): Promise<ResourceManager> Obtains the **ResourceManager** object of an application based on the specified bundle name. This API uses a promise to return the result. **Model restriction**: This API can be used only in the FA model. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ---------- | ------ | ---- | ------------- | | bundleName | string | Yes | Bundle name of the application.| **Return value** | Type | Description | | ---------------------------------------- | ------------------ | | Promise<[ResourceManager](#resourcemanager)> | Promise used to return the result.| **Example** ```js resourceManager.getResourceManager("com.example.myapplication").then(mgr => { }).catch(error => { }); ``` ## Direction Enumerates the screen directions. **System capability**: SystemCapability.Global.ResourceManager | Name | Value | Description | | -------------------- | ---- | ---- | | DIRECTION_VERTICAL | 0 | Portrait. | | DIRECTION_HORIZONTAL | 1 | Landscape. | ## DeviceType Enumerates the device types. **System capability**: SystemCapability.Global.ResourceManager | Name | Value | Description | | -------------------- | ---- | ---- | | DEVICE_TYPE_PHONE | 0x00 | Phone | | DEVICE_TYPE_TABLET | 0x01 | Tablet | | DEVICE_TYPE_CAR | 0x02 | Head unit | | DEVICE_TYPE_PC | 0x03 | PC | | DEVICE_TYPE_TV | 0x04 | TV | | DEVICE_TYPE_WEARABLE | 0x06 | Wearable | ## ScreenDensity Enumerates the screen density types. **System capability**: SystemCapability.Global.ResourceManager | Name | Value | Description | | -------------- | ---- | ---------- | | SCREEN_SDPI | 120 | Screen density with small-scale dots per inch (SDPI). | | SCREEN_MDPI | 160 | Screen density with medium-scale dots per inch (MDPI). | | SCREEN_LDPI | 240 | Screen density with large-scale dots per inch (LDPI). | | SCREEN_XLDPI | 320 | Screen density with extra-large-scale dots per inch (XLDPI). | | SCREEN_XXLDPI | 480 | Screen density with extra-extra-large-scale dots per inch (XXLDPI). | | SCREEN_XXXLDPI | 640 | Screen density with extra-extra-extra-large-scale dots per inch (XXXLDPI).| ## Configuration Defines the device configuration. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Readable | Writable | Description | | --------- | ----------------------- | ---- | ---- | -------- | | direction | [Direction](#direction) | Yes | No | Screen direction of the device.| | locale | string | Yes | No | Current system language. | **Example** ```js resourceManager.getResourceManager((error, mgr) => { mgr.getConfiguration((error, value) => { let direction = value.direction; let locale = value.locale; }); }); ``` ## DeviceCapability Defines the device capability. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Readable | Writable | Description | | ------------- | ------------------------------- | ---- | ---- | -------- | | screenDensity | [ScreenDensity](#screendensity) | Yes | No | Screen density of the device.| | deviceType | [DeviceType](#devicetype) | Yes | No | Type of the device. | **Example** ```js resourceManager.getResourceManager((error, mgr) => { mgr.getDeviceCapability((error, value) => { let screenDensity = value.screenDensity; let deviceType = value.deviceType; }); }); ``` ## RawFileDescriptor8+ Defines the descriptor of the raw file. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Readable | Writable | Description | | ------ | ------ | ---- | ---- | ------------------ | | fd | number | Yes | No| Descriptor of the raw file.| | offset | number | Yes | No| Start offset of the raw file. | | length | number | Yes | No| Length of the raw file. | ## Resource9+ Defines the resource information of an application. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Readable | Writable |Description | | ---------- | ------ | ----- | ---- | ---------------| | bundleName | string | Yes | No| Bundle name of the application.| | moduleName | string | Yes | No| Module name of the application.| | id | number | Yes | No| Resource ID. | ## ResourceManager Defines the capability of accessing application resources. > **NOTE** > > - The APIs involved in **ResourceManager** are applicable only to the TypeScript-based declarative development paradigm. > > - Resource files are defined in the **resources** directory of the project. You can obtain the resource ID using **$r(resource address).id**, for example, **$r('app.string.test').id**. ### getStringValue9+ getStringValue(resId: number, callback: AsyncCallback<string>): void Obtains the string corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | --------------------------- | ---- | --------------- | | resId | number | Yes | Resource ID. | | callback | AsyncCallback<string> | Yes | Callback used to return the result.| **Error codes** For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the module resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example (stage)** ```ts try { this.context.resourceManager.getStringValue($r('app.string.test').id, (error, value) => { if (error != null) { console.log("error is " + error); } else { let str = value; } }); } catch (error) { console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getStringValue9+ getStringValue(resId: number): Promise<string> Obtains the string corresponding to the specified resource ID. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ----- | ------ | ---- | ----- | | resId | number | Yes | Resource ID.| **Return value** | Type | Description | | --------------------- | ----------- | | Promise<string> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getStringValue($r('app.string.test').id).then(value => { let str = value; }).catch(error => { console.log("getStringValue promise error is " + error); }); } catch (error) { console.error(`promise getStringValue failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getStringValue9+ getStringValue(resource: Resource, callback: AsyncCallback<string>): void Obtains the string corresponding to the specified resource object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | --------------------------- | ---- | --------------- | | resource | [Resource](#resource9) | Yes | Resource object. | | callback | AsyncCallback<string> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts let resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.string.test').id }; try { this.context.resourceManager.getStringValue(resource, (error, value) => { if (error != null) { console.log("error is " + error); } else { let str = value; } }); } catch (error) { console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getStringValue9+ getStringValue(resource: Resource): Promise<string> Obtains the string corresponding to the specified resource object. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | Yes | Resource object.| **Return value** | Type | Description | | --------------------- | ---------------- | | Promise<string> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts let resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.string.test').id }; try { this.context.resourceManager.getStringValue(resource).then(value => { let str = value; }).catch(error => { console.log("getStringValue promise error is " + error); }); } catch (error) { console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getStringArrayValue9+ getStringArrayValue(resId: number, callback: AsyncCallback<Array<string>>): void Obtains the string array corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | ----------------- | | resId | number | Yes | Resource ID. | | callback | AsyncCallback<Array<string>> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id, (error, value) => { if (error != null) { console.log("error is " + error); } else { let strArray = value; } }); } catch (error) { console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getStringArrayValue9+ getStringArrayValue(resId: number): Promise<Array<string>> Obtains the string array corresponding to the specified resource ID. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ----- | ------ | ---- | ----- | | resId | number | Yes | Resource ID.| **Return value** | Type | Description | | ---------------------------------- | ------------- | | Promise<Array<string>> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id).then(value => { let strArray = value; }).catch(error => { console.log("getStringArrayValue promise error is " + error); }); } catch (error) { console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getStringArrayValue9+ getStringArrayValue(resource: Resource, callback: AsyncCallback<Array<string>>): void Obtains the string array corresponding to the specified resource object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | ----------------- | | resource | [Resource](#resource9) | Yes | Resource object. | | callback | AsyncCallback<Array<string>> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts let resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.strarray.test').id }; try { this.context.resourceManager.getStringArrayValue(resource, (error, value) => { if (error != null) { console.log("error is " + error); } else { let strArray = value; } }); } catch (error) { console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getStringArrayValue9+ getStringArrayValue(resource: Resource): Promise<Array<string>> Obtains the string array corresponding to the specified resource object. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | Yes | Resource object.| **Return value** | Type | Description | | ---------------------------------- | ------------------ | | Promise<Array<string>> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts let resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.strarray.test').id }; try { this.context.resourceManager.getStringArrayValue(resource).then(value => { let strArray = value; }).catch(error => { console.log("getStringArray promise error is " + error); }); } catch (error) { console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getMediaContent9+ getMediaContent(resId: number, callback: AsyncCallback<Uint8Array>): void Obtains the content of the media file corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------------- | ---- | ------------------ | | resId | number | Yes | Resource ID. | | callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **Example** ```ts try { this.context.resourceManager.getMediaContent($r('app.media.test').id, (error, value) => { if (error != null) { console.log("error is " + error); } else { let media = value; } }); } catch (error) { console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getMediaContent9+ getMediaContent(resId: number): Promise<Uint8Array> Obtains the content of the media file corresponding to the specified resource ID. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ----- | ------ | ---- | ----- | | resId | number | Yes | Resource ID.| **Return value** | Type | Description | | ------------------------- | -------------- | | Promise<Uint8Array> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **Example** ```ts try { this.context.resourceManager.getMediaContent($r('app.media.test').id).then(value => { let media = value; }).catch(error => { console.log("getMediaContent promise error is " + error); }); } catch (error) { console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getMediaContent9+ getMediaContent(resource: Resource, callback: AsyncCallback<Uint8Array>): void Obtains the content of the media file corresponding to the specified resource object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------------- | ---- | ------------------ | | resource | [Resource](#resource9) | Yes | Resource object. | | callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **Example** ```ts let resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.media.test').id }; try { this.context.resourceManager.getMediaContent(resource, (error, value) => { if (error != null) { console.log("error is " + error); } else { let media = value; } }); } catch (error) { console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getMediaContent9+ getMediaContent(resource: Resource): Promise<Uint8Array> Obtains the content of the media file corresponding to the specified resource object. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | Yes | Resource object.| **Return value** | Type | Description | | ------------------------- | ------------------- | | Promise<Uint8Array> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **Example** ```ts let resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.media.test').id }; try { this.context.resourceManager.getMediaContent(resource).then(value => { let media = value; }).catch(error => { console.log("getMediaContent promise error is " + error); }); } catch (error) { console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getMediaContentBase649+ getMediaContentBase64(resId: number, callback: AsyncCallback<string>): void Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | --------------------------- | ---- | ------------------------ | | resId | number | Yes | Resource ID. | | callback | AsyncCallback<string> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **Example** ```ts try { this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, (error, value) => { if (error != null) { console.log("error is " + error); } else { let media = value; } }); } catch (error) { console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getMediaContentBase649+ getMediaContentBase64(resId: number): Promise<string> Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ----- | ------ | ---- | ----- | | resId | number | Yes | Resource ID.| **Return value** | Type | Description | | --------------------- | -------------------- | | Promise<string> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **Example** ```ts try { this.context.resourceManager.getMediaContentBase64($r('app.media.test').id).then(value => { let media = value; }).catch(error => { console.log("getMediaContentBase64 promise error is " + error); }); } catch (error) { console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getMediaContentBase649+ getMediaContentBase64(resource: Resource, callback: AsyncCallback<string>): void Obtains the Base64 code of the image corresponding to the specified resource object. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | --------------------------- | ---- | ------------------------ | | resource | [Resource](#resource9) | Yes | Resource object. | | callback | AsyncCallback<string> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **Example** ```ts let resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.media.test').id }; try { this.context.resourceManager.getMediaContentBase64(resource, (error, value) => { if (error != null) { console.log("error is " + error); } else { let media = value; } }); } catch (error) { console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getMediaContentBase649+ getMediaContentBase64(resource: Resource): Promise<string> Obtains the Base64 code of the image corresponding to the specified resource object. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | Yes | Resource object.| **Return value** | Type | Description | | --------------------- | ------------------------- | | Promise<string> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **Example** ```ts let resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.media.test').id }; try { this.context.resourceManager.getMediaContentBase64(resource).then(value => { let media = value; }).catch(error => { console.log("getMediaContentBase64 promise error is " + error); }); } catch (error) { console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getConfiguration getConfiguration(callback: AsyncCallback<Configuration>): void Obtains the device configuration. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | ------------------------- | | callback | AsyncCallback<[Configuration](#configuration)> | Yes | Callback used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getConfiguration((error, value) => { if (error != null) { console.log("error is " + error); } else { let direction = value.direction; let locale = value.locale; } }); }); ``` ### getConfiguration getConfiguration(): Promise<Configuration> Obtains the device configuration. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Return value** | Type | Description | | ---------------------------------------- | ---------------- | | Promise<[Configuration](#configuration)> | Promise used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getConfiguration().then(value => { let direction = value.direction; let locale = value.locale; }).catch(error => { console.log("getConfiguration promise error is " + error); }); }); ``` ### getDeviceCapability getDeviceCapability(callback: AsyncCallback<DeviceCapability>): void Obtains the device capability. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | ---------------------------- | | callback | AsyncCallback<[DeviceCapability](#devicecapability)> | Yes | Callback used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getDeviceCapability((error, value) => { if (error != null) { console.log("error is " + error); } else { let screenDensity = value.screenDensity; let deviceType = value.deviceType; } }); }); ``` ### getDeviceCapability getDeviceCapability(): Promise<DeviceCapability> Obtains the device capability. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Return value** | Type | Description | | ---------------------------------------- | ------------------- | | Promise<[DeviceCapability](#devicecapability)> | Promise used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getDeviceCapability().then(value => { let screenDensity = value.screenDensity; let deviceType = value.deviceType; }).catch(error => { console.log("getDeviceCapability promise error is " + error); }); }); ``` ### getPluralStringValue9+ getPluralStringValue(resId: number, num: number, callback: AsyncCallback<string>): void Obtains the singular-plural string corresponding to the specified resource ID based on the specified number. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | --------------------------- | ---- | ------------------------------- | | resId | number | Yes | Resource ID. | | num | number | Yes | Number. | | callback | AsyncCallback<string> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1, (error, value) => { if (error != null) { console.log("error is " + error); } else { let str = value; } }); } catch (error) { console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getPluralStringValue9+ getPluralStringValue(resId: number, num: number): Promise<string> Obtains the singular-plural string corresponding to the specified resource ID based on the specified number. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ----- | ------ | ---- | ----- | | resId | number | Yes | Resource ID.| | num | number | Yes | Number. | **Return value** | Type | Description | | --------------------- | ------------------------- | | Promise<string> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1).then(value => { let str = value; }).catch(error => { console.log("getPluralStringValue promise error is " + error); }); } catch (error) { console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getPluralStringValue9+ getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback<string>): void Obtains the singular-plural string corresponding to the specified resource object based on the specified number. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | --------------------------- | ---- | ------------------------------------ | | resource | [Resource](#resource9) | Yes | Resource object. | | num | number | Yes | Number. | | callback | AsyncCallback<string> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts let resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.plural.test').id }; try { this.context.resourceManager.getPluralStringValue(resource, 1, (error, value) => { if (error != null) { console.log("error is " + error); } else { let str = value; } }); } catch (error) { console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getPluralString9+ getPluralString(resource: Resource, num: number): Promise<string> Obtains the singular-plural string corresponding to the specified resource object based on the specified number. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | Yes | Resource object.| | num | number | Yes | Number. | **Return value** | Type | Description | | --------------------- | ------------------------------ | | Promise<string> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts let resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.plural.test').id }; try { this.context.resourceManager.getPluralString(resource, 1).then(value => { let str = value; }).catch(error => { console.log("getPluralString promise error is " + error); }); } catch (error) { console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getRawFileContent9+ getRawFileContent(path: string, callback: AsyncCallback<Uint8Array>): void Obtains the content of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------------- | ---- | ----------------------- | | path | string | Yes | Path of the raw file. | | callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001005 | If the resource not found by path. | **Example** ```ts try { this.context.resourceManager.getRawFileContent("test.xml", (error, value) => { if (error != null) { console.log("error is " + error); } else { let rawFile = value; } }); } catch (error) { console.error(`callback getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getRawFileContent9+ getRawFileContent(path: string): Promise<Uint8Array> Obtains the content of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ----------- | | path | string | Yes | Path of the raw file.| **Return value** | Type | Description | | ------------------------- | ----------- | | Promise<Uint8Array> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001005 | If the resource not found by path. | **Example** ```ts try { this.context.resourceManager.getRawFileContent("test.xml").then(value => { let rawFile = value; }).catch(error => { console.log("getRawFileContent promise error is " + error); }); } catch (error) { console.error(`promise getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getRawFd9+ getRawFd(path: string, callback: AsyncCallback<RawFileDescriptor>): void Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | -------------------------------- | | path | string | Yes | Path of the raw file. | | callback | AsyncCallback<[RawFileDescriptor](#rawfiledescriptor8)> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001005 | If the resource not found by path. | **Example** ```ts try { this.context.resourceManager.getRawFd("test.xml", (error, value) => { if (error != null) { console.log(`callback getRawFd failed error code: ${error.code}, message: ${error.message}.`); } else { let fd = value.fd; let offset = value.offset; let length = value.length; } }); } catch (error) { console.error(`callback getRawFd failed, error code: ${error.code}, message: ${error.message}.`) }; ``` ### getRawFd9+ getRawFd(path: string): Promise<RawFileDescriptor> Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ----------- | | path | string | Yes | Path of the raw file.| **Return value** | Type | Description | | ---------------------------------------- | ------------------- | | Promise<[RawFileDescriptor](#rawfiledescriptor8)> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001005 | If the resource not found by path. | **Example** ```ts try { this.context.resourceManager.getRawFd("test.xml").then(value => { let fd = value.fd; let offset = value.offset; let length = value.length; }).catch(error => { console.log(`promise getRawFd error error code: ${error.code}, message: ${error.message}.`); }); } catch (error) { console.error(`promise getRawFd failed, error code: ${error.code}, message: ${error.message}.`); }; ``` ### closeRawFileDescriptor8+ closeRawFileDescriptor(path: string, callback: AsyncCallback<void>): void Closes the descriptor of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------- | ---- | ----------- | | path | string | Yes | Path of the raw file.| | callback | AsyncCallback<void> | Yes | Callback used to return the result. | **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.closeRawFileDescriptor("test.xml", (error, value) => { if (error != null) { console.log("error is " + error); } }); }); ``` ### closeRawFileDescriptor8+ closeRawFileDescriptor(path: string): Promise<void> Closes the descriptor of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ----------- | | path | string | Yes | Path of the raw file.| **Return value** | Type | Description | | ------------------- | ---- | | Promise<void> | Promise that returns no value.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.closeRawFileDescriptor("test.xml").then(value => { let result = value; }).catch(error => { console.log("closeRawFileDescriptor promise error is " + error); }); }); ``` ### closeRawFd9+ closeRawFd(path: string, callback: AsyncCallback<void>): void Closes the descriptor of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------- | ---- | ----------- | | path | string | Yes | Path of the raw file.| | callback | AsyncCallback<void> | Yes | Callback used to return the result. | For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001005 | The resource not found by path. | **Example** ```ts try { this.context.resourceManager.closeRawFd("test.xml", (error, value) => { if (error != null) { console.log("error is " + error); } }); } catch (error) { console.error(`callback closeRawFd failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### closeRawFd9+ closeRawFd(path: string): Promise<void> Closes the descriptor of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ----------- | | path | string | Yes | Path of the raw file.| **Return value** | Type | Description | | ------------------- | ---- | | Promise<void> | Promise that returns no value.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001005 | If the resource not found by path. | **Example** ```ts try { this.context.resourceManager.closeRawFd("test.xml").then(value => { let result = value; }).catch(error => { console.log("closeRawFd promise error is " + error); }); } catch (error) { console.error(`promise closeRawFd failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### release7+ release() Releases a created **resourceManager** object. **System capability**: SystemCapability.Global.ResourceManager **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.release(); }); ``` ### getStringByName9+ getStringByName(resName: string, callback: AsyncCallback<string>): void Obtains the string corresponding to the specified resource name. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | --------------------------- | ---- | --------------- | | resName | string | Yes | Resource name. | | callback | AsyncCallback<string> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getStringByName("test", (error, value) => { if (error != null) { console.log("error is " + error); } else { let string = value; } }); } catch (error) { console.error(`callback getStringByName failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getStringByName9+ getStringByName(resName: string): Promise<string> Obtains the string corresponding to the specified resource name. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ------- | ------ | ---- | ---- | | resName | string | Yes | Resource name.| **Return value** | Type | Description | | --------------------- | ---------- | | Promise<string> | String corresponding to the resource name.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getStringByName("test").then(value => { let string = value; }).catch(error => { console.log("getStringByName promise error is " + error); }); } catch (error) { console.error(`promise getStringByName failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getStringArrayByName9+ getStringArrayByName(resName: string, callback: AsyncCallback<Array<string>>): void Obtains the string array corresponding to the specified resource name. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | ----------------- | | resName | string | Yes | Resource name. | | callback | AsyncCallback<Array<string>> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getStringArrayByName("test", (error, value) => { if (error != null) { console.log("error is " + error); } else { let strArray = value; } }); } catch (error) { console.error(`callback getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getStringArrayByName9+ getStringArrayByName(resName: string): Promise<Array<string>> Obtains the string array corresponding to the specified resource name. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ------- | ------ | ---- | ---- | | resName | string | Yes | Resource name.| **Return value** | Type | Description | | ---------------------------------- | ------------ | | Promise<Array<string>> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getStringArrayByName("test").then(value => { let strArray = value; }).catch(error => { console.log("getStringArrayByName promise error is " + error); }); } catch (error) { console.error(`promise getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getMediaByName9+ getMediaByName(resName: string, callback: AsyncCallback<Uint8Array>): void Obtains the content of the media file corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------------- | ---- | ------------------ | | resName | string | Yes | Resource name. | | callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getMediaByName("test", (error, value) => { if (error != null) { console.log("error is " + error); } else { let media = value; } }); } catch (error) { console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getMediaByName9+ getMediaByName(resName: string): Promise<Uint8Array> Obtains the content of the media file corresponding to the specified resource name. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ------- | ------ | ---- | ---- | | resName | string | Yes | Resource name.| **Return value** | Type | Description | | ------------------------- | ------------- | | Promise<Uint8Array> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getMediaByName("test").then(value => { let media = value; }).catch(error => { console.log("getMediaByName promise error is " + error); }); } catch (error) { console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getMediaBase64ByName9+ getMediaBase64ByName(resName: string, callback: AsyncCallback<string>): void Obtains the Base64 code of the image corresponding to the specified resource name. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | --------------------------- | ---- | ------------------------ | | resName | string | Yes | Resource name. | | callback | AsyncCallback<string> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getMediaBase64ByName("test", (error, value) => { if (error != null) { console.log("error is " + error); } else { let media = value; } }); } catch (error) { console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getMediaBase64ByName9+ getMediaBase64ByName(resName: string): Promise<string> Obtains the Base64 code of the image corresponding to the specified resource name. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ------- | ------ | ---- | ---- | | resName | string | Yes | Resource name.| **Return value** | Type | Description | | --------------------- | ------------------- | | Promise<string> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getMediaBase64ByName("test").then(value => { let media = value; }).catch(error => { console.log("getMediaBase64ByName promise error is " + error); }); } catch (error) { console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getPluralStringByName9+ getPluralStringByName(resName: string, num: number, callback: AsyncCallback<string>): void Obtains the plural string corresponding to the specified resource name based on the specified number. This API uses an asynchronous callback to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | --------------------------- | ---- | ----------------------------- | | resName | string | Yes | Resource name. | | num | number | Yes | Number. | | callback | AsyncCallback<string> | Yes | Callback used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getPluralStringByName("test", 1, (error, value) => { if (error != null) { console.log("error is " + error); } else { let str = value; } }); } catch (error) { console.error(`callback getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getPluralStringByName9+ getPluralStringByName(resName: string, num: number): Promise<string> Obtains the plural string corresponding to the specified resource name based on the specified number. This API uses a promise to return the result. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ------- | ------ | ---- | ---- | | resName | string | Yes | Resource name.| | num | number | Yes | Number. | **Return value** | Type | Description | | --------------------- | ---------------------- | | Promise<string> | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getPluralStringByName("test", 1).then(value => { let str = value; }).catch(error => { console.log("getPluralStringByName promise error is " + error); }); } catch (error) { console.error(`promise getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getStringSync9+ getStringSync(resId: number): string Obtains the string corresponding to the specified resource ID. This API returns the result synchronously. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ----- | ------ | ---- | ----- | | resId | number | Yes | Resource ID.| **Return value** | Type | Description | | ------ | ----------- | | string | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getStringSync($r('app.string.test').id); } catch (error) { console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getStringSync9+ getStringSync(resource: Resource): string Obtains the string corresponding to the specified resource object. This API returns the result synchronously. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | Yes | Resource object.| **Return value** | Type | Description | | ------ | ---------------- | | string | Promise used to return the result.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts let resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.string.test').id }; try { this.context.resourceManager.getStringSync(resource); } catch (error) { console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getStringByNameSync9+ getStringByNameSync(resName: string): string Obtains the string corresponding to the specified resource name. This API returns the result synchronously. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ------- | ------ | ---- | ---- | | resName | string | Yes | Resource name.| **Return value** | Type | Description | | ------ | ---------- | | string | String corresponding to the specified resource name.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getStringByNameSync("test"); } catch (error) { console.error(`getStringByNameSync failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getBoolean9+ getBoolean(resId: number): boolean Obtains the Boolean result corresponding to the specified resource ID. This API returns the result synchronously. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ----- | ------ | ---- | ----- | | resId | number | Yes | Resource ID.| **Return value** | Type | Description | | ------- | ------------ | | boolean | Boolean result corresponding to the specified resource ID.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getBoolean($r('app.boolean.boolean_test').id); } catch (error) { console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getBoolean9+ getBoolean(resource: Resource): boolean Obtains the Boolean result corresponding to the specified resource object. This API returns the result synchronously. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | Yes | Resource object.| **Return value** | Type | Description | | ------- | ----------------- | | boolean | Boolean result corresponding to the specified resource object.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts let resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.boolean.boolean_test').id }; try { this.context.resourceManager.getBoolean(resource); } catch (error) { console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getBooleanByName9+ getBooleanByName(resName: string): boolean Obtains the Boolean result corresponding to the specified resource name. This API returns the result synchronously. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ------- | ------ | ---- | ---- | | resName | string | Yes | Resource name.| **Return value** | Type | Description | | ------- | ----------- | | boolean | Boolean result corresponding to the specified resource name.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getBooleanByName("boolean_test"); } catch (error) { console.error(`getBooleanByName failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getNumber9+ getNumber(resId: number): number Obtains the integer or float value corresponding to the specified resource ID. This API returns the result synchronously. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ----- | ------ | ---- | ----- | | resId | number | Yes | Resource ID.| **Return value** | Type | Description | | ------ | ---------- | | number | Integer or float value corresponding to the specified resource ID.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getNumber($r('app.integer.integer_test').id); } catch (error) { console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`) } try { this.context.resourceManager.getNumber($r('app.float.float_test').id); } catch (error) { console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getNumber9+ getNumber(resource: Resource): number Obtains the integer or float value corresponding to the specified resource object. This API returns the result synchronously. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | Yes | Resource object.| **Return value** | Type | Description | | ------ | --------------- | | number | Integer or float value corresponding to the specified resource object.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **Example** ```ts let resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.integer.integer_test').id }; try { this.context.resourceManager.getNumber(resource); } catch (error) { console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getNumberByName9+ getNumberByName(resName: string): number Obtains the integer or float value corresponding to the specified resource name. This API returns the result synchronously. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ------- | ------ | ---- | ---- | | resName | string | Yes | Resource name.| **Return value** | Type | Description | | ------ | --------- | | number | Integer or float value corresponding to the specified resource name.| For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). **Error codes** | ID| Error Message| | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **Example** ```ts try { this.context.resourceManager.getNumberByName("integer_test"); } catch (error) { console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`) } try { this.context.resourceManager.getNumberByName("float_test"); } catch (error) { console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getString(deprecated) getString(resId: number, callback: AsyncCallback<string>): void Obtains the string corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. This API is deprecated since API version 9. You are advised to use [getStringValue](#getstringvalue9) instead. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | --------------------------- | ---- | --------------- | | resId | number | Yes | Resource ID. | | callback | AsyncCallback<string> | Yes | Callback used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getString($r('app.string.test').id, (error, value) => { if (error != null) { console.log("error is " + error); } else { let str = value; } }); }); ``` ### getString(deprecated) getString(resId: number): Promise<string> Obtains the string corresponding to the specified resource ID. This API uses a promise to return the result. This API is deprecated since API version 9. You are advised to use [getStringValue](#getstringvalue9-1) instead. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ----- | ------ | ---- | ----- | | resId | number | Yes | Resource ID.| **Return value** | Type | Description | | --------------------- | ----------- | | Promise<string> | Promise used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getString($r('app.string.test').id).then(value => { let str = value; }).catch(error => { console.log("getstring promise error is " + error); }); }); ``` ### getStringArray(deprecated) getStringArray(resId: number, callback: AsyncCallback<Array<string>>): void Obtains the string array corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. This API is deprecated since API version 9. You are advised to use [getStringArrayValue](#getstringarrayvalue9) instead. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | ----------------- | | resId | number | Yes | Resource ID. | | callback | AsyncCallback<Array<string>> | Yes | Callback used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getStringArray($r('app.strarray.test').id, (error, value) => { if (error != null) { console.log("error is " + error); } else { let strArray = value; } }); }); ``` ### getStringArray(deprecated) getStringArray(resId: number): Promise<Array<string>> Obtains the string array corresponding to the specified resource ID. This API uses a promise to return the result. This API is deprecated since API version 9. You are advised to use [getStringArrayValue](#getstringarrayvalue9-1) instead. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ----- | ------ | ---- | ----- | | resId | number | Yes | Resource ID.| **Return value** | Type | Description | | ---------------------------------- | ------------- | | Promise<Array<string>> | Promise used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getStringArray($r('app.strarray.test').id).then(value => { let strArray = value; }).catch(error => { console.log("getStringArray promise error is " + error); }); }); ``` ### getMedia(deprecated) getMedia(resId: number, callback: AsyncCallback<Uint8Array>): void Obtains the content of the media file corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. This API is deprecated since API version 9. You are advised to use [getMediaContent](#getmediacontent9) instead. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------------- | ---- | ------------------ | | resId | number | Yes | Resource ID. | | callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getMedia($r('app.media.test').id, (error, value) => { if (error != null) { console.log("error is " + error); } else { let media = value; } }); }); ``` ### getMedia(deprecated) getMedia(resId: number): Promise<Uint8Array> Obtains the content of the media file corresponding to the specified resource ID. This API uses a promise to return the result. This API is deprecated since API version 9. You are advised to use [getMediaContent](#getmediacontent9-1) instead. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ----- | ------ | ---- | ----- | | resId | number | Yes | Resource ID.| **Return value** | Type | Description | | ------------------------- | -------------- | | Promise<Uint8Array> | Promise used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getMedia($r('app.media.test').id).then(value => { let media = value; }).catch(error => { console.log("getMedia promise error is " + error); }); }); ``` ### getMediaBase64(deprecated) getMediaBase64(resId: number, callback: AsyncCallback<string>): void Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. This API is deprecated since API version 9. You are advised to use [getMediaContentBase64](#getmediacontentbase649) instead. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | --------------------------- | ---- | ------------------------ | | resId | number | Yes | Resource ID. | | callback | AsyncCallback<string> | Yes | Callback used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getMediaBase64($r('app.media.test').id, (error, value) => { if (error != null) { console.log("error is " + error); } else { let media = value; } }); }); ``` ### getMediaBase64(deprecated) getMediaBase64(resId: number): Promise<string> Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses a promise to return the result. This API is deprecated since API version 9. You are advised to use [getMediaContentBase64](#getmediacontentbase649-1) instead. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ----- | ------ | ---- | ----- | | resId | number | Yes | Resource ID.| **Return value** | Type | Description | | --------------------- | -------------------- | | Promise<string> | Promise used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getMediaBase64($r('app.media.test').id).then(value => { let media = value; }).catch(error => { console.log("getMediaBase64 promise error is " + error); }); }); ``` ### getPluralString(deprecated) getPluralString(resId: number, num: number): Promise<string> Obtains the singular-plural string corresponding to the specified resource ID based on the specified number. This API uses a promise to return the result. This API is deprecated since API version 9. You are advised to use [getPluralStringValue](#getpluralstringvalue9) instead. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ----- | ------ | ---- | ----- | | resId | number | Yes | Resource ID.| | num | number | Yes | Number. | **Return value** | Type | Description | | --------------------- | ------------------------- | | Promise<string> | Promise used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getPluralString($r("app.plural.test").id, 1).then(value => { let str = value; }).catch(error => { console.log("getPluralString promise error is " + error); }); }); ``` ### getPluralString(deprecated) getPluralString(resId: number, num: number, callback: AsyncCallback<string>): void Obtains the singular-plural string corresponding to the specified resource ID based on the specified number. This API uses an asynchronous callback to return the result. This API is deprecated since API version 9. You are advised to use [getPluralStringValue](#getpluralstringvalue9-1) instead. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | --------------------------- | ---- | ------------------------------- | | resId | number | Yes | Resource ID. | | num | number | Yes | Number. | | callback | AsyncCallback<string> | Yes | Callback used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getPluralString($r("app.plural.test").id, 1, (error, value) => { if (error != null) { console.log("error is " + error); } else { let str = value; } }); }); ``` ### getRawFile(deprecated) getRawFile(path: string, callback: AsyncCallback<Uint8Array>): void Obtains the content of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. This API is deprecated since API version 9. You are advised to use [getRawFileContent](#getrawfilecontent9) instead. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ------------------------------- | ---- | ----------------------- | | path | string | Yes | Path of the raw file. | | callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getRawFile("test.xml", (error, value) => { if (error != null) { console.log("error is " + error); } else { let rawFile = value; } }); }); ``` ### getRawFile(deprecated) getRawFile(path: string): Promise<Uint8Array> Obtains the content of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. This API is deprecated since API version 9. You are advised to use [getRawFileContent](#getrawfilecontent9-1) instead. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ----------- | | path | string | Yes | Path of the raw file.| **Return value** | Type | Description | | ------------------------- | ----------- | | Promise<Uint8Array> | Promise used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getRawFile("test.xml").then(value => { let rawFile = value; }).catch(error => { console.log("getRawFile promise error is " + error); }); }); ``` ### getRawFileDescriptor(deprecated) getRawFileDescriptor(path: string, callback: AsyncCallback<RawFileDescriptor>): void Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. This API is deprecated since API version 9. You are advised to use [getRawFd](#getrawfd9) instead. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | -------- | ---------------------------------------- | ---- | -------------------------------- | | path | string | Yes | Path of the raw file. | | callback | AsyncCallback<[RawFileDescriptor](#rawfiledescriptor8)> | Yes | Callback used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getRawFileDescriptor("test.xml", (error, value) => { if (error != null) { console.log("error is " + error); } else { let fd = value.fd; let offset = value.offset; let length = value.length; } }); }); ``` ### getRawFileDescriptor(deprecated) getRawFileDescriptor(path: string): Promise<RawFileDescriptor> Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. This API is deprecated since API version 9. You are advised to use [getRawFd](#getrawfd9-1) instead. **System capability**: SystemCapability.Global.ResourceManager **Parameters** | Name | Type | Mandatory | Description | | ---- | ------ | ---- | ----------- | | path | string | Yes | Path of the raw file.| **Return value** | Type | Description | | ---------------------------------------- | ------------------- | | Promise<[RawFileDescriptor](#rawfiledescriptor8)> | Promise used to return the result.| **Example** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getRawFileDescriptor("test.xml").then(value => { let fd = value.fd; let offset = value.offset; let length = value.length; }).catch(error => { console.log("getRawFileDescriptor promise error is " + error); }); }); ```