# @ohos.resourceManager (资源管理) 资源管理模块,根据当前configuration:语言、区域、横竖屏、Mcc(移动国家码)和Mnc(移动网络码)、Device capability(设备类型)、Density(分辨率)提供获取应用资源信息读取接口。 > **说明:** > > 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 ## 导入模块 ```js import resourceManager from '@ohos.resourceManager'; ``` ## 使用说明 从API Version9开始,Stage模型通过context获取resourceManager对象的方式后,可直接调用其内部获取资源的接口,无需再导入包。此方式FA模型不适用,FA模型还需要先导入包,再调用[getResourceManager](#resourcemanagergetresourcemanager)接口获取资源对象。 Stage模型下Context的引用方法请参考[Stage模型的Context详细介绍](../../application-models/application-context-stage.md)。 ```ts import UIAbility from '@ohos.app.ability.UIAbility'; export default class EntryAbility extends UIAbility { onWindowStageCreate(windowStage) { let context = this.context; let resourceManager = context.resourceManager; } } ``` ## resourceManager.getResourceManager getResourceManager(callback: AsyncCallback<ResourceManager>): void 获取当前应用的资源管理对象,使用callback形式返回ResourceManager对象。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在FA模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ----------------------------- | | callback | AsyncCallback<[ResourceManager](#resourcemanager)> | 是 | callback方式返回ResourceManager对象 | **示例:** ```js resourceManager.getResourceManager((error, mgr) => { if (error != null) { console.log("error is " + error); return; } mgr.getStringValue(0x1000000, (error, value) => { if (error != null) { console.log("error is " + error); } else { let str = value; } }); }); ``` 注:示例代码中的0x1000000表示资源对应的id, 其可在编译后的文件ResourceTable.txt中找到。 ## resourceManager.getResourceManager getResourceManager(bundleName: string, callback: AsyncCallback<ResourceManager>): void 获取指定应用的资源管理对象,使用callback形式返回ResourceManager对象。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在FA模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ---------------------------------------- | ---- | ----------------------------- | | bundleName | string | 是 | 指定应用的Bundle名称 | | callback | AsyncCallback<[ResourceManager](#resourcemanager)> | 是 | callback方式返回ResourceManager对象 | **示例:** ```js resourceManager.getResourceManager("com.example.myapplication", (error, mgr) => { }); ``` ## resourceManager.getResourceManager getResourceManager(): Promise<ResourceManager> 获取当前应用的资源管理对象,使用Promise形式返回ResourceManager对象。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在FA模型下使用。 **返回值:** | 类型 | 说明 | | ---------------------------------------- | ----------------- | | Promise<[ResourceManager](#resourcemanager)> | Promise方式返回资源管理对象 | **示例:** ```js import resourceManager from '@ohos.resourceManager'; import { BusinessError } from '@ohos.base'; resourceManager.getResourceManager().then((mgr: resourceManager.ResourceManager) => { mgr.getStringValue(0x1000000, (error, value) => { if (error != null) { console.log("error is " + error); } else { let str = value; } }); }).catch((error: BusinessError) => { console.log("error is " + error); }); ``` 注:示例代码中的0x1000000表示资源对应的id, 其可在编译后的文件ResourceTable.txt中找到。 ## resourceManager.getResourceManager getResourceManager(bundleName: string): Promise<ResourceManager> 获取指定应用的资源管理对象,使用Promise形式返回ResourceManager对象。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在FA模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------ | ---- | ------------- | | bundleName | string | 是 | 指定应用的Bundle名称 | **返回值:** | 类型 | 说明 | | ---------------------------------------- | ------------------ | | Promise<[ResourceManager](#resourcemanager)> | Promise方式返回的资源管理对象 | **示例:** ```js import resourceManager from '@ohos.resourceManager'; import { BusinessError } from '@ohos.base'; resourceManager.getResourceManager("com.example.myapplication").then((mgr: resourceManager.ResourceManager) => { }).catch((error: BusinessError) => { }); ``` ## resourceManager.getSystemResourceManager10+ getSystemResourceManager(): ResourceManager 获取系统资源管理对象,返回系统资源的ResourceManager对象。 **系统能力**:SystemCapability.Global.ResourceManager **返回值:** | 类型 | 说明 | | ---------------------------------------- | ------------------ | | Resourcemanager | 返回系统资源的管理对象 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001009 | If application can't access system resource. | **示例:** ```js import resourceManager from '@ohos.resourceManager'; import { BusinessError } from '@ohos.base'; try { let systemResourceManager = resourceManager.getSystemResourceManager(); systemResourceManager.getStringValue($r('sys.string.ohos_lab_vibrate').id).then((value: string) => { let str = value; }).catch((error: BusinessError) => { console.log("systemResourceManager getStringValue promise error is " + error); }); } catch (error) { console.error(`systemResourceManager getStringValue failed, error code: ${error.code}, message: ${error.message}.`); } ``` ## Direction 用于表示设备屏幕方向。 **系统能力**:SystemCapability.Global.ResourceManager | 名称 | 值 | 说明 | | -------------------- | ---- | ---- | | DIRECTION_VERTICAL | 0 | 竖屏 | | DIRECTION_HORIZONTAL | 1 | 横屏 | ## DeviceType 用于表示当前设备类型。 **系统能力**:SystemCapability.Global.ResourceManager | 名称 | 值 | 说明 | | -------------------- | ---- | ---- | | DEVICE_TYPE_PHONE | 0x00 | 手机 | | DEVICE_TYPE_TABLET | 0x01 | 平板 | | DEVICE_TYPE_CAR | 0x02 | 汽车 | | DEVICE_TYPE_PC | 0x03 | 电脑 | | DEVICE_TYPE_TV | 0x04 | 电视 | | DEVICE_TYPE_WEARABLE | 0x06 | 穿戴 | ## ScreenDensity 用于表示当前设备屏幕密度。 **系统能力**:SystemCapability.Global.ResourceManager | 名称 | 值 | 说明 | | -------------- | ---- | ---------- | | SCREEN_SDPI | 120 | 小规模的屏幕密度 | | SCREEN_MDPI | 160 | 中规模的屏幕密度 | | SCREEN_LDPI | 240 | 大规模的屏幕密度 | | SCREEN_XLDPI | 320 | 特大规模的屏幕密度 | | SCREEN_XXLDPI | 480 | 超大规模的屏幕密度 | | SCREEN_XXXLDPI | 640 | 超特大规模的屏幕密度 | ## Configuration 表示当前设备的状态。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 名称 | 类型 | 可读 | 可写 | 说明 | | --------- | ----------------------- | ---- | ---- | -------- | | direction | [Direction](#direction) | 是 | 否 | 当前设备屏幕方向 | | locale | string | 是 | 否 | 当前系统语言 | ## DeviceCapability 表示设备支持的能力。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 名称 | 类型 | 可读 | 可写 | 说明 | | ------------- | ------------------------------- | ---- | ---- | -------- | | screenDensity | [ScreenDensity](#screendensity) | 是 | 否 | 当前设备屏幕密度 | | deviceType | [DeviceType](#devicetype) | 是 | 否 | 当前设备类型 | ## RawFileDescriptor8+ 表示rawfile的descriptor信息。 **系统能力:** SystemCapability.Global.ResourceManager **参数:** | 名称 | 类型 | 可读 | 可写 | 说明 | | ------ | ------ | ---- | ---- | ------------------ | | fd | number | 是 | 否 | rawfile所在hap的文件描述符 | | offset | number | 是 | 否 | rawfile的起始偏移量 | | length | number | 是 | 否 | rawfile的文件长度 | ## Resource9+ 表示的资源信息。 **系统能力:** 以下各项对应的系统能力均为SystemCapability.Global.ResourceManager **参数:** | 名称 | 类型 | 可读 | 可写 |说明 | | ---------- | ------ | ----- | ---- | ---------------| | bundleName | string | 是 | 否 | 应用的bundle名称 | | moduleName | string | 是 | 否 | 应用的module名称 | | id | number | 是 | 否 | 资源的id值 | ## ResourceManager 提供访问应用资源的能力。 > **说明:** > > - ResourceManager涉及到的方法,仅限基于TS扩展的声明式开发范式使用。 > > - 资源文件在工程的resources目录中定义,id可通过$r(资源地址).id的方式获取,例如$r('app.string.test').id。 ### getStringSync9+ getStringSync(resId: number): string 用户获取指定资源ID对应的字符串,使用同步方式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | **返回值:** | 类型 | 说明 | | ------ | ----------- | | string | 资源ID值对应的字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts try { this.context.resourceManager.getStringSync($r('app.string.test').id); } catch (error) { console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getStringSync10+ getStringSync(resId: number, ...args: Array): string 用户获取指定资源ID对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | | args | Array | 否 | 格式化字符串资源参数
支持参数类型:
%d、%f、%s、%%
说明:%%转译符,转译%
举例:%%d格式化后为%d字符串| **返回值:** | 类型 | 说明 | | ------ | ---------------------------- | | string | 资源ID值对应的格式化字符串| **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | | 9001007 | If the resource obtained by resId formatting error. | **示例:** ```ts try { this.context.resourceManager.getStringSync($r('app.string.test').id, "format string", 10, 98.78); } catch (error) { console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getStringSync9+ getStringSync(resource: Resource): string 用户获取指定resource对象对应的字符串,使用同步方式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | 是 | 资源信息 | **返回值:** | 类型 | 说明 | | ------ | ---------------- | | string | resource对象对应的字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.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}.`); } ``` ### getStringSync10+ getStringSync(resource: Resource, ...args: Array): string 用户获取指定resource对象对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | 是 | 资源信息 | | args | Array | 否 | 格式化字符串资源参数
支持参数类型:
%d、%f、%s、%%
说明:%%转译符,转译%
举例:%%d格式化后为%d字符串| **返回值:** | 类型 | 说明 | | ------ | ---------------------------- | | string | resource对象对应的格式化字符串| **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | | 9001007 | If the resource obtained by resId formatting error. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.string.test').id }; try { this.context.resourceManager.getStringSync(resource, "format string", 10, 98.78); } catch (error) { console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getStringByNameSync9+ getStringByNameSync(resName: string): string 用户获取指定资源名称对应的字符串,使用同步方式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | ------ | ---- | ---- | | resName | string | 是 | 资源名称 | **返回值:** | 类型 | 说明 | | ------ | ---------- | | string | 资源名称对应的字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts try { this.context.resourceManager.getStringByNameSync("test"); } catch (error) { console.error(`getStringByNameSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getStringByNameSync10+ getStringByNameSync(resName: string, ...args: Array): string 用户获取指定资源名称对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | ------ | ---- | ---- | | resName | string | 是 | 资源名称 | | args | Array | 否 | 格式化字符串资源参数
支持参数类型:
%d、%f、%s、%%
说明:%%转译符,转译%
举例:%%d格式化后为%d字符串| **返回值:** | 类型 | 说明 | | ------ | ---------------------------- | | string | 资源名称对应的格式化字符串| **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | | 9001008 | If the resource obtained by resName formatting error. | **示例:** ```ts try { this.context.resourceManager.getStringByNameSync("test", "format string", 10, 98.78); } catch (error) { console.error(`getStringByNameSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getStringValue9+ getStringValue(resId: number, callback: AsyncCallback<string>): void 用户获取指定资源ID对应的字符串,使用callback形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | --------------- | | resId | number | 是 | 资源ID值 | | callback | AsyncCallback<string> | 是 | 异步回调,用于返回获取的字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the module resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例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> 用户获取指定资源ID对应的字符串,使用Promise形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | **返回值:** | 类型 | 说明 | | --------------------- | ----------- | | Promise<string> | 资源ID值对应的字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getStringValue($r('app.string.test').id).then((value: string) => { let str = value; }).catch((error: BusinessError) => { 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 用户获取指定resource对象对应的字符串,使用callback形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | --------------- | | resource | [Resource](#resource9) | 是 | 资源信息 | | callback | AsyncCallback<string> | 是 | 异步回调,用于返回获取的字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.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> 用户获取指定resource对象对应的字符串,使用Promise形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | 是 | 资源信息 | **返回值:** | 类型 | 说明 | | --------------------- | ---------------- | | Promise<string> | resource对象对应的字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; import { BusinessError } from '@ohos.base'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.string.test').id }; try { this.context.resourceManager.getStringValue(resource).then((value: string) => { let str = value; }).catch((error: BusinessError) => { console.log("getStringValue promise error is " + error); }); } catch (error) { console.error(`promise getStringValue failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getStringByName9+ getStringByName(resName: string, callback: AsyncCallback<string>): void 用户获取指定资源名称对应的字符串,使用callback形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | --------------- | | resName | string | 是 | 资源名称 | | callback | AsyncCallback<string> | 是 | 异步回调,用于返回获取的字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts try { this.context.resourceManager.getStringByName("test", (error, value) => { if (error != null) { console.log("error is " + error); } else { let str = value; } }); } catch (error) { console.error(`callback getStringByName failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getStringByName9+ getStringByName(resName: string): Promise<string> 用户获取指定资源名称对应的字符串,使用Promise形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | ------ | ---- | ---- | | resName | string | 是 | 资源名称 | **返回值:** | 类型 | 说明 | | --------------------- | ---------- | | Promise<string> | 资源名称对应的字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getStringByName("test").then((value: string) => { let str = value; }).catch((error: BusinessError) => { console.log("getStringByName promise error is " + error); }); } catch (error) { console.error(`promise getStringByName failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getStringArrayValueSync10+ getStringArrayValueSync(resId: number): Array<string> 用户获取指定资源ID对应的字符串数组,使用同步方式返回字符串数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | **返回值:** | 类型 | 说明 | | --------------------- | ----------- | | Array<string> | 资源ID值对应的字符串数组 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts try { this.context.resourceManager.getStringArrayValueSync($r('app.strarray.test').id); } catch (error) { console.error(`getStringArrayValueSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getStringArrayValueSync10+ getStringArrayValueSync(resource: Resource): Array<string> 用户获取指定resource对象对应的字符串数组,使用同步方式返回字符串数组。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resource | [Resource](#resource9) | 是 | 资源信息 | **返回值:** | 类型 | 说明 | | --------------------- | ----------- | | Array<string> | resource对象对应的字符串数组 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.strarray.test').id }; try { this.context.resourceManager.getStringArrayValueSync(resource); } catch (error) { console.error(`getStringArrayValueSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getStringArrayByNameSync10+ getStringArrayByNameSync(resName: string): Array<string> 用户获取指定资源名称对应的字符串数组,使用同步方式返回字符串数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resName | string | 是 | 资源名称 | **返回值:** | 类型 | 说明 | | --------------------- | ----------- | | Array<string> | 对应资源名称的字符串数组 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts try { this.context.resourceManager.getStringArrayByNameSync("test"); } catch (error) { console.error(`getStringArrayByNameSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getStringArrayValue9+ getStringArrayValue(resId: number, callback: AsyncCallback<Array<string>>): void 用户获取指定资源ID对应的字符串数组,使用callback形式返回字符串数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ----------------- | | resId | number | 是 | 资源ID值 | | callback | AsyncCallback<Array<string>> | 是 | 异步回调,用于返回获取的字符串数组 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```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>> 用户获取指定资源ID对应的字符串数组,使用Promise形式返回字符串数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | **返回值:** | 类型 | 说明 | | ---------------------------------- | ------------- | | Promise<Array<string>> | 资源ID值对应的字符串数组 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id).then((value: Array) => { let strArray = value; }).catch((error: BusinessError) => { 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 用户获取指定resource对象对应的字符串数组,使用callback形式返回回字符串数组。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ----------------- | | resource | [Resource](#resource9) | 是 | 资源信息 | | callback | AsyncCallback<Array<string>> | 是 | 异步回调,用于返回获取的字符串数组 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.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>> 用户获取指定resource对象对应的字符串数组,使用Promise形式返回字符串数组。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | 是 | 资源信息 | **返回值:** | 类型 | 说明 | | ---------------------------------- | ------------------ | | Promise<Array<string>> | resource对象对应的字符串数组 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; import { BusinessError } from '@ohos.base'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.strarray.test').id }; try { this.context.resourceManager.getStringArrayValue(resource).then((value: Array) => { let strArray = value; }).catch((error: BusinessError) => { console.log("getStringArray promise error is " + error); }); } catch (error) { console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getStringArrayByName9+ getStringArrayByName(resName: string, callback: AsyncCallback<Array<string>>): void 用户获取指定资源名称对应的字符串数组,使用callback形式返回字符串数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ----------------- | | resName | string | 是 | 资源名称 | | callback | AsyncCallback<Array<string>> | 是 | 异步回调,用于返回获取的字符串数组 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **示例:** ```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>> 用户获取指定资源名称对应的字符串数组,使用Promise形式返回字符串数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | ------ | ---- | ---- | | resName | string | 是 | 资源名称 | **返回值:** | 类型 | 说明 | | ---------------------------------- | ------------ | | Promise<Array<string>> | 资源名称对应的字符串数组 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getStringArrayByName("test").then((value: Array) => { let strArray = value; }).catch((error: BusinessError) => { console.log("getStringArrayByName promise error is " + error); }); } catch (error) { console.error(`promise getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getPluralStringValueSync10+ getPluralStringValueSync(resId: number, num: number): string 根据指定数量获取指定ID字符串表示的单复数字符串,使用同步方式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | | num | number | 是 | 数量值 | **返回值:** | 类型 | 说明 | | -------- | ----------- | | string | 根据指定数量获取指定ID字符串表示的单复数字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts try { this.context.resourceManager.getPluralStringValueSync($r('app.plural.test').id, 1); } catch (error) { console.error(`getPluralStringValueSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getPluralStringValueSync10+ getPluralStringValueSync(resource: Resource, num: number): string 根据指定数量获取指定resource对象表示的单复数字符串,使用同步方式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resource | [Resource](#resource9) | 是 | 资源信息 | | num | number | 是 | 数量值 | **返回值:** | 类型 | 说明 | | --------------------- | ----------- | | string | 根据指定数量获取指定resource对象表示的单复数字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.plural.test').id }; try { this.context.resourceManager.getPluralStringValueSync(resource, 1); } catch (error) { console.error(`getPluralStringValueSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getPluralStringByNameSync10+ getPluralStringByNameSync(resName: string, num: number): string 根据指定数量获取指定资源名称表示的单复数字符串,使用同步方式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resName | string | 是 | 资源名称 | | num | number | 是 | 数量值 | **返回值:** | 类型 | 说明 | | --------------------- | ----------- | | string | 根据指定数量获取指定资源名称表示的单复数字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts try { this.context.resourceManager.getPluralStringByNameSync("test", 1); } catch (error) { console.error(`getPluralStringByNameSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getPluralStringValue9+ getPluralStringValue(resId: number, num: number, callback: AsyncCallback<string>): void 根据指定数量获取指定ID字符串表示的单复数字符串,使用callback形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | ------------------------------- | | resId | number | 是 | 资源ID值 | | num | number | 是 | 数量值 | | callback | AsyncCallback<string> | 是 | 异步回调,返回根据指定数量获取指定ID字符串表示的单复数字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```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> 根据指定数量获取对指定ID字符串表示的单复数字符串,使用Promise形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | | num | number | 是 | 数量值 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------------- | | Promise<string> | 根据提供的数量获取对应ID字符串表示的单复数字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1).then((value: string) => { let str = value; }).catch((error: BusinessError) => { console.log("getPluralStringValue promise error is " + error); }); } catch (error) { console.error(`promise getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getPluralStringValue9+ getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback<string>): void 根据指定数量获取指定resource对象表示的单复数字符串,使用callback形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | ------------------------------------ | | resource | [Resource](#resource9) | 是 | 资源信息 | | num | number | 是 | 数量值 | | callback | AsyncCallback<string> | 是 | 异步回调,返回根据指定数量获取指定resource对象表示的单复数字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.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}.`); } ``` ### getPluralStringValue9+ getPluralStringValue(resource: Resource, num: number): Promise<string> 根据指定数量获取对指定resource对象表示的单复数字符串,使用Promise形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | 是 | 资源信息 | | num | number | 是 | 数量值 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------------------ | | Promise<string> | 根据提供的数量获取对应resource对象表示的单复数字符串 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; import { BusinessError } from '@ohos.base'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.plural.test').id }; try { this.context.resourceManager.getPluralStringValue(resource, 1).then((value: string) => { let str = value; }).catch((error: BusinessError) => { console.log("getPluralStringValue promise error is " + error); }); } catch (error) { console.error(`promise getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getPluralStringByName9+ getPluralStringByName(resName: string, num: number, callback: AsyncCallback<string>): void 根据传入的数量值,获取资源名称对应的字符串资源,使用callback形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | ----------------------------- | | resName | string | 是 | 资源名称 | | num | number | 是 | 数量值 | | callback | AsyncCallback<string> | 是 | 异步回调,返回根据传入的数量值获取资源名称对应的字符串资源 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **示例:** ```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> 根据传入的数量值,获取资源名称对应的字符串资源,使用Promise形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | ------ | ---- | ---- | | resName | string | 是 | 资源名称 | | num | number | 是 | 数量值 | **返回值:** | 类型 | 说明 | | --------------------- | ---------------------- | | Promise<string> | 根据传入的数量值获取资源名称对应的字符串资源 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getPluralStringByName("test", 1).then((value: string) => { let str = value; }).catch((error: BusinessError) => { console.log("getPluralStringByName promise error is " + error); }); } catch (error) { console.error(`promise getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContentSync10+ getMediaContentSync(resId: number, density?: number): Uint8Array 用户获取指定资源ID对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | | [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 | **返回值:** | 类型 | 说明 | | -------- | ----------- | | Uint8Array | 资源ID对应的媒体文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts try { this.context.resourceManager.getMediaContentSync($r('app.media.test').id); // 默认屏幕密度 } catch (error) { console.error(`getMediaContentSync failed, error code: ${error.code}, message: ${error.message}.`); } try { this.context.resourceManager.getMediaContentSync($r('app.media.test').id, 120); // 指定屏幕密度 } catch (error) { console.error(`getMediaContentSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContentSync10+ getMediaContentSync(resource: Resource, density?: number): Uint8Array 用户获取指定resource对象对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resource | [Resource](#resource9) | 是 | 资源信息 | | [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 | **返回值:** | 类型 | 说明 | | --------------------- | ----------- | | Uint8Array | resource对象对应的媒体文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.media.test').id }; try { this.context.resourceManager.getMediaContentSync(resource); // 默认屏幕密度 } catch (error) { console.error(`getMediaContentSync failed, error code: ${error.code}, message: ${error.message}.`); } try { this.context.resourceManager.getMediaContentSync(resource, 120); // 指定屏幕密度 } catch (error) { console.error(`getMediaContentSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaByNameSync10+ getMediaByNameSync(resName: string, density?: number): Uint8Array 用户获取指定资源名称对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resName | string | 是 | 资源名称 | | [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 | **返回值:** | 类型 | 说明 | | --------------------- | ----------- | | Uint8Array | 对应资源名称的媒体文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | **示例:** ```ts try { this.context.resourceManager.getMediaByNameSync("test"); // 默认屏幕密度 } catch (error) { console.error(`getMediaByNameSync failed, error code: ${error.code}, message: ${error.message}.`); } try { this.context.resourceManager.getMediaByNameSync("test", 120); // 指定屏幕密度 } catch (error) { console.error(`getMediaByNameSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContent9+ getMediaContent(resId: number, callback: AsyncCallback<Uint8Array>): void 用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------- | ---- | ------------------ | | resId | number | 是 | 资源ID值 | | callback | AsyncCallback<Uint8Array> | 是 | 异步回调,用于返回获取的媒体文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```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}.`); } ``` ### getMediaContent10+ getMediaContent(resId: number, density: number, callback: AsyncCallback<Uint8Array>): void 用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------- | ---- | ------------------ | | resId | number | 是 | 资源ID值 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | callback | AsyncCallback<Uint8Array> | 是 | 异步回调,用于返回获取的媒体文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts try { this.context.resourceManager.getMediaContent($r('app.media.test').id, 120, (error, value) => { if (error != null) { console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); } else { let media = value; } }); } catch (error) { console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContent9+ getMediaContent(resId: number): Promise<Uint8Array> 用户获取指定资源ID对应的媒体文件内容,使用Promise形式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | **返回值:** | 类型 | 说明 | | ------------------------- | -------------- | | Promise<Uint8Array> | 资源ID值对应的媒体文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getMediaContent($r('app.media.test').id).then((value: Uint8Array) => { let media = value; }).catch((error: BusinessError) => { console.log("getMediaContent promise error is " + error); }); } catch (error) { console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContent10+ getMediaContent(resId: number, density: number): Promise<Uint8Array> 用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | **返回值:** | 类型 | 说明 | | ------------------------- | -------------- | | Promise<Uint8Array> | 资源ID值对应的媒体文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getMediaContent($r('app.media.test').id, 120).then((value: Uint8Array) => { let media = value; }).catch((error: BusinessError) => { console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); }); } catch (error) { console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContent9+ getMediaContent(resource: Resource, callback: AsyncCallback<Uint8Array>): void 用户获取指定resource对象对应的媒体文件内容,使用callback形式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------- | ---- | ------------------ | | resource | [Resource](#resource9) | 是 | 资源信息 | | callback | AsyncCallback<Uint8Array> | 是 | 异步回调,用于返回获取的媒体文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.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}.`); } ``` ### getMediaContent10+ getMediaContent(resource: Resource, density: number, callback: AsyncCallback<Uint8Array>): void 用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------- | ---- | ------------------ | | resource | [Resource](#resource9) | 是 | 资源信息 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | callback | AsyncCallback<Uint8Array> | 是 | 异步回调,用于返回获取的媒体文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.media.test').id }; try { this.context.resourceManager.getMediaContent(resource, 120, (error, value) => { if (error != null) { console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); } else { let media = value; } }); } catch (error) { console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContent9+ getMediaContent(resource: Resource): Promise<Uint8Array> 用户获取指定resource对象对应的媒体文件内容,使用Promise形式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | 是 | 资源信息 | **返回值:** | 类型 | 说明 | | ------------------------- | ------------------- | | Promise<Uint8Array> | resource对象对应的媒体文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; import { BusinessError } from '@ohos.base'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.media.test').id }; try { this.context.resourceManager.getMediaContent(resource).then((value: Uint8Array) => { let media = value; }).catch((error: BusinessError) => { console.log("getMediaContent promise error is " + error); }); } catch (error) { console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContent10+ getMediaContent(resource: Resource, density: number): Promise<Uint8Array> 用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | 是 | 资源信息 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | **返回值:** | 类型 | 说明 | | ------------------------- | ------------------- | | Promise<Uint8Array> | resource对象对应的媒体文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; import { BusinessError } from '@ohos.base'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.media.test').id }; try { this.context.resourceManager.getMediaContent(resource, 120).then((value: Uint8Array) => { let media = value; }).catch((error: BusinessError) => { console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); }); } catch (error) { console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaByName9+ getMediaByName(resName: string, callback: AsyncCallback<Uint8Array>): void 用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------- | ---- | ------------------ | | resName | string | 是 | 资源名称 | | callback | AsyncCallback<Uint8Array> | 是 | 异步回调,用于返回获取的媒体文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | **示例:** ```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}.`); } ``` ### getMediaByName10+ getMediaByName(resName: string, density: number, callback: AsyncCallback<Uint8Array>): void 用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback形式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------- | ---- | ------------------ | | resName | string | 是 | 资源名称 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | callback | AsyncCallback<Uint8Array> | 是 | 异步回调,用于返回获取的媒体文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | **示例:** ```ts try { this.context.resourceManager.getMediaByName("test", 120, (error, value) => { if (error != null) { console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); } else { let media = value; } }); } catch (error) { console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaByName9+ getMediaByName(resName: string): Promise<Uint8Array> 用户获取指定资源名称对应的媒体文件内容,使用Promise形式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | ------ | ---- | ---- | | resName | string | 是 | 资源名称 | **返回值:** | 类型 | 说明 | | ------------------------- | ------------- | | Promise<Uint8Array> | 资源名称对应的媒体文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getMediaByName("test").then((value: Uint8Array) => { let media = value; }).catch((error: BusinessError) => { console.log("getMediaByName promise error is " + error); }); } catch (error) { console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`) } ``` ### getMediaByName10+ getMediaByName(resName: string, density: number): Promise<Uint8Array> 用户获取指定资源名称对应的指定屏幕密度媒体文件内容,使用Promise形式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | ------ | ---- | ---- | | resName | string | 是 | 资源名称 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | **返回值:** | 类型 | 说明 | | ------------------------- | ------------- | | Promise<Uint8Array> | 资源名称对应的媒体文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getMediaByName("test", 120).then((value: Uint8Array) => { let media = value; }).catch((error: BusinessError) => { console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); }); } catch (error) { console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContentBase64Sync10+ getMediaContentBase64Sync(resId: number, density?: number): string 用户获取指定资源ID对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | | [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 | **返回值:** | 类型 | 说明 | | -------- | ----------- | | string | 资源ID对应的图片资源Base64编码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts try { this.context.resourceManager.getMediaContentBase64Sync($r('app.media.test').id); // 默认屏幕密度 } catch (error) { console.error(`getMediaContentBase64Sync failed, error code: ${error.code}, message: ${error.message}.`); } try { this.context.resourceManager.getMediaContentBase64Sync($r('app.media.test').id, 120); // 指定屏幕密度 } catch (error) { console.error(`getMediaContentBase64Sync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContentBase64Sync10+ getMediaContentBase64Sync(resource: Resource, density?: number): string 用户获取指定resource对象对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resource | [Resource](#resource9) | 是 | 资源信息 | | [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 | **返回值:** | 类型 | 说明 | | --------------------- | ----------- | | string | resource对象对应的图片资源Base64编码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.media.test').id }; try { this.context.resourceManager.getMediaContentBase64Sync(resource); // 默认屏幕密度 } catch (error) { console.error(`getMediaContentBase64Sync failed, error code: ${error.code}, message: ${error.message}.`); } try { this.context.resourceManager.getMediaContentBase64Sync(resource, 120); // 指定屏幕密度 } catch (error) { console.error(`getMediaContentBase64Sync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaBase64ByNameSync10+ getMediaBase64ByNameSync(resName: string, density?: number): string 用户获取指定资源名称对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resName | string | 是 | 资源名称 | | [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 | **返回值:** | 类型 | 说明 | | --------------------- | ----------- | | string | 资源名称对应的图片资源Base64编码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | **示例:** ```ts try { this.context.resourceManager.getMediaBase64ByNameSync("test"); // 默认屏幕密度 } catch (error) { console.error(`getMediaBase64ByNameSync failed, error code: ${error.code}, message: ${error.message}.`); } try { this.context.resourceManager.getMediaBase64ByNameSync("test", 120); // 指定屏幕密度 } catch (error) { console.error(`getMediaBase64ByNameSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContentBase649+ getMediaContentBase64(resId: number, callback: AsyncCallback<string>): void 用户获取指定资源ID对应的图片资源Base64编码,使用callback形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** Content | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | ------------------------ | | resId | number | 是 | 资源ID值 | | callback | AsyncCallback<string> | 是 | 异步回调,用于返回获取的图片资源Base64编码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```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}.`); } ``` ### getMediaContentBase6410+ getMediaContentBase64(resId: number, density: number, callback: AsyncCallback<string>): void 用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | ------------------------ | | resId | number | 是 | 资源ID值 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | callback | AsyncCallback<string> | 是 | 异步回调,用于返回获取的图片资源Base64编码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts try { this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120, (error, value) => { if (error != null) { console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); } else { let media = value; } }); } catch (error) { console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContentBase649+ getMediaContentBase64(resId: number): Promise<string> 用户获取指定资源ID对应的图片资源Base64编码,使用Promise形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | **返回值:** | 类型 | 说明 | | --------------------- | -------------------- | | Promise<string> | 资源ID值对应的图片资源Base64编码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getMediaContentBase64($r('app.media.test').id).then((value: string) => { let media = value; }).catch((error: BusinessError) => { console.log("getMediaContentBase64 promise error is " + error); }); } catch (error) { console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContentBase6410+ getMediaContentBase64(resId: number, density: number): Promise<string> 用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | **返回值:** | 类型 | 说明 | | --------------------- | -------------------- | | Promise<string> | 资源ID值对应的图片资源Base64编码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120).then((value: string) => { let media = value; }).catch((error: BusinessError) => { console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); }); } catch (error) { console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContentBase649+ getMediaContentBase64(resource: Resource, callback: AsyncCallback<string>): void 用户获取指定resource对象对应的图片资源Base64编码,使用callback形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | ------------------------ | | resource | [Resource](#resource9) | 是 | 资源信息 | | callback | AsyncCallback<string> | 是 | 异步回调,用于返回获取的图片资源Base64编码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.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(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContentBase6410+ getMediaContentBase64(resource: Resource, density: number, callback: AsyncCallback<string>): void 用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | ------------------------ | | resource | [Resource](#resource9) | 是 | 资源信息 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | callback | AsyncCallback<string> | 是 | 异步回调,用于返回获取的图片资源Base64编码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.media.test').id }; try { this.context.resourceManager.getMediaContentBase64(resource, 120, (error, value) => { if (error != null) { console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); } else { let media = value; } }); } catch (error) { console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContentBase649+ getMediaContentBase64(resource: Resource): Promise<string> 用户获取指定resource对象对应的图片资源Base64编码,使用Promise形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | 是 | 资源信息 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------------- | | Promise<string> | resource对象对应的图片资源Base64编码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; import { BusinessError } from '@ohos.base'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.media.test').id }; try { this.context.resourceManager.getMediaContentBase64(resource).then((value: string) => { let media = value; }).catch((error: BusinessError) => { console.log("getMediaContentBase64 promise error is " + error); }); } catch (error) { console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaContentBase6410+ getMediaContentBase64(resource: Resource, density: number): Promise<string> 用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | 是 | 资源信息 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------------- | | Promise<string> | resource对象对应的图片资源Base64编码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; import { BusinessError } from '@ohos.base'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.media.test').id }; try { this.context.resourceManager.getMediaContentBase64(resource, 120).then((value: string) => { let media = value; }).catch((error: BusinessError) => { console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); }); } catch (error) { console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaBase64ByName9+ getMediaBase64ByName(resName: string, callback: AsyncCallback<string>): void 用户获取指定资源名称对应的图片资源Base64编码,使用callback形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | ------------------------ | | resName | string | 是 | 资源名称 | | callback | AsyncCallback<string> | 是 | 异步回调,用于返回获取的图片资源Base64编码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | **示例:** ```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}.`); } ``` ### getMediaBase64ByName10+ getMediaBase64ByName(resName: string, density: number, callback: AsyncCallback<string>): void 用户获取指定资源名称对应的指定屏幕密度图片资源Base64编码,使用callback形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | ------------------------ | | resName | string | 是 | 资源名称 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | | callback | AsyncCallback<string> | 是 | 异步回调,用于返回获取的图片资源Base64编码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | **示例:** ```ts try { this.context.resourceManager.getMediaBase64ByName("test", 120, (error, value) => { if (error != null) { console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); } else { let media = value; } }); } catch (error) { console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaBase64ByName9+ getMediaBase64ByName(resName: string): Promise<string> 用户获取指定资源名称对应的图片资源Base64编码,使用Promise形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | ------ | ---- | ---- | | resName | string | 是 | 资源名称 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------- | | Promise<string> | 资源名称对应的图片资源Base64编码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getMediaBase64ByName("test").then((value: string) => { let media = value; }).catch((error: BusinessError) => { console.log("getMediaBase64ByName promise error is " + error); }); } catch (error) { console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getMediaBase64ByName10+ getMediaBase64ByName(resName: string, density: number): Promise<string> 用户获取指定资源名称对应的指定屏幕密度图片资源Base64编码,使用Promise形式返回字符串。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | ------ | ---- | ---- | | resName | string | 是 | 资源名称 | | [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------- | | Promise<string> | 资源名称对应的图片资源Base64编码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getMediaBase64ByName("test", 120).then((value: string) => { let media = value; }).catch((error: BusinessError) => { console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); }); } catch (error) { console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getDrawableDescriptor10+ getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor; 用户获取指定资源ID对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | | [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 | **返回值:** | 类型 | 说明 | | ------ | ---------- | | DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts try { this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id); } catch (error) { console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`); } try { this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id, 120); } catch (error) { console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getDrawableDescriptor10+ getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor; 用户获取指定resource对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | 是 | 资源信息 | | [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 | **返回值:** | 类型 | 说明 | | ------- | ----------------- | | DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.media.icon').id }; try { this.context.resourceManager.getDrawableDescriptor(resource); } catch (error) { console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`); } try { this.context.resourceManager.getDrawableDescriptor(resource, 120); } catch (error) { console.error(`getDrawableDescriptor failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getDrawableDescriptorByName10+ getDrawableDescriptorByName(resName: string, density?: number): DrawableDescriptor; 用户获取指定资源名称对应的DrawableDescriptor对象,使用同步方式返回资源对应的DrawableDescriptor,用于图标的显示。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | ------ | ---- | ---- | | resName | string | 是 | 资源名称 | | [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度 | **返回值:** | 类型 | 说明 | | ------ | --------- | | DrawableDescriptor | 资源ID值对应的DrawableDescriptor对象 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | **示例:** ```ts try { this.context.resourceManager.getDrawableDescriptorByName('icon'); } catch (error) { console.error(`getDrawableDescriptorByName failed, error code: ${error.code}, message: ${error.message}.`); } try { this.context.resourceManager.getDrawableDescriptorByName('icon', 120); } catch (error) { console.error(`getDrawableDescriptorByName failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getBoolean9+ getBoolean(resId: number): boolean 使用同步方式,返回获取指定资源ID对应的布尔结果。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | **返回值:** | 类型 | 说明 | | ------- | ------------ | | boolean | 资源ID值对应的布尔结果 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```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 使用同步方式,返回获取指定resource对象对应的布尔结果。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | 是 | 资源信息 | **返回值:** | 类型 | 说明 | | ------- | ----------------- | | boolean | resource对象对应的布尔结果 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.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 使用同步方式,返回获取指定资源名称对应的布尔结果 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | ------ | ---- | ---- | | resName | string | 是 | 资源名称 | **返回值:** | 类型 | 说明 | | ------- | ----------- | | boolean | 资源名称对应的布尔结果 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **示例:** ```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 用户获取指定资源ID对应的integer数值或者float数值,使用同步方式返回资源对应的数值。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | **返回值:** | 类型 | 说明 | | ------ | ---------- | | number | 资源ID值对应的数值。Integer对应的是原数值,float对应的是真实像素点值,具体参考示例代码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts try { this.context.resourceManager.getNumber($r('app.integer.integer_test').id); // integer对应返回的是原数值 } catch (error) { console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`); } try { this.context.resourceManager.getNumber($r('app.float.float_test').id); // float对应返回的是真实像素点值 } catch (error) { console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getNumber9+ getNumber(resource: Resource): number 用户获取指定resource对象对应的integer数值或者float数值,使用同步方式返回资源对应的数值。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | 是 | 资源信息 | **返回值:** | 类型 | 说明 | | ------ | --------------- | | number | resource对象对应的数值。Integer对应的是原数值,float对应的是真实像素点值, 具体参考示例代码 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.integer.integer_test').id }; try { this.context.resourceManager.getNumber(resource);// integer对应返回的是原数值, float对应返回的是真实像素点值 } catch (error) { console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getNumberByName9+ getNumberByName(resName: string): number 用户获取指定资源名称对应的integer数值或者float数值,使用同步方式资源对应的数值。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | ------ | ---- | ---- | | resName | string | 是 | 资源名称 | **返回值:** | 类型 | 说明 | | ------ | --------- | | number | 资源名称对应的数值 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **示例:** ```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}.`); } ``` ### getColorSync10+ getColorSync(resId: number) : number; 用户获取指定资源ID对应的颜色值,使用同步方式返回其对应的颜色值。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | **返回值:** | 类型 | 说明 | | ------ | ----------- | | number | 资源ID值对应的颜色值(十进制) | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts try { this.context.resourceManager.getColorSync($r('app.color.test').id); } catch (error) { console.error(`getColorSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getColorSync10+ getColorSync(resource: Resource): number 用户获取指定resource对象对应的颜色值,使用同步方式返回其对应的颜色值。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | 是 | 资源信息 | **返回值:** | 类型 | 说明 | | ------ | ---------------- | | number | resource对象对应的颜色值(十进制) | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.color.test').id }; try { this.context.resourceManager.getColorSync(resource); } catch (error) { console.error(`getColorSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getColorByNameSync10+ getColorByNameSync(resName: string) : number; 用户获取指定资源名称对应的颜色值,使用同步方式返回其对应的颜色值。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | ------ | ---- | ---- | | resName | string | 是 | 资源名称 | **返回值:** | 类型 | 说明 | | ------ | ---------- | | number | 资源名称对应的颜色值(十进制) | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts try { this.context.resourceManager.getColorByNameSync("test"); } catch (error) { console.error(`getColorByNameSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getColor10+ getColor(resId: number, callback: AsyncCallback<number>): void; 用户获取指定资源ID对应的颜色值,使用callback形式返回其对应的颜色值。 **系统能力:** SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | --------------- | | resId | number | 是 | 资源ID值 | | callback | AsyncCallback<number> | 是 | 异步回调,用于返回获取的颜色值(十进制) | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the module resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例Stage:** ```ts try { this.context.resourceManager.getColor($r('app.color.test').id, (error, value) => { if (error != null) { console.log("error is " + error); } else { let str = value; } }); } catch (error) { console.error(`callback getColor failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getColor10+ getColor(resId: number): Promise<number> 用户获取指定资源ID对应的颜色值,使用Promise形式返回对应其对应的颜色值。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | **返回值:** | 类型 | 说明 | | --------------------- | ----------- | | Promise<number> | 资源ID值对应的颜色值(十进制) | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getColor($r('app.color.test').id).then((value: number) => { let str = value; }).catch((error: BusinessError) => { console.log("getColor promise error is " + error); }); } catch (error) { console.error(`promise getColor failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getColor10+ getColor(resource: Resource, callback: AsyncCallback<number>): void; 用户获取指定resource对象对应的颜色值,使用callback形式返回其对应的颜色值。 **系统能力:** SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | --------------- | | resource | [Resource](#resource9) | 是 | 资源信息 | | callback | AsyncCallback<number> | 是 | 异步回调,用于返回获取的颜色值(十进制) | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.color.test').id }; try { this.context.resourceManager.getColor(resource, (error, value) => { if (error != null) { console.log("error is " + error); } else { let str = value; } }); } catch (error) { console.error(`callback getColor failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getColor10+ getColor(resource: Resource): Promise<number>; 用户获取指定resource对象对应的颜色值,使用Promise形式返回其对应的颜色值。 **系统能力**:SystemCapability.Global.ResourceManager **模型约束**:此接口仅可在Stage模型下使用。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | resource | [Resource](#resource9) | 是 | 资源信息 | **返回值:** | 类型 | 说明 | | --------------------- | ---------------- | | Promise<number> | resource对象对应的颜色值(十进制) | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001001 | If the resId invalid. | | 9001002 | If the resource not found by resId. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import resourceManager from '@ohos.resourceManager'; import { BusinessError } from '@ohos.base'; let resource: resourceManager.Resource = { bundleName: "com.example.myapplication", moduleName: "entry", id: $r('app.color.test').id }; try { this.context.resourceManager.getColor(resource).then((value: number) => { let str = value; }).catch((error: BusinessError) => { console.log("getColor promise error is " + error); }); } catch (error) { console.error(`promise getColor failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getColorByName10+ getColorByName(resName: string, callback: AsyncCallback<number>): void 用户获取指定资源名称对应的颜色值,使用callback形式返回其对应的颜色值。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | --------------- | | resName | string | 是 | 资源名称 | | callback | AsyncCallback<number> | 是 | 异步回调,用于返回获取的颜色值(十进制) | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts try { this.context.resourceManager.getColorByName("test", (error, value) => { if (error != null) { console.log("error is " + error); } else { let string = value; } }); } catch (error) { console.error(`callback getColorByName failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getColorByName10+ getColorByName(resName: string): Promise<number> 用户获取指定资源名称对应的颜色值,使用Promise形式返回其对应的颜色值。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | ------ | ---- | ---- | | resName | string | 是 | 资源名称 | **返回值:** | 类型 | 说明 | | --------------------- | ---------- | | Promise<number> | 资源名称对应的颜色值(十进制) | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001003 | If the resName invalid. | | 9001004 | If the resource not found by resName. | | 9001006 | If the resource re-ref too much. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getColorByName("test").then((value: number) => { let string = value; }).catch((error: BusinessError) => { console.log("getColorByName promise error is " + error); }); } catch (error) { console.error(`promise getColorByName failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getRawFileContentSync10+ getRawFileContentSync(path: string): Uint8Array 用户获取resources/rawfile目录下对应的rawfile文件内容,使用同步形式返回字节数组。 **系统能力:** SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------- | ---- | ----------------------- | | path | string | 是 | rawfile文件路径 | **返回值:** | 类型 | 说明 | | --------------------- | ---------- | | Uint8Array | 返回获取的rawfile文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001005 | If the resource not found by path. | **示例:** ```ts try { this.context.resourceManager.getRawFileContentSync("test.txt"); } catch (error) { console.error(`getRawFileContentSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getRawFileContent9+ getRawFileContent(path: string, callback: AsyncCallback<Uint8Array>): void 用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback形式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------- | ---- | ----------------------- | | path | string | 是 | rawfile文件路径 | | callback | AsyncCallback<Uint8Array> | 是 | 异步回调,用于返回获取的rawfile文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001005 | If the resource not found by path. | **示例:** ```ts try { this.context.resourceManager.getRawFileContent("test.txt", (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> 用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise形式返回字节数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---- | ------ | ---- | ----------- | | path | string | 是 | rawfile文件路径 | **返回值:** | 类型 | 说明 | | ------------------------- | ----------- | | Promise<Uint8Array> | rawfile文件内容 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001005 | If the resource not found by path. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getRawFileContent("test.txt").then((value: Uint8Array) => { let rawFile = value; }).catch((error: BusinessError) => { console.log("getRawFileContent promise error is " + error); }); } catch (error) { console.error(`promise getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getRawFileListSync10+ getRawFileListSync(path: string): Array\ 用户获取resources/rawfile目录下文件夹及文件列表,使用同步形式返回文件列表的字符串数组。 **系统能力:** SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------- | ---- | ----------------------- | | path | string | 是 | rawfile文件夹路径 | **返回值:** | 类型 | 说明 | | ------------------------- | ----------- | | Array\ | rawfile文件夹下的列表(包含子文件夹和文件) | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001005 | If the resource not found by path. | **示例:** ```ts try { // 传入""表示获取rawfile根目录下的文件列表 this.context.resourceManager.getRawFileListSync("") } catch (error) { console.error(`getRawFileListSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getRawFileList10+ getRawFileList(path: string, callback: AsyncCallback<Array\>): void; 用户获取resources/rawfile目录下文件夹及文件列表,使用callback形式返回文件列表的字符串数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------- | ---- | ----------------------- | | path | string | 是 | rawfile文件夹路径 | | callback | AsyncCallback<Array\> | 是 | 异步回调,用于返回获取rawfile文件目录下的文件列表 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001005 | If the resource not found by path. | **示例:** ```ts try { // 传入""表示获取rawfile根目录下的文件列表 this.context.resourceManager.getRawFileList("", (error, value) => { if (error != null) { console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); } else { let rawFile = value; } }); } catch (error) { console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getRawFileList10+ getRawFileList(path: string): Promise<Array\> 用户获取resources/rawfile目录下文件夹及文件列表,使用Promise形式返回文件列表字符串数组。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---- | ------ | ---- | ----------- | | path | string | 是 | rawfile文件夹路径 | **返回值:** | 类型 | 说明 | | ------------------------- | ----------- | | Promise<Array\> | rawfile文件目录下的文件列表 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001005 | If the resource not found by path. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { // 传入""表示获取rawfile根目录下的文件列表 this.context.resourceManager.getRawFileList("").then((value: Array) => { let rawFile = value; }).catch((error: BusinessError) => { console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); }); } catch (error) { console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getRawFdSync10+ getRawFdSync(path: string): RawFileDescriptor 用户获取resources/rawfile目录下对应rawfile文件的descriptor。 **系统能力:** SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | -------------------------------- | | path | string | 是 | rawfile文件路径 | **返回值:** | 类型 | 说明 | | ------------------------- | ----------- | | [RawFileDescriptor](#rawfiledescriptor8) | rawfile文件的descriptor | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001005 | If the resource not found by path. | **示例:** ```ts try { this.context.resourceManager.getRawFdSync("test.txt"); } catch (error) { console.error(`getRawFdSync failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getRawFd9+ getRawFd(path: string, callback: AsyncCallback<RawFileDescriptor>): void 用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用callback形式返回。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | -------------------------------- | | path | string | 是 | rawfile文件路径 | | callback | AsyncCallback<[RawFileDescriptor](#rawfiledescriptor8)> | 是 | 异步回调,用于返回获取的rawfile文件的descriptor | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001005 | If the resource not found by path. | **示例:** ```ts try { this.context.resourceManager.getRawFd("test.txt", (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> 用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用Promise形式返回。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---- | ------ | ---- | ----------- | | path | string | 是 | rawfile文件路径 | **返回值:** | 类型 | 说明 | | ---------------------------------------- | ------------------- | | Promise<[RawFileDescriptor](#rawfiledescriptor8)> | rawfile文件descriptor | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001005 | If the resource not found by path. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getRawFd("test.txt").then((value: resourceManager.RawFileDescriptor) => { let fd = value.fd; let offset = value.offset; let length = value.length; }).catch((error: BusinessError) => { 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}.`); } ``` ### closeRawFdSync10+ closeRawFdSync(path: string): void 用户关闭resources/rawfile目录下rawfile文件的descriptor。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------- | ---- | ----------- | | path | string | 是 | rawfile文件路径 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001005 | The resource not found by path. | **示例:** ```ts try { this.context.resourceManager.closeRawFdSync("test.txt"); } catch (error) { console.error(`closeRawFd failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### closeRawFd9+ closeRawFd(path: string, callback: AsyncCallback<void>): void 用户关闭resources/rawfile目录下rawfile文件的descriptor,使用callback形式返回。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------- | ---- | ----------- | | path | string | 是 | rawfile文件路径 | | callback | AsyncCallback<void> | 是 | 异步回调 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001005 | The resource not found by path. | **示例:** ```ts try { this.context.resourceManager.closeRawFd("test.txt", (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> 用户关闭resources/rawfile目录下rawfile文件的descriptor,使用Promise形式返回。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---- | ------ | ---- | ----------- | | path | string | 是 | rawfile文件路径 | **返回值:** | 类型 | 说明 | | ------------------- | ---- | | Promise<void> | 无返回值 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001005 | If the resource not found by path. | **示例:** ```ts try { this.context.resourceManager.closeRawFd("test.txt"); } catch (error) { console.error(`promise closeRawFd failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getConfigurationSync getConfigurationSync(): Configuration 用户获取设备的Configuration,使用同步形式返回Configuration对象。 **系统能力**:SystemCapability.Global.ResourceManager **返回值:** | 类型 | 说明 | | ---------------------------------------- | ---------------- | | Promise<[Configuration](#configuration)> | 设备的Configuration | **示例:** ```ts try { let value = this.context.resourceManager.getConfigurationSync(); let direction = value.direction; let locale = value.locale; } catch (error) { console.error("getConfigurationSync error is " + error); } ``` ### getConfiguration getConfiguration(callback: AsyncCallback<Configuration>): void 用户获取设备的Configuration,使用callback形式返回Configuration对象。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ------------------------- | | callback | AsyncCallback<[Configuration](#configuration)> | 是 | 异步回调,用于返回设备的Configuration | **示例:** ```ts try { this.context.resourceManager.getConfiguration((error, value) => { if (error != null) { console.error("getConfiguration callback error is " + error); } else { let direction = value.direction; let locale = value.locale; } }); } catch (error) { console.error("getConfiguration callback error is " + error); } ``` ### getConfiguration getConfiguration(): Promise<Configuration> 用户获取设备的Configuration,使用Promise形式返回Configuration对象。 **系统能力**:SystemCapability.Global.ResourceManager **返回值:** | 类型 | 说明 | | ---------------------------------------- | ---------------- | | Promise<[Configuration](#configuration)> | 设备的Configuration | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getConfiguration().then((value: resourceManager.Configuration) => { let direction = value.direction; let locale = value.locale; }).catch((error: BusinessError) => { console.error("getConfiguration promise error is " + error); }); } catch (error) { console.error("getConfiguration promise error is " + error); } ``` ### getDeviceCapabilitySync getDeviceCapabilitySync(): DeviceCapability 用户获取设备的DeviceCapability,使用同步形式返回DeviceCapability对象。 **系统能力**:SystemCapability.Global.ResourceManager **返回值:** | 类型 | 说明 | | ---------------------------------------- | ------------------- | | DeviceCapability | 设备的DeviceCapability | **示例:** ```ts try { let value = this.context.resourceManager.getDeviceCapabilitySync(); let screenDensity = value.screenDensity; let deviceType = value.deviceType; } catch (error) { console.error("getDeviceCapabilitySync error is " + error); } ``` ### getDeviceCapability getDeviceCapability(callback: AsyncCallback<DeviceCapability>): void 用户获取设备的DeviceCapability,使用callback形式返回DeviceCapability对象。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ---------------------------- | | callback | AsyncCallback<[DeviceCapability](#devicecapability)> | 是 | 异步回调,用于返回设备的DeviceCapability | **示例:** ```ts try { this.context.resourceManager.getDeviceCapability((error, value) => { if (error != null) { console.error("getDeviceCapability callback error is " + error); } else { let screenDensity = value.screenDensity; let deviceType = value.deviceType; } }); } catch (error) { console.error("getDeviceCapability callback error is " + error); } ``` ### getDeviceCapability getDeviceCapability(): Promise<DeviceCapability> 用户获取设备的DeviceCapability,使用Promise形式返回DeviceCapability对象。 **系统能力**:SystemCapability.Global.ResourceManager **返回值:** | 类型 | 说明 | | ---------------------------------------- | ------------------- | | Promise<[DeviceCapability](#devicecapability)> | 设备的DeviceCapability | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { this.context.resourceManager.getDeviceCapability().then((value: resourceManager.DeviceCapability) => { let screenDensity = value.screenDensity; let deviceType = value.deviceType; }).catch((error: BusinessError) => { console.error("getDeviceCapability promise error is " + error); }); } catch (error) { console.error("getDeviceCapability promise error is " + error); } ``` ### release7+ release() 用户释放创建的resourceManager。 **系统能力**:SystemCapability.Global.ResourceManager **示例:** ```ts try { this.context.resourceManager.release(); } catch (error) { console.error("release error is " + error); } ``` ### addResource10+ addResource(path: string) : void; 应用运行时,加载指定的资源路径,实现资源覆盖。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | path | string | 是 | 资源路径 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001010 | If the overlay path is invalid. | **示例:** ```ts let path = getContext().bundleCodeDir + "/library1-default-signed.hsp"; try { this.context.resourceManager.addResource(path); } catch (error) { console.error(`addResource failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### removeResource10+ removeResource(path: string) : void; 用户运行时,移除指定的资源路径,还原被覆盖前的资源。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---- | | path | string | 是 | 资源路径 | **错误码:** 以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 9001010 | If the overlay path is invalid. | **示例:** ```ts let path = getContext().bundleCodeDir + "/library1-default-signed.hsp"; try { this.context.resourceManager.removeResource(path); } catch (error) { console.error(`removeResource failed, error code: ${error.code}, message: ${error.message}.`); } ``` ### getString(deprecated) getString(resId: number, callback: AsyncCallback<string>): void 用户获取指定资源ID对应的字符串,使用callback形式返回字符串。 从API version 9开始不再维护,建议使用[getStringValue](#getstringvalue9)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | --------------- | | resId | number | 是 | 资源ID值 | | callback | AsyncCallback<string> | 是 | 异步回调,用于返回获取的字符串 | **示例:** ```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> 用户获取指定资源ID对应的字符串,使用Promise形式返回字符串。 从API version 9开始不再维护,建议使用[getStringValue](#getstringvalue9-1)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | **返回值:** | 类型 | 说明 | | --------------------- | ----------- | | Promise<string> | 资源ID值对应的字符串 | **示例:** ```ts import { BusinessError } from '@ohos.base'; resourceManager.getResourceManager((error, mgr) => { mgr.getString($r('app.string.test').id).then((value: string) => { let str = value; }).catch((error: BusinessError) => { console.log("getstring promise error is " + error); }); }); ``` ### getStringArray(deprecated) getStringArray(resId: number, callback: AsyncCallback<Array<string>>): void 用户获取指定资源ID对应的字符串数组,使用callback形式返回字符串数组。 从API version 9开始不再维护,建议使用[getStringArrayValue](#getstringarrayvalue9)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ----------------- | | resId | number | 是 | 资源ID值 | | callback | AsyncCallback<Array<string>> | 是 | 异步回调,用于返回获取的字符串数组 | **示例:** ```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>> 用户获取指定资源ID对应的字符串数组,使用Promise形式返回字符串数组。 从API version 9开始不再维护,建议使用[getStringArrayValue](#getstringarrayvalue9-1)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | **返回值:** | 类型 | 说明 | | ---------------------------------- | ------------- | | Promise<Array<string>> | 资源ID值对应的字符串数组 | **示例:** ```ts import { BusinessError } from '@ohos.base'; resourceManager.getResourceManager((error, mgr) => { mgr.getStringArray($r('app.strarray.test').id).then((value: Array) => { let strArray = value; }).catch((error: BusinessError) => { console.log("getStringArray promise error is " + error); }); }); ``` ### getMedia(deprecated) getMedia(resId: number, callback: AsyncCallback<Uint8Array>): void 用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组。 从API version 9开始不再维护,建议使用[getMediaContent](#getmediacontent9)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------- | ---- | ------------------ | | resId | number | 是 | 资源ID值 | | callback | AsyncCallback<Uint8Array> | 是 | 异步回调,用于返回获取的媒体文件内容 | **示例:** ```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> 用户获取指定资源ID对应的媒体文件内容,使用Promise形式返回字节数组。 从API version 9开始不再维护,建议使用[getMediaContent](#getmediacontent9-1)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | **返回值:** | 类型 | 说明 | | ------------------------- | -------------- | | Promise<Uint8Array> | 资源ID值对应的媒体文件内容 | **示例:** ```ts import { BusinessError } from '@ohos.base'; resourceManager.getResourceManager((error, mgr) => { mgr.getMedia($r('app.media.test').id).then((value: Uint8Array) => { let media = value; }).catch((error: BusinessError) => { console.log("getMedia promise error is " + error); }); }); ``` ### getMediaBase64(deprecated) getMediaBase64(resId: number, callback: AsyncCallback<string>): void 用户获取指定资源ID对应的图片资源Base64编码,使用callback形式返回字符串。 从API version 9开始不再维护,建议使用[getMediaContentBase64](#getmediacontentbase649)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | ------------------------ | | resId | number | 是 | 资源ID值 | | callback | AsyncCallback<string> | 是 | 异步回调,用于返回获取的图片资源Base64编码 | **示例:** ```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> 用户获取指定资源ID对应的图片资源Base64编码,使用Promise形式返回字符串。 从API version 9开始不再维护,建议使用[getMediaContentBase64](#getmediacontentbase649-1)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | **返回值:** | 类型 | 说明 | | --------------------- | -------------------- | | Promise<string> | 资源ID值对应的图片资源Base64编码 | **示例:** ```ts import { BusinessError } from '@ohos.base'; resourceManager.getResourceManager((error, mgr) => { mgr.getMediaBase64($r('app.media.test').id).then((value: string) => { let media = value; }).catch((error: BusinessError) => { console.log("getMediaBase64 promise error is " + error); }); }); ``` ### getPluralString(deprecated) getPluralString(resId: number, num: number): Promise<string> 根据指定数量获取对指定ID字符串表示的单复数字符串,使用Promise形式返回字符串。 从API version 9开始不再维护,建议使用[getPluralStringValue](#getpluralstringvalue9)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | ----- | | resId | number | 是 | 资源ID值 | | num | number | 是 | 数量值 | **返回值:** | 类型 | 说明 | | --------------------- | ------------------------- | | Promise<string> | 根据提供的数量获取对应ID字符串表示的单复数字符串 | **示例:** ```ts import { BusinessError } from '@ohos.base'; resourceManager.getResourceManager((error, mgr) => { mgr.getPluralString($r("app.plural.test").id, 1).then((value: string) => { let str = value; }).catch((error: BusinessError) => { console.log("getPluralString promise error is " + error); }); }); ``` ### getPluralString(deprecated) getPluralString(resId: number, num: number, callback: AsyncCallback<string>): void 根据指定数量获取指定ID字符串表示的单复数字符串,使用callback形式返回字符串。 从API version 9开始不再维护,建议使用[getPluralStringValue](#getpluralstringvalue9-1)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------- | ---- | ------------------------------- | | resId | number | 是 | 资源ID值 | | num | number | 是 | 数量值 | | callback | AsyncCallback<string> | 是 | 异步回调,返回根据指定数量获取指定ID字符串表示的单复数字符串 | **示例:** ```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 用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback形式返回字节数组。 从API version 9开始不再维护,建议使用[getRawFileContent](#getrawfilecontent9)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------- | ---- | ----------------------- | | path | string | 是 | rawfile文件路径 | | callback | AsyncCallback<Uint8Array> | 是 | 异步回调,用于返回获取的rawfile文件内容 | **示例:** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getRawFile("test.txt", (error, value) => { if (error != null) { console.log("error is " + error); } else { let rawFile = value; } }); }); ``` ### getRawFile(deprecated) getRawFile(path: string): Promise<Uint8Array> 用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise形式返回字节数组。 从API version 9开始不再维护,建议使用[getRawFileContent](#getrawfilecontent9-1)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---- | ------ | ---- | ----------- | | path | string | 是 | rawfile文件路径 | **返回值:** | 类型 | 说明 | | ------------------------- | ----------- | | Promise<Uint8Array> | rawfile文件内容 | **示例:** ```ts import { BusinessError } from '@ohos.base'; resourceManager.getResourceManager((error, mgr) => { mgr.getRawFile("test.txt").then((value: Uint8Array) => { let rawFile = value; }).catch((error: BusinessError) => { console.log("getRawFile promise error is " + error); }); }); ``` ### getRawFileDescriptor(deprecated) getRawFileDescriptor(path: string, callback: AsyncCallback<RawFileDescriptor>): void 用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用callback形式返回。 从API version 9开始不再维护,建议使用[getRawFd](#getrawfd9)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | -------------------------------- | | path | string | 是 | rawfile文件路径 | | callback | AsyncCallback<[RawFileDescriptor](#rawfiledescriptor8)> | 是 | 异步回调,用于返回获取的rawfile文件的descriptor | **示例:** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.getRawFileDescriptor("test.txt", (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> 用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用Promise形式返回。 从API version 9开始不再维护,建议使用[getRawFd](#getrawfd9-1)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---- | ------ | ---- | ----------- | | path | string | 是 | rawfile文件路径 | **返回值:** | 类型 | 说明 | | ---------------------------------------- | ------------------- | | Promise<[RawFileDescriptor](#rawfiledescriptor8)> | rawfile文件descriptor | **示例:** ```ts import { BusinessError } from '@ohos.base'; resourceManager.getResourceManager((error, mgr) => { mgr.getRawFileDescriptor("test.txt").then((value: resourceManager.RawFileDescriptor) => { let fd = value.fd; let offset = value.offset; let length = value.length; }).catch((error: BusinessError) => { console.log("getRawFileDescriptor promise error is " + error); }); }); ``` ### closeRawFileDescriptor(deprecated) closeRawFileDescriptor(path: string, callback: AsyncCallback<void>): void 用户关闭resources/rawfile目录下rawfile文件的descriptor,使用callback形式返回。 从API version 9开始不再维护,建议使用[closeRawFd](#closerawfd9)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------- | ---- | ----------- | | path | string | 是 | rawfile文件路径 | | callback | AsyncCallback<void> | 是 | 异步回调 | **示例:** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.closeRawFileDescriptor("test.txt", (error, value) => { if (error != null) { console.log("error is " + error); } }); }); ``` ### closeRawFileDescriptor(deprecated) closeRawFileDescriptor(path: string): Promise<void> 用户关闭resources/rawfile目录下rawfile文件的descriptor,使用Promise形式返回。 从API version 9开始不再维护,建议使用[closeRawFd](#closerawfd9-1)代替。 **系统能力**:SystemCapability.Global.ResourceManager **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---- | ------ | ---- | ----------- | | path | string | 是 | rawfile文件路径 | **返回值:** | 类型 | 说明 | | ------------------- | ---- | | Promise<void> | 无返回值 | **示例:** ```ts resourceManager.getResourceManager((error, mgr) => { mgr.closeRawFileDescriptor("test.txt"); }); ```