# @ohos.resourceManager (资源管理)
资源管理模块,根据当前configuration:语言、区域、横竖屏、Mcc(移动国家码)和Mnc(移动网络码)、Device capability(设备类型)、Density(分辨率)提供获取应用资源信息读取接口。
> **说明:**
>
> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 导入模块
```js
import { resourceManager } from '@kit.LocalizationKit'
```
## 使用说明
从API Version9开始,Stage模型通过context获取resourceManager对象的方式后,可直接调用其内部获取资源的接口,无需再导入包。
FA模型仍需要先导入包,再调用[getResourceManager](#resourcemanagergetresourcemanager)接口获取资源对象。
Stage模型下Context的引用方法请参考[Stage模型的Context详细介绍](../../application-models/application-context-stage.md)。
```ts
import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage) {
let context = this.context;
let resourceManager = context.resourceManager;
}
}
```
## resourceManager.getResourceManager
getResourceManager(callback: AsyncCallback<ResourceManager>): void
获取当前应用的资源管理对象,使用callback异步回调。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在FA模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ----------------------------- |
| callback | AsyncCallback<[ResourceManager](#resourcemanager)> | 是 |返回资源管理ResourceManager对象。 |
**示例:**
```js
resourceManager.getResourceManager((error, mgr) => {
if (error != null) {
console.error("error is " + error);
return;
}
mgr.getStringValue($r('app.string.test').id, (error: BusinessError, value: string) => {
if (error != null) {
console.error("error is " + error);
} else {
let str = value;
}
});
});
```
## resourceManager.getResourceManager
getResourceManager(bundleName: string, callback: AsyncCallback<ResourceManager>): void
获取指定应用的资源管理对象,使用callback异步回调。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在FA模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---------- | ---------------------------------------- | ---- | ----------------------------- |
| bundleName | string | 是 | 应用的Bundle名称。 |
| callback | AsyncCallback<[ResourceManager](#resourcemanager)> | 是 | 返回资源管理ResourceManager对象。 |
**示例:**
```js
resourceManager.getResourceManager("com.example.myapplication", (error, mgr) => {
});
```
## resourceManager.getResourceManager
getResourceManager(): Promise<ResourceManager>
获取当前应用的资源管理对象,使用Promise异步回调。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在FA模型下使用。
**返回值:**
| 类型 | 说明 |
| ---------------------------------------- | ----------------- |
| Promise<[ResourceManager](#resourcemanager)> | 返回资源管理ResourceManager对象。 |
**示例:**
```js
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
resourceManager.getResourceManager().then((mgr: resourceManager.ResourceManager) => {
mgr.getStringValue($r('app.string.test').id, (error: BusinessError, value: string) => {
if (error != null) {
console.error("error is " + error);
} else {
let str = value;
}
});
}).catch((error: BusinessError) => {
console.error("error is " + error);
});
```
## resourceManager.getResourceManager
getResourceManager(bundleName: string): Promise<ResourceManager>
获取指定应用的资源管理对象,使用Promise异步回调。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在FA模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---------- | ------ | ---- | ------------- |
| bundleName | string | 是 | 应用的Bundle名称。 |
**返回值:**
| 类型 | 说明 |
| ---------------------------------------- | ------------------ |
| Promise<[ResourceManager](#resourcemanager)> | 返回资源管理ResourceManager对象。 |
**示例:**
```js
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
resourceManager.getResourceManager("com.example.myapplication").then((mgr: resourceManager.ResourceManager) => {
}).catch((error: BusinessError) => {
});
```
## resourceManager.getSystemResourceManager10+
getSystemResourceManager(): ResourceManager
获取系统资源管理对象,返回系统资源的ResourceManager对象。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**返回值:**
| 类型 | 说明 |
| ---------------------------------------- | ------------------ |
| [Resourcemanager](#resourcemanager) | 返回系统资源的管理对象。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001009 | Failed to access the system resource.which is not mapped to application sandbox, This error code will be thrown. |
**示例:**
```js
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
try {
let systemResourceManager = resourceManager.getSystemResourceManager();
systemResourceManager.getStringValue($r('sys.string.ohos_lab_vibrate').id).then((value: string) => {
let str = value;
}).catch((error: BusinessError) => {
console.error("systemResourceManager getStringValue promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`systemResourceManager getStringValue failed, error code: ${code}, message: ${message}.`);
}
```
## Direction
用于表示设备屏幕方向。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
| 名称 | 值 | 说明 |
| -------------------- | ---- | ---- |
| DIRECTION_VERTICAL | 0 | 竖屏。 |
| DIRECTION_HORIZONTAL | 1 | 横屏。 |
## DeviceType
用于表示当前设备类型。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
| 名称 | 值 | 说明 |
| -------------------- | ---- | ---- |
| DEVICE_TYPE_PHONE | 0x00 | 手机。 |
| DEVICE_TYPE_TABLET | 0x01 | 平板。 |
| DEVICE_TYPE_CAR | 0x02 | 汽车。 |
| DEVICE_TYPE_TV | 0x04 | 电视。 |
| DEVICE_TYPE_WEARABLE | 0x06 | 穿戴。 |
| DEVICE_TYPE_2IN111+ | 0x07 | 2in1。 |
## ScreenDensity
用于表示当前设备屏幕密度。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
| 名称 | 值 | 说明 |
| -------------- | ---- | ---------- |
| SCREEN_SDPI | 120 | 小规模的屏幕密度。 |
| SCREEN_MDPI | 160 | 中规模的屏幕密度。 |
| SCREEN_LDPI | 240 | 大规模的屏幕密度。 |
| SCREEN_XLDPI | 320 | 特大规模的屏幕密度。 |
| SCREEN_XXLDPI | 480 | 超大规模的屏幕密度。 |
| SCREEN_XXXLDPI | 640 | 超特大规模的屏幕密度。 |
## ColorMode12+
用于表示当前设备颜色模式。
**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
| 名称 | 值 | 说明 |
| ----- | ---- | ---------- |
| DARK | 0 | 深色模式。 |
| LIGHT | 1 | 浅色模式。 |
## Configuration
表示当前设备的状态。
**系统能力**:SystemCapability.Global.ResourceManager
| 名称 | 类型 | 可读 | 可写 | 说明 |
| --------------------------- | ------------------------------- | ---- | ---- | ------------------ |
| direction | [Direction](#direction) | 是 | 是 | 屏幕方向。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 |
| locale | string | 是 | 是 | 语言文字国家地区。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 |
| deviceType12+ | [DeviceType](#devicetype) | 是 | 是 | 设备类型。
**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 |
| screenDensity12+ | [ScreenDensity](#screendensity) | 是 | 是 | 屏幕密度。
**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 |
| colorMode12+ | [ColorMode](#colormode12) | 是 | 是 | 颜色模式。
**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 |
| mcc12+ | number | 是 | 是 | 移动国家码。
**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 |
| mnc12+ | number | 是 | 是 | 移动网络码。
**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 |
## DeviceCapability
表示设备支持的能力。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
| 名称 | 类型 | 可读 | 可写 | 说明 |
| ------------- | ------------------------------- | ---- | ---- | -------- |
| screenDensity | [ScreenDensity](#screendensity) | 是 | 否 | 当前设备屏幕密度。 |
| deviceType | [DeviceType](#devicetype) | 是 | 否 | 当前设备类型。 |
## RawFileDescriptor8+
type RawFileDescriptor = _RawFileDescriptor
**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Global.ResourceManager
| 类型 | 说明 |
| ------ | ---- |
|[_RawFileDescriptor](rawFileDescriptor.md#rawfiledescriptor-1)|表示rawfile文件所在hap的descriptor信息。|
## Resource9+
type Resource = _Resource
**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Global.ResourceManager
| 类型 | 说明 |
| ------ | ---- |
|[_Resource](resource.md#resource-1)|表示资源信息。|
## ResourceManager
提供访问应用资源的能力。
> **说明:**
>
> - ResourceManager涉及到的方法,仅限基于TS扩展的声明式开发范式使用。
>
> - 资源文件在工程的resources目录中定义,通过resId、resName、resource对象等可以获取对应的字符串、字符串数组等,resId可通过`$r(资源地址).id`的方式获取,例如`$r('app.string.test').id`。
>
> - resource对象适用于多工程应用内的跨包访问,因resource对象需创建对应module的context获取资源,故相比于入参为resId、resName的接口耗时长。
>
> - 单HAP包和跨HAP/HSP包资源的访问方式具体请参考[资源访问](../../quick-start/resource-categories-and-access.md#资源访问)。
>
> - 示例代码中test文件的具体内容请参考[附录](#附录)。
### getStringSync9+
getStringSync(resId: number): string
用户获取指定资源ID对应的字符串,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
**返回值:**
| 类型 | 说明 |
| ------ | ----------- |
| string | 资源ID值对应的字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getStringSync($r('app.string.test').id);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getStringSync failed, error code: ${code}, message: ${message}.`);
}
```
### getStringSync10+
getStringSync(resId: number, ...args: Array): string
用户获取指定资源ID对应的字符串,根据args参数进行格式化,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
| args | Array | 否 | 格式化字符串资源参数。
支持参数类型:%d、%f、%s、%%、%数字`$d`、%数字`$f`、%数字`$s`
说明:%%转义为%; %数字`$d`表示使用第几个参数
举例:%%d格式化后为%d字符串; %1`$d`表示使用第一个参数|
**返回值:**
| 类型 | 说明 |
| ------ | ---------------------------- |
| string | 资源ID值对应的格式化字符串。|
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
| 9001007 | Failed to format the resource obtained based on the resource ID. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getStringSync($r('app.string.test').id, "format string", 10, 98.78);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getStringSync failed, error code: ${code}, message: ${message}.`);
}
```
### getStringSync9+
getStringSync(resource: Resource): string
用户获取指定resource对象对应的字符串,使用同步方式返回字符串。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------------- |
| string | resource对象对应的字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.string.test').id
};
try {
this.context.resourceManager.getStringSync(resource);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getStringSync failed, error code: ${code}, message: ${message}.`);
}
```
### getStringSync10+
getStringSync(resource: Resource, ...args: Array): string
用户获取指定resource对象对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| args | Array | 否 | 格式化字符串资源参数。
支持参数类型:%d、%f、%s、%%、%数字`$d`、%数字`$f`、%数字`$s`
说明:%%转义为%; %数字`$d`表示使用第几个参数
举例:%%d格式化后为%d字符串; %1`$d`表示使用第一个参数|
**返回值:**
| 类型 | 说明 |
| ------ | ---------------------------- |
| string | resource对象对应的格式化字符串。|
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
| 9001007 | Failed to format the resource obtained based on the resource ID. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
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) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getStringSync failed, error code: ${code}, message: ${message}.`);
}
```
### getStringByNameSync9+
getStringByNameSync(resName: string): string
用户获取指定资源名称对应的字符串,使用同步方式返回字符串。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称。 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------- |
| string | 资源名称对应的字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getStringByNameSync("test");
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getStringByNameSync failed, error code: ${code}, message: ${message}.`);
}
```
### getStringByNameSync10+
getStringByNameSync(resName: string, ...args: Array): string
用户获取指定资源名称对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称。 |
| args | Array | 否 | 格式化字符串资源参数。
支持参数类型:%d、%f、%s、%%、%数字`$d`、%数字`$f`、%数字`$s`
说明:%%转义为%; %数字`$d`表示使用第几个参数
举例:%%d格式化后为%d字符串; %1`$d`表示使用第一个参数|
**返回值:**
| 类型 | 说明 |
| ------ | ---------------------------- |
| string | 资源名称对应的格式化字符串。|
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
| 9001008 | Failed to format the resource obtained based on the resource Name. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getStringByNameSync("test", "format string", 10, 98.78);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getStringByNameSync failed, error code: ${code}, message: ${message}.`);
}
```
### getStringValue9+
getStringValue(resId: number, callback: AsyncCallback<string>): void
用户获取指定资源ID对应的字符串,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| resId | number | 是 | 资源ID值。 |
| callback | AsyncCallback<string> | 是 | 返回获取的字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | If the module resId invalid. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例Stage:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getStringValue($r('app.string.test').id, (error: BusinessError, value: string) => {
if (error != null) {
console.error("error is " + error);
} else {
let str = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getStringValue failed, error code: ${code}, message: ${message}.`);
}
```
### getStringValue9+
getStringValue(resId: number): Promise<string>
用户获取指定资源ID对应的字符串,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| Promise<string> | 资源ID值对应的字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getStringValue($r('app.string.test').id).then((value: string) => {
let str = value;
}).catch((error: BusinessError) => {
console.error("getStringValue promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getStringValue failed, error code: ${code}, message: ${message}.`);
}
```
### getStringValue9+
getStringValue(resource: Resource, callback: AsyncCallback<string>): void
用户获取指定resource对象对应的字符串,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| callback | AsyncCallback<string> | 是 | 返回获取的字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.string.test').id
};
try {
this.context.resourceManager.getStringValue(resource, (error: BusinessError, value: string) => {
if (error != null) {
console.error("error is " + error);
} else {
let str = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getStringValue failed, error code: ${code}, message: ${message}.`);
}
```
### getStringValue9+
getStringValue(resource: Resource): Promise<string>
用户获取指定resource对象对应的字符串,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ---------------- |
| Promise<string> | resource对象对应的字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
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.error("getStringValue promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getStringValue failed, error code: ${code}, message: ${message}.`);
}
```
### getStringByName9+
getStringByName(resName: string, callback: AsyncCallback<string>): void
用户获取指定资源名称对应的字符串,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| resName | string | 是 | 资源名称。 |
| callback | AsyncCallback<string> | 是 |返回获取的字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getStringByName("test", (error: BusinessError, value: string) => {
if (error != null) {
console.error("error is " + error);
} else {
let str = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getStringByName failed, error code: ${code}, message: ${message}.`);
}
```
### getStringByName9+
getStringByName(resName: string): Promise<string>
用户获取指定资源名称对应的字符串,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ---------- |
| Promise<string> | 资源名称对应的字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getStringByName("test").then((value: string) => {
let str = value;
}).catch((error: BusinessError) => {
console.error("getStringByName promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getStringByName failed, error code: ${code}, message: ${message}.`);
}
```
### getStringArrayValueSync10+
getStringArrayValueSync(resId: number): Array<string>
用户获取指定资源ID对应的字符串数组,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| Array<string> | 资源ID值对应的字符串数组。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getStringArrayValueSync($r('app.strarray.test').id);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getStringArrayValueSync failed, error code: ${code}, message: ${message}.`);
}
```
### getStringArrayValueSync10+
getStringArrayValueSync(resource: Resource): Array<string>
用户获取指定resource对象对应的字符串数组,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| Array<string> | resource对象对应的字符串数组。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.strarray.test').id
};
try {
this.context.resourceManager.getStringArrayValueSync(resource);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getStringArrayValueSync failed, error code: ${code}, message: ${message}.`);
}
```
### getStringArrayByNameSync10+
getStringArrayByNameSync(resName: string): Array<string>
用户获取指定资源名称对应的字符串数组,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resName | string | 是 | 资源名称。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| Array<string> | 对应资源名称的字符串数组。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
try {
this.context.resourceManager.getStringArrayByNameSync("test");
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getStringArrayByNameSync failed, error code: ${code}, message: ${message}.`);
}
```
### getStringArrayValue9+
getStringArrayValue(resId: number, callback: AsyncCallback<Array<string>>): void
用户获取指定资源ID对应的字符串数组,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resId | number | 是 | 资源ID值。 |
| callback | AsyncCallback<Array<string>> | 是 | 返回获取的字符串数组。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id, (error: BusinessError, value: Array) => {
if (error != null) {
console.error("error is " + error);
} else {
let strArray = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getStringArrayValue failed, error code: ${code}, message: ${message}.`);
}
```
### getStringArrayValue9+
getStringArrayValue(resId: number): Promise<Array<string>>
用户获取指定资源ID对应的字符串数组,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值 |
**返回值:**
| 类型 | 说明 |
| ---------------------------------- | ------------- |
| Promise<Array<string>> | 资源ID值对应的字符串数组。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id).then((value: Array) => {
let strArray = value;
}).catch((error: BusinessError) => {
console.error("getStringArrayValue promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getStringArrayValue failed, error code: ${code}, message: ${message}.`);
}
```
### getStringArrayValue9+
getStringArrayValue(resource: Resource, callback: AsyncCallback<Array<string>>): void
用户获取指定resource对象对应的字符串数组,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| callback | AsyncCallback<Array<string>> | 是 | 返回获取的字符串数组。|
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.strarray.test').id
};
try {
this.context.resourceManager.getStringArrayValue(resource, (error: BusinessError, value: Array) => {
if (error != null) {
console.error("error is " + error);
} else {
let strArray = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getStringArrayValue failed, error code: ${code}, message: ${message}.`);
}
```
### getStringArrayValue9+
getStringArrayValue(resource: Resource): Promise<Array<string>>
用户获取指定resource对象对应的字符串数组,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
**返回值:**
| 类型 | 说明 |
| ---------------------------------- | ------------------ |
| Promise<Array<string>> | resource对象对应的字符串数组。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
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.error("getStringArray promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getStringArrayValue failed, error code: ${code}, message: ${message}.`);
}
```
### getStringArrayByName9+
getStringArrayByName(resName: string, callback: AsyncCallback<Array<string>>): void
用户获取指定资源名称对应的字符串数组,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resName | string | 是 | 资源名称。 |
| callback | AsyncCallback<Array<string>> | 是 | 返回获取的字符串数组。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getStringArrayByName("test", (error: BusinessError, value: Array) => {
if (error != null) {
console.error("error is " + error);
} else {
let strArray = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getStringArrayByName failed, error code: ${code}, message: ${message}.`);
}
```
### getStringArrayByName9+
getStringArrayByName(resName: string): Promise<Array<string>>
用户获取指定资源名称对应的字符串数组,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称。 |
**返回值:**
| 类型 | 说明 |
| ---------------------------------- | ------------ |
| Promise<Array<string>> | 资源名称对应的字符串数组。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getStringArrayByName("test").then((value: Array) => {
let strArray = value;
}).catch((error: BusinessError) => {
console.error("getStringArrayByName promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getStringArrayByName failed, error code: ${code}, message: ${message}.`);
}
```
### getPluralStringValueSync10+
getPluralStringValueSync(resId: number, num: number): string
根据指定数量获取指定ID字符串表示的单复数字符串,使用同步方式返回。
>**说明**
>
> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
| num | number | 是 | 数量值。 |
**返回值:**
| 类型 | 说明 |
| -------- | ----------- |
| string | 根据指定数量获取指定ID字符串表示的单复数字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getPluralStringValueSync($r('app.plural.test').id, 1);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getPluralStringValueSync failed, error code: ${code}, message: ${message}.`);
}
```
### getPluralStringValueSync10+
getPluralStringValueSync(resource: Resource, num: number): string
根据指定数量获取指定resource对象表示的单复数字符串,使用同步方式返回。
>**说明**
>
> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| num | number | 是 | 数量值。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| string | 根据指定数量获取指定resource对象表示的单复数字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
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) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getPluralStringValueSync failed, error code: ${code}, message: ${message}.`);
}
```
### getPluralStringByNameSync10+
getPluralStringByNameSync(resName: string, num: number): string
根据指定数量获取指定资源名称表示的单复数字符串,使用同步方式返回。
>**说明**
>
> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resName | string | 是 | 资源名称。 |
| num | number | 是 | 数量值。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| string | 根据指定数量获取指定资源名称表示的单复数字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getPluralStringByNameSync("test", 1);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getPluralStringByNameSync failed, error code: ${code}, message: ${message}.`);
}
```
### getPluralStringValue9+
getPluralStringValue(resId: number, num: number, callback: AsyncCallback<string>): void
根据指定数量获取指定ID字符串表示的单复数字符串,使用callback异步回调。
>**说明**
>
> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------------- |
| resId | number | 是 | 资源ID值。 |
| num | number | 是 | 数量值。 |
| callback | AsyncCallback<string> | 是 | 根据指定数量,获取指定ID字符串表示的单复数字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1, (error: BusinessError, value: string) => {
if (error != null) {
console.error("error is " + error);
} else {
let str = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getPluralStringValue failed, error code: ${code}, message: ${message}.`);
}
```
### getPluralStringValue9+
getPluralStringValue(resId: number, num: number): Promise<string>
根据指定数量获取对指定ID字符串表示的单复数字符串,使用Promise异步回调。
>**说明**
>
> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
| num | number | 是 | 数量值。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------------- |
| Promise<string> | 根据提供的数量,获取对应ID字符串表示的单复数字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1).then((value: string) => {
let str = value;
}).catch((error: BusinessError) => {
console.error("getPluralStringValue promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getPluralStringValue failed, error code: ${code}, message: ${message}.`);
}
```
### getPluralStringValue9+
getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback<string>): void
根据指定数量获取指定resource对象表示的单复数字符串,使用callback异步回调。
>**说明**
>
> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| num | number | 是 | 数量值。 |
| callback | AsyncCallback<string> | 是 | 根据指定数量,获取指定resource对象表示的单复数字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.plural.test').id
};
try {
this.context.resourceManager.getPluralStringValue(resource, 1, (error: BusinessError, value: string) => {
if (error != null) {
console.error("error is " + error);
} else {
let str = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getPluralStringValue failed, error code: ${code}, message: ${message}.`);
}
```
### getPluralStringValue9+
getPluralStringValue(resource: Resource, num: number): Promise<string>
根据指定数量获取对指定resource对象表示的单复数字符串,使用Promise异步回调。
>**说明**
>
> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| num | number | 是 | 数量值。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------------------ |
| Promise<string> | 根据提供的数量,获取对应resource对象表示的单复数字符串。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
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.error("getPluralStringValue promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getPluralStringValue failed, error code: ${code}, message: ${message}.`);
}
```
### getPluralStringByName9+
getPluralStringByName(resName: string, num: number, callback: AsyncCallback<string>): void
根据传入的数量值,获取资源名称对应的字符串资源,使用callback异步回调。
>**说明**
>
> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ----------------------------- |
| resName | string | 是 | 资源名称。 |
| num | number | 是 | 数量值。 |
| callback | AsyncCallback<string> | 是 | 根据传入的数量值,获取资源名称对应的字符串资源。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getPluralStringByName("test", 1, (error: BusinessError, value: string) => {
if (error != null) {
console.error("error is " + error);
} else {
let str = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getPluralStringByName failed, error code: ${code}, message: ${message}.`);
}
```
### getPluralStringByName9+
getPluralStringByName(resName: string, num: number): Promise<string>
根据传入的数量值,获取资源名称对应的字符串资源,使用Promise异步回调。
>**说明**
>
> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称。 |
| num | number | 是 | 数量值。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ---------------------- |
| Promise<string> | 根据传入的数量值,获取资源名称对应的字符串资源。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getPluralStringByName("test", 1).then((value: string) => {
let str = value;
}).catch((error: BusinessError) => {
console.error("getPluralStringByName promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getPluralStringByName failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContentSync10+
getMediaContentSync(resId: number, density?: number): Uint8Array
用户获取指定资源ID对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 |
**返回值:**
| 类型 | 说明 |
| -------- | ----------- |
| Uint8Array | 资源ID对应的媒体文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaContentSync($r('app.media.test').id); // 默认屏幕密度
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`);
}
try {
this.context.resourceManager.getMediaContentSync($r('app.media.test').id, 120); // 指定屏幕密度
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContentSync10+
getMediaContentSync(resource: Resource, density?: number): Uint8Array
用户获取指定resource对象对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| Uint8Array | resource对象对应的媒体文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getMediaContentSync(resource); // 默认屏幕密度
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`);
}
try {
this.context.resourceManager.getMediaContentSync(resource, 120); // 指定屏幕密度
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaByNameSync10+
getMediaByNameSync(resName: string, density?: number): Uint8Array
用户获取指定资源名称对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resName | string | 是 | 资源名称。 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| Uint8Array | 对应资源名称的媒体文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaByNameSync("test"); // 默认屏幕密度
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getMediaByNameSync failed, error code: ${code}, message: ${message}.`);
}
try {
this.context.resourceManager.getMediaByNameSync("test", 120); // 指定屏幕密度
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getMediaByNameSync failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContent9+
getMediaContent(resId: number, callback: AsyncCallback<Uint8Array>): void
用户获取指定资源ID对应的媒体文件内容,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ |
| resId | number | 是 | 资源ID值。 |
| callback | AsyncCallback<Uint8Array> | 是 | 返回获取的媒体文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaContent($r('app.media.test').id, (error: BusinessError, value: Uint8Array) => {
if (error != null) {
console.error("error is " + error);
} else {
let media = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContent10+
getMediaContent(resId: number, density: number, callback: AsyncCallback<Uint8Array>): void
用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ |
| resId | number | 是 | 资源ID值。 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 |
| callback | AsyncCallback<Uint8Array> | 是 | 返回获取的媒体文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaContent($r('app.media.test').id, 120, (error: BusinessError, value: Uint8Array) => {
if (error != null) {
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let media = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContent9+
getMediaContent(resId: number): Promise<Uint8Array>
用户获取指定资源ID对应的媒体文件内容,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | -------------- |
| Promise<Uint8Array> | 资源ID值对应的媒体文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaContent($r('app.media.test').id).then((value: Uint8Array) => {
let media = value;
}).catch((error: BusinessError) => {
console.error("getMediaContent promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContent10+
getMediaContent(resId: number, density: number): Promise<Uint8Array>
用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | -------------- |
| Promise<Uint8Array> | 资源ID值对应的媒体文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
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) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContent9+
getMediaContent(resource: Resource, callback: AsyncCallback<Uint8Array>): void
用户获取指定resource对象对应的媒体文件内容,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| callback | AsyncCallback<Uint8Array> | 是 | 返回获取的媒体文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getMediaContent(resource, (error: BusinessError, value: Uint8Array) => {
if (error != null) {
console.error("error is " + error);
} else {
let media = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContent10+
getMediaContent(resource: Resource, density: number, callback: AsyncCallback<Uint8Array>): void
用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 |
| callback | AsyncCallback<Uint8Array> | 是 | 返回获取的媒体文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getMediaContent(resource, 120, (error: BusinessError, value: Uint8Array) => {
if (error != null) {
console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let media = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContent9+
getMediaContent(resource: Resource): Promise<Uint8Array>
用户获取指定resource对象对应的媒体文件内容,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ------------------- |
| Promise<Uint8Array> | resource对象对应的媒体文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
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.error("getMediaContent promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContent10+
getMediaContent(resource: Resource, density: number): Promise<Uint8Array>
用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ------------------- |
| Promise<Uint8Array> | resource对象对应的媒体文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
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) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaByName9+
getMediaByName(resName: string, callback: AsyncCallback<Uint8Array>): void
用户获取指定资源名称对应的媒体文件内容,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ |
| resName | string | 是 | 资源名称。 |
| callback | AsyncCallback<Uint8Array> | 是 | 返回获取的媒体文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaByName("test", (error: BusinessError, value: Uint8Array) => {
if (error != null) {
console.error("error is " + error);
} else {
let media = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getMediaByName failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaByName10+
getMediaByName(resName: string, density: number, callback: AsyncCallback<Uint8Array>): void
用户获取指定资源名称对应的指定屏幕密度媒体文件内容,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ------------------ |
| resName | string | 是 | 资源名称。 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 |
| callback | AsyncCallback<Uint8Array> | 是 | 返回获取的媒体文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaByName("test", 120, (error: BusinessError, value: Uint8Array) => {
if (error != null) {
console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let media = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getMediaByName failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaByName9+
getMediaByName(resName: string): Promise<Uint8Array>
用户获取指定资源名称对应的媒体文件内容,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称。 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ------------- |
| Promise<Uint8Array> | 资源名称对应的媒体文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaByName("test").then((value: Uint8Array) => {
let media = value;
}).catch((error: BusinessError) => {
console.error("getMediaByName promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getMediaByName failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaByName10+
getMediaByName(resName: string, density: number): Promise<Uint8Array>
用户获取指定资源名称对应的指定屏幕密度媒体文件内容,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称。 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ------------- |
| Promise<Uint8Array> | 资源名称对应的媒体文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
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) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getMediaByName failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContentBase64Sync10+
getMediaContentBase64Sync(resId: number, density?: number): string
用户获取指定资源ID对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 |
**返回值:**
| 类型 | 说明 |
| -------- | ----------- |
| string | 资源ID对应的图片资源Base64编码。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaContentBase64Sync($r('app.media.test').id); // 默认屏幕密度
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`);
}
try {
this.context.resourceManager.getMediaContentBase64Sync($r('app.media.test').id, 120); // 指定屏幕密度
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContentBase64Sync10+
getMediaContentBase64Sync(resource: Resource, density?: number): string
用户获取指定resource对象对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| string | resource对象对应的图片资源Base64编码。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getMediaContentBase64Sync(resource); // 默认屏幕密度
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`);
}
try {
this.context.resourceManager.getMediaContentBase64Sync(resource, 120); // 指定屏幕密度
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaBase64ByNameSync10+
getMediaBase64ByNameSync(resName: string, density?: number): string
用户获取指定资源名称对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resName | string | 是 | 资源名称。 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| string | 资源名称对应的图片资源Base64编码。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaBase64ByNameSync("test"); // 默认屏幕密度
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getMediaBase64ByNameSync failed, error code: ${code}, message: ${message}.`);
}
try {
this.context.resourceManager.getMediaBase64ByNameSync("test", 120); // 指定屏幕密度
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getMediaBase64ByNameSync failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContentBase649+
getMediaContentBase64(resId: number, callback: AsyncCallback<string>): void
用户获取指定资源ID对应的图片资源Base64编码,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:** Content
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ |
| resId | number | 是 | 资源ID值。 |
| callback | AsyncCallback<string> | 是 | 返回获取的图片资源Base64编码。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, (error: BusinessError, value: string) => {
if (error != null) {
console.error("error is " + error);
} else {
let media = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContentBase6410+
getMediaContentBase64(resId: number, density: number, callback: AsyncCallback<string>): void
用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ |
| resId | number | 是 | 资源ID值。 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 |
| callback | AsyncCallback<string> | 是 | 获取的图片资源Base64编码。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120, (error: BusinessError, value: string) => {
if (error != null) {
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let media = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContentBase649+
getMediaContentBase64(resId: number): Promise<string>
用户获取指定资源ID对应的图片资源Base64编码,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | -------------------- |
| Promise<string> | 资源ID值对应的图片资源Base64编码。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaContentBase64($r('app.media.test').id).then((value: string) => {
let media = value;
}).catch((error: BusinessError) => {
console.error("getMediaContentBase64 promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContentBase6410+
getMediaContentBase64(resId: number, density: number): Promise<string>
用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | -------------------- |
| Promise<string> | 资源ID值对应的图片资源Base64编码。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
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) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContentBase649+
getMediaContentBase64(resource: Resource, callback: AsyncCallback<string>): void
用户获取指定resource对象对应的图片资源Base64编码,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| callback | AsyncCallback<string> | 是 | 返回获取的图片资源Base64编码。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getMediaContentBase64(resource, (error: BusinessError, value: string) => {
if (error != null) {
console.error("error is " + error);
} else {
let media = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContentBase6410+
getMediaContentBase64(resource: Resource, density: number, callback: AsyncCallback<string>): void
用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 |
| callback | AsyncCallback<string> | 是 | 获取的图片资源Base64编码。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.test').id
};
try {
this.context.resourceManager.getMediaContentBase64(resource, 120, (error: BusinessError, value: string) => {
if (error != null) {
console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let media = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContentBase649+
getMediaContentBase64(resource: Resource): Promise<string>
用户获取指定resource对象对应的图片资源Base64编码,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------------- |
| Promise<string> | resource对象对应的图片资源Base64编码。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
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.error("getMediaContentBase64 promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaContentBase6410+
getMediaContentBase64(resource: Resource, density: number): Promise<string>
用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------------- |
| Promise<string> | resource对象对应的图片资源Base64编码。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
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) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaBase64ByName9+
getMediaBase64ByName(resName: string, callback: AsyncCallback<string>): void
用户获取指定资源名称对应的图片资源Base64编码,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ |
| resName | string | 是 | 资源名称。 |
| callback | AsyncCallback<string> | 是 | 返回获取的图片资源Base64编码。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaBase64ByName("test", (error: BusinessError, value: string) => {
if (error != null) {
console.error("error is " + error);
} else {
let media = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getMediaBase64ByName failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaBase64ByName10+
getMediaBase64ByName(resName: string, density: number, callback: AsyncCallback<string>): void
用户获取指定资源名称对应的指定屏幕密度图片资源Base64编码,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | ------------------------ |
| resName | string | 是 | 资源名称。 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 |
| callback | AsyncCallback<string> | 是 | 返回获取的图片资源Base64编码。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaBase64ByName("test", 120, (error: BusinessError, value: string) => {
if (error != null) {
console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let media = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getMediaBase64ByName failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaBase64ByName9+
getMediaBase64ByName(resName: string): Promise<string>
用户获取指定资源名称对应的图片资源Base64编码,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------- |
| Promise<string> | 资源名称对应的图片资源Base64编码。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getMediaBase64ByName("test").then((value: string) => {
let media = value;
}).catch((error: BusinessError) => {
console.error("getMediaBase64ByName promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getMediaBase64ByName failed, error code: ${code}, message: ${message}.`);
}
```
### getMediaBase64ByName10+
getMediaBase64ByName(resName: string, density: number): Promise<string>
用户获取指定资源名称对应的指定屏幕密度图片资源Base64编码,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称。 |
| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ------------------- |
| Promise<string> | 资源名称对应的图片资源Base64编码 。|
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
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) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getMediaBase64ByName failed, error code: ${code}, message: ${message}.`);
}
```
### getDrawableDescriptor10+
getDrawableDescriptor(resId: number, density?: number, type?: number): DrawableDescriptor
用户获取指定资源ID对应的DrawableDescriptor对象,用于图标的显示,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 |
| type11+ | number | 否 | 1表示用于取主题资源包中应用的分层图标资源,0或缺省表示取应用自身图标资源。 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------- |
| [DrawableDescriptor](../apis-arkui/js-apis-arkui-drawableDescriptor.md#drawabledescriptor) | 资源ID值对应的DrawableDescriptor对象。|
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
import { DrawableDescriptor } from '@kit.ArkUI';
try {
let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`);
}
try {
let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id, 120);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`);
}
try {
let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id, 0, 1);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`);
}
```
### getDrawableDescriptor10+
getDrawableDescriptor(resource: Resource, density?: number, type?: number): DrawableDescriptor
用户获取指定resource对应的DrawableDescriptor对象,用于图标的显示,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 |
| type11+ | number | 否 | 1表示用于取主题资源包中应用的分层图标资源,0或缺省表示取应用自身图标资源。 |
**返回值:**
| 类型 | 说明 |
| ------- | ----------------- |
| [DrawableDescriptor](../apis-arkui/js-apis-arkui-drawableDescriptor.md#drawabledescriptor) | 资源ID值对应的DrawableDescriptor对象。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
import { DrawableDescriptor } from '@kit.ArkUI';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.media.icon').id
};
try {
let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor(resource);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`);
}
try {
let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor(resource, 120);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`);
}
try {
let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor(resource, 0, 1);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`);
}
```
### getDrawableDescriptorByName10+
getDrawableDescriptorByName(resName: string, density?: number, type?: number): DrawableDescriptor
用户获取指定资源名称对应的DrawableDescriptor对象,用于图标的显示,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称。 |
| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 |
| type11+ | number | 否 | 1表示用于取主题资源包中应用的分层图标资源,0或缺省表示取应用自身图标资源。 |
**返回值:**
| 类型 | 说明 |
| ------ | --------- |
| [DrawableDescriptor](../apis-arkui/js-apis-arkui-drawableDescriptor.md#drawabledescriptor) | 资源ID值对应的DrawableDescriptor对象。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
import { DrawableDescriptor } from '@kit.ArkUI';
try {
let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptorByName('icon');
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getDrawableDescriptorByName failed, error code: ${code}, message: ${message}.`);
}
try {
let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptorByName('icon', 120);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getDrawableDescriptorByName failed, error code: ${code}, message: ${message}.`);
}
try {
let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptorByName('icon', 0, 1);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getDrawableDescriptorByName failed, error code: ${code}, message: ${message}.`);
}
```
### getBoolean9+
getBoolean(resId: number): boolean
使用同步方式,返回获取指定资源ID对应的布尔结果。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
**返回值:**
| 类型 | 说明 |
| ------- | ------------ |
| boolean | 资源ID值对应的布尔结果。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getBoolean($r('app.boolean.boolean_test').id);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getBoolean failed, error code: ${code}, message: ${message}.`);
}
```
### getBoolean9+
getBoolean(resource: Resource): boolean
使用同步方式,返回获取指定resource对象对应的布尔结果。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
**返回值:**
| 类型 | 说明 |
| ------- | ----------------- |
| boolean | resource对象对应的布尔结果。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
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) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getBoolean failed, error code: ${code}, message: ${message}.`);
}
```
### getBooleanByName9+
getBooleanByName(resName: string): boolean
使用同步方式,返回获取指定资源名称对应的布尔结果。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称。 |
**返回值:**
| 类型 | 说明 |
| ------- | ----------- |
| boolean | 资源名称对应的布尔结果。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getBooleanByName("boolean_test");
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getBooleanByName failed, error code: ${code}, message: ${message}.`);
}
```
### getNumber9+
getNumber(resId: number): number
用户获取指定资源ID对应的integer数值或者float数值,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------- |
| number | 资源ID值对应的数值。integer对应的是原数值,float对应的是真实像素点值,具体参考示例代码。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getNumber($r('app.integer.integer_test').id); // integer对应返回的是原数值
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getNumber failed, error code: ${code}, message: ${message}.`);
}
try {
this.context.resourceManager.getNumber($r('app.float.float_test').id); // float对应返回的是真实像素点值
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getNumber failed, error code: ${code}, message: ${message}.`);
}
```
### getNumber9+
getNumber(resource: Resource): number
用户获取指定resource对象对应的integer数值或者float数值,使用同步方式返回。
> **说明**
>
> 使用接口获取单位为"vp"的float值时,通过resId和resource对象获取到的值不一致,resId获取的值是准确的。该问题正在优化改进。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
**返回值:**
| 类型 | 说明 |
| ------ | --------------- |
| number | 资源名称对应的数值。
integer对应的是原数值,float不带单位时对应的是原数值,带"vp","fp"单位时对应的是px值。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.integer.integer_test').id
};
try {
this.context.resourceManager.getNumber(resource);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getNumber failed, error code: ${code}, message: ${message}.`);
}
```
### getNumberByName9+
getNumberByName(resName: string): number
用户获取指定资源名称对应的integer数值或者float数值,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称。 |
**返回值:**
| 类型 | 说明 |
| ------ | --------- |
| number | 资源名称对应的数值。
integer对应的是原数值,float不带单位时对应的是原数值,带"vp","fp"单位时对应的是px值。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getNumberByName("integer_test");
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getNumberByName failed, error code: ${code}, message: ${message}.`);
}
try {
this.context.resourceManager.getNumberByName("float_test");
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getNumberByName failed, error code: ${code}, message: ${message}.`);
}
```
### getColorSync10+
getColorSync(resId: number) : number;
用户获取指定资源ID对应的颜色值,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
**返回值:**
| 类型 | 说明 |
| ------ | ----------- |
| number | 资源ID值对应的颜色值(十进制)。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getColorSync($r('app.color.test').id);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getColorSync failed, error code: ${code}, message: ${message}.`);
}
```
### getColorSync10+
getColorSync(resource: Resource): number
用户获取指定resource对象对应的颜色值,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------------- |
| number | resource对象对应的颜色值(十进制)。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.color.test').id
};
try {
this.context.resourceManager.getColorSync(resource);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getColorSync failed, error code: ${code}, message: ${message}.`);
}
```
### getColorByNameSync10+
getColorByNameSync(resName: string) : number;
用户获取指定资源名称对应的颜色值,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称。 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------- |
| number | 资源名称对应的颜色值(十进制)。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getColorByNameSync("test");
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getColorByNameSync failed, error code: ${code}, message: ${message}.`);
}
```
### getColor10+
getColor(resId: number, callback: AsyncCallback<number>): void;
用户获取指定资源ID对应的颜色值,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| resId | number | 是 | 资源ID值。 |
| callback | AsyncCallback<number> | 是 | 返回获取的颜色值(十进制)。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | If the module resId invalid. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例Stage:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getColor($r('app.color.test').id, (error: BusinessError, value: number) => {
if (error != null) {
console.error("error is " + error);
} else {
let str = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getColor failed, error code: ${code}, message: ${message}.`);
}
```
### getColor10+
getColor(resId: number): Promise<number>
用户获取指定资源ID对应的颜色值,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ----------- |
| Promise<number> | 资源ID值对应的颜色值(十进制)。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getColor($r('app.color.test').id).then((value: number) => {
let str = value;
}).catch((error: BusinessError) => {
console.error("getColor promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getColor failed, error code: ${code}, message: ${message}.`);
}
```
### getColor10+
getColor(resource: Resource, callback: AsyncCallback<number>): void;
用户获取指定resource对象对应的颜色值,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
| callback | AsyncCallback<number> | 是 | 返回获取的颜色值(十进制)。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.color.test').id
};
try {
this.context.resourceManager.getColor(resource, (error: BusinessError, value: number) => {
if (error != null) {
console.error("error is " + error);
} else {
let str = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getColor failed, error code: ${code}, message: ${message}.`);
}
```
### getColor10+
getColor(resource: Resource): Promise<number>;
用户获取指定resource对象对应的颜色值,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ---------------- |
| Promise<number> | resource对象对应的颜色值(十进制)。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
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.error("getColor promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getColor failed, error code: ${code}, message: ${message}.`);
}
```
### getColorByName10+
getColorByName(resName: string, callback: AsyncCallback<number>): void
用户获取指定资源名称对应的颜色值,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | --------------------------- | ---- | --------------- |
| resName | string | 是 | 资源名称。 |
| callback | AsyncCallback<number> | 是 | 异步回调,用于返回获取的颜色值(十进制)。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getColorByName("test", (error: BusinessError, value: number) => {
if (error != null) {
console.error("error is " + error);
} else {
let string = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getColorByName failed, error code: ${code}, message: ${message}.`);
}
```
### getColorByName10+
getColorByName(resName: string): Promise<number>
用户获取指定资源名称对应的颜色值,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ---------- |
| Promise<number> | 资源名称对应的颜色值(十进制)。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getColorByName("test").then((value: number) => {
let string = value;
}).catch((error: BusinessError) => {
console.error("getColorByName promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getColorByName failed, error code: ${code}, message: ${message}.`);
}
```
### getRawFileContentSync10+
getRawFileContentSync(path: string): Uint8Array
用户获取resources/rawfile目录下对应的rawfile文件内容,使用同步形式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ----------------------- |
| path | string | 是 | rawfile文件路径。 |
**返回值:**
| 类型 | 说明 |
| --------------------- | ---------- |
| Uint8Array | 返回获取的rawfile文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001005 | Invalid relative path. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getRawFileContentSync("test.txt");
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getRawFileContentSync failed, error code: ${code}, message: ${message}.`);
}
```
### getRawFileContent9+
getRawFileContent(path: string, callback: AsyncCallback<Uint8Array>): void
用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ----------------------- |
| path | string | 是 | rawfile文件路径。 |
| callback | AsyncCallback<Uint8Array> | 是 | 返回获取的rawfile文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getRawFileContent("test.txt", (error: BusinessError, value: Uint8Array) => {
if (error != null) {
console.error("error is " + error);
} else {
let rawFile = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getRawFileContent failed, error code: ${code}, message: ${message}.`);
}
```
### getRawFileContent9+
getRawFileContent(path: string): Promise<Uint8Array>
用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件路径。 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ----------- |
| Promise<Uint8Array> | rawfile文件内容。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001005 | Invalid relative path. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getRawFileContent("test.txt").then((value: Uint8Array) => {
let rawFile = value;
}).catch((error: BusinessError) => {
console.error("getRawFileContent promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getRawFileContent failed, error code: ${code}, message: ${message}.`);
}
```
### getRawFileListSync10+
getRawFileListSync(path: string): Array\
用户获取resources/rawfile目录下文件夹及文件列表,使用同步形式返回。
>**说明**
>
> 若文件夹中无文件,则不返回;若文件夹中有文件,则返回文件夹及文件列表。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ----------------------- |
| path | string | 是 | rawfile文件夹路径。 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ----------- |
| Array\ | rawfile文件目录下的文件夹及文件列表。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001005 | Invalid relative path. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try { // 传入""表示获取rawfile根目录下的文件列表
this.context.resourceManager.getRawFileListSync("")
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getRawFileListSync failed, error code: ${code}, message: ${message}.`);
}
```
### getRawFileList10+
getRawFileList(path: string, callback: AsyncCallback<Array\>): void;
用户获取resources/rawfile目录下文件夹及文件列表,使用callback异步回调。
>**说明**
>
> 若文件夹中无文件,则不返回;若文件夹中有文件,则返回文件夹及文件列表。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------- | ---- | ----------------------- |
| path | string | 是 | rawfile文件夹路径。 |
| callback | AsyncCallback<Array\> | 是 | rawfile文件目录下的文件夹及文件列表。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001005 | Invalid relative path. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try { // 传入""表示获取rawfile根目录下的文件列表
this.context.resourceManager.getRawFileList("", (error: BusinessError, value: Array) => {
if (error != null) {
console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`);
} else {
let rawFile = value;
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getRawFileList failed, error code: ${code}, message: ${message}.`);
}
```
### getRawFileList10+
getRawFileList(path: string): Promise<Array\>
用户获取resources/rawfile目录下文件夹及文件列表,使用Promise异步回调。
>**说明**
>
> 若文件夹中无文件,则不返回;若文件夹中有文件,则返回文件夹及文件列表。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件夹路径。 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ----------- |
| Promise<Array\> | rawfile文件目录下的文件夹及文件列表。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001005 | Invalid relative path. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
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) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getRawFileList failed, error code: ${code}, message: ${message}.`);
}
```
### getRawFdSync10+
getRawFdSync(path: string): RawFileDescriptor
用户获取resources/rawfile目录下rawfile文件所在hap的descriptor信息。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | -------------------------------- |
| path | string | 是 | rawfile文件路径。 |
**返回值:**
| 类型 | 说明 |
| ------------------------- | ----------- |
| [RawFileDescriptor](#rawfiledescriptor8) | rawfile文件所在hap的descriptor信息。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001005 | Invalid relative path. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getRawFdSync("test.txt");
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getRawFdSync failed, error code: ${code}, message: ${message}.`);
}
```
### getRawFd9+
getRawFd(path: string, callback: AsyncCallback<RawFileDescriptor>): void
用户获取resources/rawfile目录下对应rawfile文件所在hap的descriptor信息,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | -------------------------------- |
| path | string | 是 | rawfile文件路径。 |
| callback | AsyncCallback<[RawFileDescriptor](#rawfiledescriptor8)> | 是 | 返回获取的rawfile文件所在hap的descriptor信息。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001005 | Invalid relative path. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
import { resourceManager } from '@kit.LocalizationKit'
try {
this.context.resourceManager.getRawFd("test.txt", (error: BusinessError, value: resourceManager.RawFileDescriptor) => {
if (error != null) {
console.error(`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) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getRawFd failed, error code: ${code}, message: ${message}.`);
}
```
### getRawFd9+
getRawFd(path: string): Promise<RawFileDescriptor>
用户获取resources/rawfile目录下rawfile文件所在hap的descriptor信息,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件路径。 |
**返回值:**
| 类型 | 说明 |
| ---------------------------------------- | ------------------- |
| Promise<[RawFileDescriptor](#rawfiledescriptor8)> | rawfile文件所在hap的descriptor信息。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001005 | Invalid relative path. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
import { resourceManager } from '@kit.LocalizationKit'
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.error(`promise getRawFd error error code: ${error.code}, message: ${error.message}.`);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getRawFd failed, error code: ${code}, message: ${message}.`);
}
```
### closeRawFdSync10+
closeRawFdSync(path: string): void
用户关闭resources/rawfile目录下rawfile文件所在hap的descriptor信息。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ----------- |
| path | string | 是 | rawfile文件路径 。|
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001005 | The resource not found by path. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.closeRawFdSync("test.txt");
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`closeRawFd failed, error code: ${code}, message: ${message}.`);
}
```
### closeRawFd9+
closeRawFd(path: string, callback: AsyncCallback<void>): void
用户关闭resources/rawfile目录下rawfile文件所在hap的descriptor信息,使用callback异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------- | ---- | ----------- |
| path | string | 是 | rawfile文件路径。 |
| callback | AsyncCallback<void> | 是 | 异步回调。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001005 | The resource not found by path. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.closeRawFd("test.txt", (error: BusinessError) => {
if (error != null) {
console.error("error is " + error);
}
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback closeRawFd failed, error code: ${code}, message: ${message}.`);
}
```
### closeRawFd9+
closeRawFd(path: string): Promise<void>
用户关闭resources/rawfile目录下rawfile文件所在hap的descriptor信息,使用Promise异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ---- | ------ | ---- | ----------- |
| path | string | 是 | rawfile文件路径。 |
**返回值:**
| 类型 | 说明 |
| ------------------- | ---- |
| Promise<void> | 无返回结果的promise对象。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001005 | Invalid relative path. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.closeRawFd("test.txt");
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise closeRawFd failed, error code: ${code}, message: ${message}.`);
}
```
### getConfigurationSync10+
getConfigurationSync(): Configuration
用户获取设备的Configuration,使用同步形式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**返回值:**
| 类型 | 说明 |
| ---------------------------------------- | ---------------- |
| [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异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ------------------------- |
| callback | AsyncCallback<[Configuration](#configuration)> | 是 | 返回设备的Configuration。 |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
try {
this.context.resourceManager.getConfiguration((error: BusinessError, value: resourceManager.Configuration) => {
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异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**返回值:**
| 类型 | 说明 |
| ---------------------------------------- | ---------------- |
| Promise<[Configuration](#configuration)> | 设备的Configuration。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
import { resourceManager } from '@kit.LocalizationKit'
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);
}
```
### getDeviceCapabilitySync10+
getDeviceCapabilitySync(): DeviceCapability
用户获取设备的DeviceCapability,使用同步形式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**返回值:**
| 类型 | 说明 |
| ---------------------------------------- | ------------------- |
| [DeviceCapability](#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异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ---------------------------- |
| callback | AsyncCallback<[DeviceCapability](#devicecapability)> | 是 | 返回设备的DeviceCapability。 |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
try {
this.context.resourceManager.getDeviceCapability((error: BusinessError, value: resourceManager.DeviceCapability) => {
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异步回调。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**返回值:**
| 类型 | 说明 |
| ---------------------------------------- | ------------------- |
| Promise<[DeviceCapability](#devicecapability)> | 设备的DeviceCapability。 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
import { resourceManager } from '@kit.LocalizationKit'
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);
}
```
### addResource10+
addResource(path: string) : void
应用运行时,加载指定的资源路径,实现资源覆盖。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| path | string | 是 | 资源路径。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001010 | Invalid overlay path. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
let path = getContext().bundleCodeDir + "/library1-default-signed.hsp";
try {
this.context.resourceManager.addResource(path);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`addResource failed, error code: ${code}, message: ${message}.`);
}
```
### removeResource10+
removeResource(path: string) : void
用户运行时,移除指定的资源路径,还原被覆盖前的资源。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| path | string | 是 | 资源路径。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001010 | Invalid overlay path. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
let path = getContext().bundleCodeDir + "/library1-default-signed.hsp";
try {
this.context.resourceManager.removeResource(path);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`removeResource failed, error code: ${code}, message: ${message}.`);
}
```
### getLocales11+
getLocales(includeSystem?: boolean): Array\
获取应用的语言列表。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------------- | ------- | ------ | -------------------- |
| includeSystem | boolean | 否 | 是否包含系统资源,默认值为false。
false:表示仅获取应用资源的语言列表。
true:表示获取系统资源和应用资源的语言列表。
当系统资源管理对象获取语言列表时,includeSystem值无效,返回获取系统资源语言列表 。|
**返回值:**
| 类型 | 说明 |
| ------------------------- | ----------- |
| Array\ | 返回获取的语言列表,列表中的字符串由语言、脚本(可选)、地区(可选),按照顺序使用中划线"-"连接组成。|
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getLocales(); // 仅获取应用资源语言列表
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getLocales failed, error code: ${code}, message: ${message}.`);
}
try {
resourceManager.getSystemResourceManager().getLocales(); // 仅获取系统资源语言列表
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getLocales failed, error code: ${code}, message: ${message}.`);
}
try {
this.context.resourceManager.getLocales(true); // 获取应用资源和系统资源语言列表
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getLocales failed, error code: ${code}, message: ${message}.`);
}
```
### getSymbol11+
getSymbol(resId: number):number
用户获取指定资源ID对应的符号值,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ----- | ------ | ---- | ----- |
| resId | number | 是 | 资源ID值。 |
**返回值:**
| 类型 | 说明 |
| ------ | ----------- |
| number | 资源ID值对应的符号值(十进制)。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getSymbol($r('app.symbol.test').id);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getSymbol failed, error code: ${code}, message: ${message}.`);
}
```
### getSymbol11+
getSymbol(resource: Resource): number
用户获取指定resource对象对应的符号值,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**模型约束**:此接口仅可在Stage模型下使用。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | ---- |
| resource | [Resource](#resource9) | 是 | 资源信息。 |
**返回值:**
| 类型 | 说明 |
| ------ | ----------- |
| number | resource对象对应的符号值(十进制)。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001001 | Invalid resource ID. |
| 9001002 | No matching resource is found based on the resource ID. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { resourceManager } from '@kit.LocalizationKit'
import { BusinessError } from '@kit.BasicServicesKit';
let resource: resourceManager.Resource = {
bundleName: "com.example.myapplication",
moduleName: "entry",
id: $r('app.symbol.test').id
};
try {
this.context.resourceManager.getSymbol(resource);
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getSymbol failed, error code: ${code}, message: ${message}.`);
}
```
### getSymbolByName11+
getSymbolByName(resName: string) : number;
用户获取指定资源名称对应的符号值,使用同步方式返回。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| resName | string | 是 | 资源名称。 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------- |
| number | 资源名称对应的符号值(十进制)。 |
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001003 | Invalid resource name. |
| 9001004 | No matching resource is found based on the resource name. |
| 9001006 | The resource is referenced cyclically. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getSymbolByName("test");
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getSymbolByName failed, error code: ${code}, message: ${message}.`);
}
```
### isRawDir12+
isRawDir(path: string) : bool
用户判断指定路径是否是rawfile下的目录,使用同步方式返回。
**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------ | ---- | ---- |
| path | string | 是 | rawfile路径。 |
**返回值:**
| 类型 | 说明 |
| ------ | ---------- |
| bool |是否是rawfile下的目录。
true:表示是rawfile下的目录
false:表示不是rawfile下的目录|
**错误码:**
以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
| 9001005 | Invalid relative path. |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.isRawDir("test.txt");
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`isRawDir failed, error code: ${code}, message: ${message}.`);
}
```
### getOverrideResourceManager12+
getOverrideResourceManager(configuration?: Configuration) : ResourceManager
获取可以加载差异化资源的资源管理对象,使用同步方式返回。
普通的资源管理对象获取的资源的样式(语言、深浅色、分辨率、横竖屏等)是由系统决定的,而通过该接口返回的对象,应用可以获取符合指定配置的资源,即差异化资源,比如浅色模式时可以获取深色资源。
**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | ------------------------------- | ---- | ------------------------------------------------------------ |
| configuration | [Configuration](#configuration) | 否 | 指定想要获取的资源样式。
通过[getOverrideConfiguration](#getoverrideconfiguration12)获取差异化配置后,根据需求修改配置项,再作为参数传入该函数。
若缺省则获取与当前系统最匹配的资源。 |
**返回值:**
| 类型 | 说明 |
| --------------- | ---------------------------------- |
| ResourceManager | 可以加载差异化资源的资源管理对象。 |
**错误码:**
以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------------ |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
import { resourceManager } from '@kit.LocalizationKit'
try {
let resMgr = this.context.resourceManager
let overrideConfig = resMgr.getOverrideConfiguration()
overrideConfig.colorMode = resourceManager.ColorMode.DARK
let overrideResMgr = resMgr.getOverrideResourceManager(overrideConfig)
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getOverrideResourceManager failed, error code: ${code}, message: ${message}.`);
}
```
### getOverrideConfiguration12+
getOverrideConfiguration() : Configuration
获取差异化资源的配置,使用同步方式返回。普通资源管理对象与通过它的[getOverrideResourceManager](#getoverrideresourcemanager12)接口获取的差异化资源管理对象调用该方法可获得相同的返回值。
**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**返回值:**
| 类型 | 说明 |
| ------------------------------- | ---------------- |
| [Configuration](#configuration) | 差异化资源的配置 |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
import { resourceManager } from '@kit.LocalizationKit'
let overrideConfig = this.context.resourceManager.getOverrideConfiguration()
```
### updateOverrideConfiguration12+
updateOverrideConfiguration(configuration: Configuration) : void
更新差异化资源配置。普通资源管理对象与通过它的[getOverrideResourceManager](#getoverrideresourcemanager12)接口获取的差异化资源管理对象调用该方法均可更新差异化资源管理对象的配置。
**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------------- | ------------------------------- | ---- | ------------------------------------------------------------ |
| configuration | [Configuration](#configuration) | 是 | 指定差异化资源的配置项。通过[getOverrideConfiguration](#getoverrideconfiguration12)获取差异化配置后,根据需求修改配置项,再作为参数传入。 |
**错误码:**
以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------------ |
| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types |
**示例:**
```ts
import { BusinessError } from '@kit.BasicServicesKit';
import { resourceManager } from '@kit.LocalizationKit'
try {
let resMgr = this.context.resourceManager
let overrideConfig = resMgr.getOverrideConfiguration()
overrideConfig.colorMode = resourceManager.ColorMode.DARK
let overrideResMgr = resMgr.updateOverrideConfiguration(overrideConfig)
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`updateOverrideConfiguration failed, error code: ${code}, message: ${message}.`);
}
```
### release(deprecated)
release()
用户释放创建的resourceManager, 此接口暂不支持。
从API version 7开始支持,API version 12开始不再维护。
**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。
**系统能力**:SystemCapability.Global.ResourceManager
**示例:**
```ts
try {
this.context.resourceManager.release();
} catch (error) {
console.error("release error is " + error);
}
```
### 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: Error, value: string) => {
if (error != null) {
console.error("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 '@kit.BasicServicesKit';
resourceManager.getResourceManager((error, mgr) => {
mgr.getString($r('app.string.test').id).then((value: string) => {
let str = value;
}).catch((error: BusinessError) => {
console.error("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: Error, value: Array) => {
if (error != null) {
console.error("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 '@kit.BasicServicesKit';
resourceManager.getResourceManager((error, mgr) => {
mgr.getStringArray($r('app.strarray.test').id).then((value: Array) => {
let strArray = value;
}).catch((error: BusinessError) => {
console.error("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: Error, value: Uint8Array) => {
if (error != null) {
console.error("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 '@kit.BasicServicesKit';
resourceManager.getResourceManager((error, mgr) => {
mgr.getMedia($r('app.media.test').id).then((value: Uint8Array) => {
let media = value;
}).catch((error: BusinessError) => {
console.error("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: Error, value: string) => {
if (error != null) {
console.error("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 '@kit.BasicServicesKit';
resourceManager.getResourceManager((error, mgr) => {
mgr.getMediaBase64($r('app.media.test').id).then((value: string) => {
let media = value;
}).catch((error: BusinessError) => {
console.error("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 '@kit.BasicServicesKit';
resourceManager.getResourceManager((error, mgr) => {
mgr.getPluralString($r("app.plural.test").id, 1).then((value: string) => {
let str = value;
}).catch((error: BusinessError) => {
console.error("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: Error, value: string) => {
if (error != null) {
console.error("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: Error, value: Uint8Array) => {
if (error != null) {
console.error("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 '@kit.BasicServicesKit';
resourceManager.getResourceManager((error, mgr) => {
mgr.getRawFile("test.txt").then((value: Uint8Array) => {
let rawFile = value;
}).catch((error: BusinessError) => {
console.error("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
import { resourceManager } from '@kit.LocalizationKit'
resourceManager.getResourceManager((error, mgr) => {
mgr.getRawFileDescriptor("test.txt", (error: Error, value: resourceManager.RawFileDescriptor) => {
if (error != null) {
console.error("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 '@kit.BasicServicesKit';
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.error("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: Error) => {
if (error != null) {
console.error("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");
});
```
### 附录
- 示例代码中用到的'app.string.test'文件内容如下:
```json
{
"string": [
{
"name": "test",
"value": "10"
}
]
}
```
```json
{
"string": [
{
"name": "test",
"value": "%s %d %f"
}
]
}
```
- 示例代码中用到的'app.strarray.test'文件内容如下:
```json
{
"strarray": [
{
"name": "test",
"value": [
{
"value": "strarray_test"
}
]
}
]
}
```
- 示例代码中用到的'app.plural.test'文件内容如下:
```json
{
"plural": [
{
"name": "test",
"value": [
{
"quantity": "one",
"value": "%d apple"
},
{
"quantity": "other",
"value": "%d apples"
}
]
}
]
}
```
- 示例代码中用到的'app.boolean.boolean_test'文件内容如下:
```json
{
"boolean": [
{
"name": "boolean_test",
"value": true
}
}
```
- 示例代码中用到的"integer_test"和"float_test"文件内容如下:
```json
{
"integer": [
{
"name": "integer_test",
"value": 100
}
]
}
```
```json
{
"float": [
{
"name": "float_test",
"value": "30.6"
}
]
}
```
- 示例代码中用到的'app.color.test'文件内容如下:
```json
{
"color": [
{
"name": "test",
"value": "#FFFFFF"
}
]
}
```