1# @ohos.resourceManager (资源管理) 2 3<!--Kit: Localization Kit--> 4<!--Subsystem: Global--> 5<!--Owner: @liule_123--> 6<!--Designer: @buda_wy--> 7<!--Tester: @lpw_work--> 8<!--Adviser: @Brilliantry_Rui--> 9 10本模块提供资源获取能力。根据当前的[Configuration](#configuration)配置,获取最匹配的应用资源或系统资源。具体匹配规则参考[资源匹配](../../quick-start/resource-categories-and-access.md#资源匹配)。 11 12Configuration配置包括语言、区域、横竖屏、Mcc(移动国家码)和Mnc(移动网络码)、Device capability(设备类型)、Density(分辨率)。 13 14> **说明:** 15> 16> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 17 18## 导入模块 19 20```js 21import { resourceManager } from '@kit.LocalizationKit'; 22``` 23 24## 使用说明 25 26从API version 9开始,Stage模型支持通过Context获取资源管理resourceManager对象,无需再导入模块。 27FA模型仍需要先导入模块,再调用[getResourceManager](#resourcemanagergetresourcemanager)接口获取资源管理对象。 28Stage模型下Context的引用方法请参考[Stage模型的Context详细介绍](../../application-models/application-context-stage.md)。 29 30```ts 31import { UIAbility } from '@kit.AbilityKit'; 32import { window } from '@kit.ArkUI'; 33 34export default class EntryAbility extends UIAbility { 35 onWindowStageCreate(windowStage: window.WindowStage) { 36 let context = this.context; 37 let resourceManager = context.resourceManager; 38 } 39} 40``` 41 42## resourceManager.getResourceManager 43 44getResourceManager(callback: AsyncCallback<ResourceManager>): void 45 46获取当前应用的资源管理对象,使用callback异步回调。 47 48**系统能力:** SystemCapability.Global.ResourceManager 49 50**模型约束:** 此接口仅可在FA模型下使用。 51 52**参数:** 53 54| 参数名 | 类型 | 必填 | 说明 | 55| -------- | ---------------------------------------- | ---- | ----------------------------- | 56| callback | [AsyncCallback](#asynccallbackdeprecated)<[ResourceManager](#resourcemanager)> | 是 | 回调函数,返回资源管理ResourceManager对象。 | 57 58**示例:** 59 60 <!--code_no_check_fa--> 61 ```js 62 import { resourceManager } from '@kit.LocalizationKit'; 63 import { BusinessError } from '@kit.BasicServicesKit'; 64 65 resourceManager.getResourceManager((error, mgr) => { 66 if (error != null) { 67 console.error("error is " + error); 68 return; 69 } 70 // 'app.string.test'仅作示例,请替换为实际使用的资源 71 mgr.getStringValue($r('app.string.test').id, (error: BusinessError, value: string) => { 72 if (error != null) { 73 console.error("error is " + error); 74 } else { 75 let str = value; 76 } 77 }); 78 }); 79 ``` 80 81## resourceManager.getResourceManager 82 83getResourceManager(bundleName: string, callback: AsyncCallback<ResourceManager>): void 84 85获取指定应用的资源管理对象,使用callback异步回调。 86 87**系统能力:** SystemCapability.Global.ResourceManager 88 89**模型约束:** 此接口仅可在FA模型下使用。 90 91**参数:** 92 93| 参数名 | 类型 | 必填 | 说明 | 94| ---------- | ---------------------------------------- | ---- | ----------------------------- | 95| bundleName | string | 是 | 应用包名。 | 96| callback | [AsyncCallback](#asynccallbackdeprecated)<[ResourceManager](#resourcemanager)> | 是 | 回调函数,返回应用包名对应的资源管理ResourceManager对象。 | 97 98**示例:** 99 <!--code_no_check_fa--> 100 ```js 101 import { resourceManager } from '@kit.LocalizationKit'; 102 import { BusinessError } from '@kit.BasicServicesKit'; 103 104 // "com.example.myapplication"仅作示例,请替换为实际使用的包名 105 resourceManager.getResourceManager("com.example.myapplication", (error, mgr) => { 106 if (error != null) { 107 console.error("error is " + error); 108 return; 109 } 110 }); 111 ``` 112 113## resourceManager.getResourceManager 114 115getResourceManager(): Promise<ResourceManager> 116 117获取当前应用的资源管理对象,使用Promise异步回调。 118 119**系统能力:** SystemCapability.Global.ResourceManager 120 121**模型约束:** 此接口仅可在FA模型下使用。 122 123**返回值:** 124 125| 类型 | 说明 | 126| ---------------------------------------- | ----------------- | 127| Promise<[ResourceManager](#resourcemanager)> | Promise对象,返回资源管理ResourceManager对象。 | 128 129**示例:** 130 <!--code_no_check_fa--> 131 ```js 132 import { resourceManager } from '@kit.LocalizationKit'; 133 import { BusinessError } from '@kit.BasicServicesKit'; 134 135 resourceManager.getResourceManager().then((mgr: resourceManager.ResourceManager) => { 136 // 'app.string.test'仅作示例,请替换为实际使用的资源 137 mgr.getStringValue($r('app.string.test').id, (error: BusinessError, value: string) => { 138 if (error != null) { 139 console.error("error is " + error); 140 } else { 141 let str = value; 142 } 143 }); 144 }).catch((error: BusinessError) => { 145 console.error("error is " + error); 146 }); 147 ``` 148 149## resourceManager.getResourceManager 150 151getResourceManager(bundleName: string): Promise<ResourceManager> 152 153获取指定应用的资源管理对象,使用Promise异步回调。 154 155**系统能力:** SystemCapability.Global.ResourceManager 156 157**模型约束:** 此接口仅可在FA模型下使用。 158 159**参数:** 160 161| 参数名 | 类型 | 必填 | 说明 | 162| ---------- | ------ | ---- | ------------- | 163| bundleName | string | 是 | 应用包名。 | 164 165**返回值:** 166 167| 类型 | 说明 | 168| ---------------------------------------- | ------------------ | 169| Promise<[ResourceManager](#resourcemanager)> | Promise对象,返回应用包名对应的资源管理ResourceManager对象。 | 170 171**示例:** 172 <!--code_no_check_fa--> 173 ```js 174 import { resourceManager } from '@kit.LocalizationKit'; 175 import { BusinessError } from '@kit.BasicServicesKit'; 176 177 // "com.example.myapplication"仅作示例,请替换为实际使用的包名 178 resourceManager.getResourceManager("com.example.myapplication").then((mgr: resourceManager.ResourceManager) => { 179 }).catch((error: BusinessError) => { 180 console.error("error is " + error); 181 }); 182 ``` 183 184## resourceManager.getSysResourceManager<sup>20+</sup> 185 186getSysResourceManager(): ResourceManager 187 188获取系统资源管理对象。 189 190**原子化服务API:** 从API version 20开始,该接口支持在原子化服务中使用。 191 192**系统能力:** SystemCapability.Global.ResourceManager 193 194**返回值:** 195 196| 类型 | 说明 | 197| ---------------------------------------- | ------------------ | 198| [ResourceManager](#resourcemanager) | 系统资源管理对象。 | 199 200**错误码:** 201 202以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 203 204| 错误码ID | 错误信息 | 205| -------- | ---------------------------------------- | 206| 9001009 | Failed to access the system resource. which is not mapped to application sandbox, This error code will be thrown. | 207 208**示例:** 209 ```js 210import { resourceManager } from '@kit.LocalizationKit'; 211import { BusinessError } from '@kit.BasicServicesKit'; 212 213 try { 214 let systemResourceManager = resourceManager.getSysResourceManager(); 215 // 'sys.string.ohos_lab_vibrate'仅作示例,请替换为实际使用的资源 216 systemResourceManager.getStringValue($r('sys.string.ohos_lab_vibrate').id).then((value: string) => { 217 let str = value; 218 }).catch((error: BusinessError) => { 219 console.error(`systemResourceManager getStringValue promise error: ${error}`); 220 }); 221 } catch (error) { 222 let code = (error as BusinessError).code; 223 let message = (error as BusinessError).message; 224 console.error(`getSysResourceManager failed, error code: ${code}, message: ${message}.`); 225 } 226 ``` 227 228## Direction 229 230用于表示设备屏幕方向。 231 232**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 233 234**系统能力:** SystemCapability.Global.ResourceManager 235 236| 名称 | 值 | 说明 | 237| -------------------- | ---- | ---- | 238| DIRECTION_VERTICAL | 0 | 竖屏。 | 239| DIRECTION_HORIZONTAL | 1 | 横屏。 | 240 241 242## DeviceType 243 244用于表示当前设备类型。 245 246**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 247 248**系统能力:** SystemCapability.Global.ResourceManager 249<!--RP1--> 250| 名称 | 值 | 说明 | 251| -------------------- | ---- | ---- | 252| DEVICE_TYPE_PHONE | 0x00 | 手机。 | 253| DEVICE_TYPE_TABLET | 0x01 | 平板。 | 254| DEVICE_TYPE_CAR | 0x02 | 车机。 | 255| DEVICE_TYPE_TV | 0x04 | 智慧屏。 | 256| DEVICE_TYPE_WEARABLE | 0x06 | 智能手表。 | 257| DEVICE_TYPE_2IN1<sup>11+</sup> | 0x07 | PC/2in1。 | 258<!--RP1End--> 259 260## ScreenDensity 261 262用于表示当前设备屏幕密度。 263 264**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 265 266**系统能力:** SystemCapability.Global.ResourceManager 267 268| 名称 | 值 | 说明 | 269| -------------- | ---- | ---------- | 270| SCREEN_SDPI | 120 | 低屏幕密度。 | 271| SCREEN_MDPI | 160 | 中屏幕密度。 | 272| SCREEN_LDPI | 240 | 高屏幕密度。 | 273| SCREEN_XLDPI | 320 | 特高屏幕密度。 | 274| SCREEN_XXLDPI | 480 | 超高屏幕密度。 | 275| SCREEN_XXXLDPI | 640 | 超特高屏幕密度。 | 276 277 278## ColorMode<sup>12+</sup> 279 280用于表示当前设备颜色模式。 281 282**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 283 284**系统能力:** SystemCapability.Global.ResourceManager 285 286| 名称 | 值 | 说明 | 287| ----- | ---- | ---------- | 288| DARK | 0 | 深色模式。 | 289| LIGHT | 1 | 浅色模式。 | 290 291 292## Configuration 293 294表示当前设备的状态。 295 296**系统能力:** SystemCapability.Global.ResourceManager 297 298| 名称 | 类型 | 只读 | 可选 | 说明 | 299| --------------------------- | ------------------------------- | ---- | ---- | ------------------ | 300| direction | [Direction](#direction) | 否 | 否 | 屏幕方向。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 301| locale | string | 否 | 否 | 语言文字国家地区。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 302| deviceType<sup>12+</sup> | [DeviceType](#devicetype) | 否 | 否 | 设备类型。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 303| screenDensity<sup>12+</sup> | [ScreenDensity](#screendensity) | 否 | 否 | 屏幕密度。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 304| colorMode<sup>12+</sup> | [ColorMode](#colormode12) | 否 | 否 | 颜色模式。 <br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 305| mcc<sup>12+</sup> | number | 否 | 否 | 移动国家码。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 306| mnc<sup>12+</sup> | number | 否 | 否 | 移动网络码。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 307 308 309 310## DeviceCapability 311 312表示设备支持的能力。 313 314**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 315 316**系统能力:** SystemCapability.Global.ResourceManager 317 318| 名称 | 类型 | 只读 | 可选 | 说明 | 319| ------------- | ------------------------------- | ---- | ---- | -------- | 320| screenDensity | [ScreenDensity](#screendensity) | 否 | 否 | 当前设备屏幕密度。 | 321| deviceType | [DeviceType](#devicetype) | 否 | 否 | 当前设备类型。 | 322 323 324## RawFileDescriptor<sup>9+</sup> 325 326type RawFileDescriptor = _RawFileDescriptor 327 328**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 329 330**系统能力:** SystemCapability.Global.ResourceManager 331 332| 类型 | 说明 | 333| ------ | ---- | 334|[_RawFileDescriptor](js-apis-rawFileDescriptor.md#rawfiledescriptor-1)|表示rawfile文件所在HAP的文件描述符(fd)。| 335 336## Resource<sup>9+</sup> 337 338type Resource = _Resource 339 340**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 341 342**系统能力:** SystemCapability.Global.ResourceManager 343 344| 类型 | 说明 | 345| ------ | ---- | 346|[_Resource](js-apis-resource.md#resource-1)|表示资源信息,包含资源ID值、应用包名、模块名称等信息,一般可使用$r方式获取。| 347 348## ResourceManager 349 350提供访问应用资源和系统资源的能力。 351 352> **说明:** 353> 354> - ResourceManager涉及到的方法,仅限基于TS扩展的声明式开发范式使用。 355> 356> - 资源文件在工程的resources目录中定义,通过resName、resId、Resource对象等可以获取对应的字符串、字符串数组、颜色等资源值,resName为资源名称,resId可通过`$r(资源地址).id`的方式获取,例如`$r('app.string.test').id`。 357> 358> - 单HAP包获取自身资源、跨HAP/HSP包获取资源,由于入参为Resource的接口相比于入参为resName、resId的接口耗时更长,因此更推荐使用参数为resName或resId的接口。跨HAP/HSP包获取资源,**需要先使用[createModuleContext](../apis-ability-kit/js-apis-app-ability-application.md#applicationcreatemodulecontext12)创建对应module的context**,再调用参数为resName或resId的接口。具体请参考[资源访问](../../quick-start/resource-categories-and-access.md#资源访问)。 359> 360> - 示例代码中test文件的具体内容请参考[附录](#附录)。 361 362### getStringSync<sup>9+</sup> 363 364getStringSync(resId: number): string 365 366获取指定资源ID对应的字符串,使用同步方式返回。 367 368**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 369 370**系统能力:** SystemCapability.Global.ResourceManager 371 372**参数:** 373 374| 参数名 | 类型 | 必填 | 说明 | 375| ----- | ------ | ---- | ----- | 376| resId | number | 是 | 资源ID值。 | 377 378**返回值:** 379 380| 类型 | 说明 | 381| ------ | ----------- | 382| string | 资源ID值对应的字符串。 | 383 384**错误码:** 385 386以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 387 388| 错误码ID | 错误信息 | 389| -------- | ---------------------------------------- | 390| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 391| 9001001 | Invalid resource ID. | 392| 9001002 | No matching resource is found based on the resource ID. | 393| 9001006 | The resource is referenced cyclically. | 394 395**示例:** 396 ```json 397 // 资源文件路径: src/main/resources/base/element/string.json 398 { 399 "string": [ 400 { 401 "name": "test", 402 "value": "I'm a test string resource." 403 } 404 ] 405 } 406 ``` 407 ```ts 408import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 409import { BusinessError } from '@kit.BasicServicesKit'; 410 411export default class EntryAbility extends UIAbility { 412 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 413 try { 414 // 'app.string.test'仅作示例,请替换为实际使用的资源 415 let testStr = this.context.resourceManager.getStringSync($r('app.string.test').id); 416 console.log(`getStringSync, result: ${testStr}`); 417 // 打印输出结果: getStringSync, result: I'm a test string resource. 418 } catch (error) { 419 let code = (error as BusinessError).code; 420 let message = (error as BusinessError).message; 421 console.error(`getStringSync failed, error code: ${code}, message: ${message}.`); 422 } 423 } 424} 425 ``` 426 427### getStringSync<sup>10+</sup> 428 429getStringSync(resId: number, ...args: Array<string | number>): string 430 431获取指定资源ID对应的字符串,并根据args参数对字符串进行格式化,使用同步方式返回。 432 433**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 434 435**系统能力:** SystemCapability.Global.ResourceManager 436 437**参数:** 438 439| 参数名 | 类型 | 必填 | 说明 | 440| ----- | ------ | ---- | ----- | 441| resId | number | 是 | 资源ID值。 | 442| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:`%d`、`%f`、`%s`、`%%`、`%数字$d`、`%数字$f`、`%数字$s`。<br>说明:`%%`转义为`%`; `%数字$d`中的数字表示使用args中的第几个参数。<br>举例:`%%d`格式化后为`%d`字符串; `%1$d`表示使用第一个参数。 | 443 444**返回值:** 445 446| 类型 | 说明 | 447| ------ | ---------------------------- | 448| string | 资源ID值对应的格式化字符串。| 449 450**错误码:** 451 452以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 453 454| 错误码ID | 错误信息 | 455| -------- | ---------------------------------------- | 456| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 457| 9001001 | Invalid resource ID. | 458| 9001002 | No matching resource is found based on the resource ID. | 459| 9001006 | The resource is referenced cyclically. | 460| 9001007 | Failed to format the resource obtained based on the resource ID. | 461 462**示例:** 463 ```json 464 // 资源文件路径: src/main/resources/base/element/string.json 465 { 466 "string": [ 467 { 468 "name": "test", 469 "value": "I'm a %1$s, format int: %2$d, format float: %3$f." 470 } 471 ] 472 } 473 ``` 474 ```ts 475import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 476import { BusinessError } from '@kit.BasicServicesKit'; 477 478export default class EntryAbility extends UIAbility { 479 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 480 try { 481 // 'app.string.test'仅作示例,请替换为实际使用的资源 482 let testStr = this.context.resourceManager.getStringSync($r('app.string.test').id, "format string", 10, 98.78); 483 console.log(`getStringSync, result: ${testStr}`); 484 // 打印输出结果: getStringSync, result: I'm a format string, format int: 10, format float: 98.78. 485 } catch (error) { 486 let code = (error as BusinessError).code; 487 let message = (error as BusinessError).message; 488 console.error(`getStringSync failed, error code: ${code}, message: ${message}.`); 489 } 490 } 491} 492 ``` 493 494### getStringByNameSync<sup>9+</sup> 495 496getStringByNameSync(resName: string): string 497 498获取指定资源名称对应的字符串,使用同步方式返回。 499 500**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 501 502**系统能力:** SystemCapability.Global.ResourceManager 503 504**参数:** 505 506| 参数名 | 类型 | 必填 | 说明 | 507| ------- | ------ | ---- | ---- | 508| resName | string | 是 | 资源名称。 | 509 510**返回值:** 511 512| 类型 | 说明 | 513| ------ | ---------- | 514| string | 资源名称对应的字符串。 | 515 516**错误码:** 517 518以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 519 520| 错误码ID | 错误信息 | 521| -------- | ---------------------------------------- | 522| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 523| 9001003 | Invalid resource name. | 524| 9001004 | No matching resource is found based on the resource name. | 525| 9001006 | The resource is referenced cyclically. | 526 527**示例:** 528 ```json 529 // 资源文件路径: src/main/resources/base/element/string.json 530 { 531 "string": [ 532 { 533 "name": "test", 534 "value": "I'm a test string resource." 535 } 536 ] 537 } 538 ``` 539 ```ts 540import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 541import { BusinessError } from '@kit.BasicServicesKit'; 542 543export default class EntryAbility extends UIAbility { 544 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 545 try { 546 // "test"仅作示例,请替换为实际使用的资源 547 let testStr = this.context.resourceManager.getStringByNameSync("test"); 548 console.log(`getStringByNameSync, result: ${testStr}`); 549 // 打印输出结果: getStringByNameSync, result: I'm a test string resource. 550 } catch (error) { 551 let code = (error as BusinessError).code; 552 let message = (error as BusinessError).message; 553 console.error(`getStringByNameSync failed, error code: ${code}, message: ${message}.`); 554 } 555 } 556} 557 ``` 558 559### getStringByNameSync<sup>10+</sup> 560 561getStringByNameSync(resName: string, ...args: Array<string | number>): string 562 563获取指定资源名称对应的字符串,并根据args参数对字符串进行格式化,使用同步方式返回。 564 565**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 566 567**系统能力:** SystemCapability.Global.ResourceManager 568 569**参数:** 570 571| 参数名 | 类型 | 必填 | 说明 | 572| ------- | ------ | ---- | ---- | 573| resName | string | 是 | 资源名称。 | 574| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:`%d`、`%f`、`%s`、`%%`、`%数字$d`、`%数字$f`、`%数字$s`。<br>说明:`%%`转义为`%`; `%数字$d`中的数字表示使用args中的第几个参数。<br>举例:`%%d`格式化后为`%d`字符串; `%1$d`表示使用第一个参数。| 575 576**返回值:** 577 578| 类型 | 说明 | 579| ------ | ---------------------------- | 580| string | 资源名称对应的格式化字符串。| 581 582**错误码:** 583 584以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 585 586| 错误码ID | 错误信息 | 587| -------- | ---------------------------------------- | 588| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 589| 9001003 | Invalid resource name. | 590| 9001004 | No matching resource is found based on the resource name. | 591| 9001006 | The resource is referenced cyclically. | 592| 9001008 | Failed to format the resource obtained based on the resource Name. | 593 594**示例:** 595 ```json 596 // 资源文件路径: src/main/resources/base/element/string.json 597 { 598 "string": [ 599 { 600 "name": "test", 601 "value": "I'm a %1$s, format int: %2$d, format float: %3$f." 602 } 603 ] 604 } 605 ``` 606 ```ts 607import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 608import { BusinessError } from '@kit.BasicServicesKit'; 609 610export default class EntryAbility extends UIAbility { 611 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 612 try { 613 // "test"仅作示例,请替换为实际使用的资源 614 let testStr = this.context.resourceManager.getStringByNameSync("test", "format string", 10, 98.78); 615 console.log(`getStringByNameSync, result: ${testStr}`); 616 // 打印输出结果: getStringByNameSync, result: I'm a format string, format int: 10, format float: 98.78. 617 } catch (error) { 618 let code = (error as BusinessError).code; 619 let message = (error as BusinessError).message; 620 console.error(`getStringByNameSync failed, error code: ${code}, message: ${message}.`); 621 } 622 } 623} 624 ``` 625 626### getStringValue<sup>9+</sup> 627 628getStringValue(resId: number, callback: _AsyncCallback<string>): void 629 630获取指定资源ID对应的字符串,使用callback异步回调。 631 632**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 633 634**系统能力:** SystemCapability.Global.ResourceManager 635 636**参数:** 637 638| 参数名 | 类型 | 必填 | 说明 | 639| -------- | --------------------------- | ---- | --------------- | 640| resId | number | 是 | 资源ID值。 | 641| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 回调函数,返回获取的字符串。 | 642 643**错误码:** 644 645以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 646 647| 错误码ID | 错误信息 | 648| -------- | ---------------------------------------- | 649| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 650| 9001001 | Invalid resource ID. | 651| 9001002 | No matching resource is found based on the resource ID. | 652| 9001006 | The resource is referenced cyclically. | 653 654**示例Stage:** 655 ```json 656 // 资源文件路径: src/main/resources/base/element/string.json 657 { 658 "string": [ 659 { 660 "name": "test", 661 "value": "I'm a test string resource." 662 } 663 ] 664 } 665 ``` 666 ```ts 667import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 668import { BusinessError } from '@kit.BasicServicesKit'; 669 670export default class EntryAbility extends UIAbility { 671 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 672 // 'app.string.test'仅作示例,请替换为实际使用的资源 673 this.context.resourceManager.getStringValue($r('app.string.test').id, (error: BusinessError, value: string) => { 674 if (error != null) { 675 console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`); 676 } else { 677 console.log(`getStringValue, result: ${value}`); 678 // 打印输出结果: getStringValue, result: I'm a test string resource. 679 } 680 }); 681 } 682} 683 ``` 684 685### getStringValue<sup>9+</sup> 686 687getStringValue(resId: number): Promise<string> 688 689获取指定资源ID对应的字符串,使用Promise异步回调。 690 691**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 692 693**系统能力:** SystemCapability.Global.ResourceManager 694 695**参数:** 696 697| 参数名 | 类型 | 必填 | 说明 | 698| ----- | ------ | ---- | ----- | 699| resId | number | 是 | 资源ID值。 | 700 701**返回值:** 702 703| 类型 | 说明 | 704| --------------------- | ----------- | 705| Promise<string> | Promise对象,返回资源ID值对应的字符串。 | 706 707**错误码:** 708 709以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 710 711| 错误码ID | 错误信息 | 712| -------- | ---------------------------------------- | 713| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 714| 9001001 | Invalid resource ID. | 715| 9001002 | No matching resource is found based on the resource ID. | 716| 9001006 | The resource is referenced cyclically. | 717 718**示例:** 719 ```json 720 // 资源文件路径: src/main/resources/base/element/string.json 721 { 722 "string": [ 723 { 724 "name": "test", 725 "value": "I'm a test string resource." 726 } 727 ] 728 } 729 ``` 730 ```ts 731import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 732import { BusinessError } from '@kit.BasicServicesKit'; 733 734export default class EntryAbility extends UIAbility { 735 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 736 // 'app.string.test'仅作示例,请替换为实际使用的资源 737 this.context.resourceManager.getStringValue($r('app.string.test').id).then((value: string) => { 738 console.log(`getStringValue, result: ${value}`); 739 // 打印输出结果: getStringValue, result: I'm a test string resource. 740 }).catch((error: BusinessError) => { 741 console.error(`promise getStringValue failed, error code: ${error.code}, message: ${error.message}.`); 742 }); 743 } 744} 745 ``` 746 747### getStringByName<sup>9+</sup> 748 749getStringByName(resName: string, callback: _AsyncCallback<string>): void 750 751获取指定资源名称对应的字符串,使用callback异步回调。 752 753**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 754 755**系统能力:** SystemCapability.Global.ResourceManager 756 757**参数:** 758 759| 参数名 | 类型 | 必填 | 说明 | 760| -------- | --------------------------- | ---- | --------------- | 761| resName | string | 是 | 资源名称。 | 762| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 |返回获取的字符串。 | 763 764**错误码:** 765 766以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 767 768| 错误码ID | 错误信息 | 769| -------- | ---------------------------------------- | 770| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 771| 9001003 | Invalid resource name. | 772| 9001004 | No matching resource is found based on the resource name. | 773| 9001006 | The resource is referenced cyclically. | 774 775**示例:** 776 ```json 777 // 资源文件路径: src/main/resources/base/element/string.json 778 { 779 "string": [ 780 { 781 "name": "test", 782 "value": "I'm a test string resource." 783 } 784 ] 785 } 786 ``` 787 ```ts 788import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 789import { BusinessError } from '@kit.BasicServicesKit'; 790 791export default class EntryAbility extends UIAbility { 792 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 793 // "test"仅作示例,请替换为实际使用的资源 794 this.context.resourceManager.getStringByName("test", (error: BusinessError, value: string) => { 795 if (error != null) { 796 console.error(`callback getStringByName failed, error code: ${error.code}, message: ${error.message}.`); 797 } else { 798 console.log(`getStringByName, result: ${value}`); 799 // 打印输出结果: getStringByName, result: I'm a test string resource. 800 } 801 }); 802 } 803} 804 ``` 805 806### getStringByName<sup>9+</sup> 807 808getStringByName(resName: string): Promise<string> 809 810获取指定资源名称对应的字符串,使用Promise异步回调。 811 812**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 813 814**系统能力:** SystemCapability.Global.ResourceManager 815 816**参数:** 817 818| 参数名 | 类型 | 必填 | 说明 | 819| ------- | ------ | ---- | ---- | 820| resName | string | 是 | 资源名称。 | 821 822**返回值:** 823 824| 类型 | 说明 | 825| --------------------- | ---------- | 826| Promise<string> | Promise对象,返回资源名称对应的字符串。 | 827 828**错误码:** 829 830以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 831 832| 错误码ID | 错误信息 | 833| -------- | ---------------------------------------- | 834| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 835| 9001003 | Invalid resource name. | 836| 9001004 | No matching resource is found based on the resource name. | 837| 9001006 | The resource is referenced cyclically. | 838 839**示例:** 840 ```json 841 // 资源文件路径: src/main/resources/base/element/string.json 842 { 843 "string": [ 844 { 845 "name": "test", 846 "value": "I'm a test string resource." 847 } 848 ] 849 } 850 ``` 851 ```ts 852import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 853import { BusinessError } from '@kit.BasicServicesKit'; 854 855export default class EntryAbility extends UIAbility { 856 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 857 // "test"仅作示例,请替换为实际使用的资源 858 this.context.resourceManager.getStringByName("test").then((value: string) => { 859 console.log(`getStringByName, result: ${value}`); 860 // 打印输出结果: getStringByName, result: I'm a test string resource. 861 }).catch((error: BusinessError) => { 862 console.error(`promise getStringByName failed, error code: ${error.code}, message: ${error.message}.`); 863 }); 864 } 865} 866 ``` 867 868### getStringArrayValueSync<sup>10+</sup> 869 870getStringArrayValueSync(resId: number): Array<string> 871 872获取指定资源ID对应的字符串数组,使用同步方式返回。 873 874**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 875 876**系统能力:** SystemCapability.Global.ResourceManager 877 878**参数:** 879 880| 参数名 | 类型 | 必填 | 说明 | 881| ----- | ------ | ---- | ----- | 882| resId | number | 是 | 资源ID值。 | 883 884**返回值:** 885 886| 类型 | 说明 | 887| --------------------- | ----------- | 888| Array<string> | 资源ID值对应的字符串数组。 | 889 890**错误码:** 891 892以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 893 894| 错误码ID | 错误信息 | 895| -------- | ---------------------------------------- | 896| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 897| 9001001 | Invalid resource ID. | 898| 9001002 | No matching resource is found based on the resource ID. | 899| 9001006 | The resource is referenced cyclically. | 900 901**示例:** 902 ```json 903 // 资源文件路径: src/main/resources/base/element/strarray.json 904 { 905 "strarray": [ 906 { 907 "name": "test", 908 "value": [ 909 { 910 "value": "I'm one of the array's values." 911 } 912 ] 913 } 914 ] 915 } 916 ``` 917 ```ts 918import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 919import { BusinessError } from '@kit.BasicServicesKit'; 920 921export default class EntryAbility extends UIAbility { 922 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 923 try { 924 // 'app.strarray.test'仅作示例,请替换为实际使用的资源 925 let strArray: Array<string> = this.context.resourceManager.getStringArrayValueSync($r('app.strarray.test').id); 926 console.log(`getStringArrayValueSync, result: ${strArray[0]}`); 927 // 打印输出结果: getStringArrayValueSync, result: I'm one of the array's values. 928 } catch (error) { 929 let code = (error as BusinessError).code; 930 let message = (error as BusinessError).message; 931 console.error(`getStringArrayValueSync failed, error code: ${code}, message: ${message}.`); 932 } 933 } 934} 935 ``` 936 937### getStringArrayByNameSync<sup>10+</sup> 938 939getStringArrayByNameSync(resName: string): Array<string> 940 941获取指定资源名称对应的字符串数组,使用同步方式返回。 942 943**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 944 945**系统能力:** SystemCapability.Global.ResourceManager 946 947**参数:** 948 949| 参数名 | 类型 | 必填 | 说明 | 950| ----- | ------ | ---- | ----- | 951| resName | string | 是 | 资源名称。 | 952 953**返回值:** 954 955| 类型 | 说明 | 956| --------------------- | ----------- | 957| Array<string> | 对应资源名称的字符串数组。 | 958 959**错误码:** 960 961以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 962 963| 错误码ID | 错误信息 | 964| -------- | ---------------------------------------- | 965| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 966| 9001003 | Invalid resource name. | 967| 9001004 | No matching resource is found based on the resource name. | 968| 9001006 | The resource is referenced cyclically. | 969 970**示例:** 971 ```json 972 // 资源文件路径: src/main/resources/base/element/strarray.json 973 { 974 "strarray": [ 975 { 976 "name": "test", 977 "value": [ 978 { 979 "value": "I'm one of the array's values." 980 } 981 ] 982 } 983 ] 984 } 985 ``` 986 ```ts 987import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 988import { BusinessError } from '@kit.BasicServicesKit'; 989 990export default class EntryAbility extends UIAbility { 991 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 992 try { 993 // "test"仅作示例,请替换为实际使用的资源 994 let strArray: Array<string> = this.context.resourceManager.getStringArrayByNameSync("test"); 995 console.log(`getStringArrayByNameSync, result: ${strArray[0]}`); 996 // 打印输出结果: getStringArrayByNameSync, result: I'm one of the array's values. 997 } catch (error) { 998 let code = (error as BusinessError).code; 999 let message = (error as BusinessError).message; 1000 console.error(`getStringArrayByNameSync failed, error code: ${code}, message: ${message}.`); 1001 } 1002 } 1003} 1004 ``` 1005 1006### getStringArrayValue<sup>9+</sup> 1007 1008getStringArrayValue(resId: number, callback: _AsyncCallback<Array<string>>): void 1009 1010获取指定资源ID对应的字符串数组,使用callback异步回调。 1011 1012**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1013 1014**系统能力:** SystemCapability.Global.ResourceManager 1015 1016**参数:** 1017 1018| 参数名 | 类型 | 必填 | 说明 | 1019| -------- | ---------------------------------------- | ---- | ----------------- | 1020| resId | number | 是 | 资源ID值。 | 1021| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Array<string>> | 是 | 回调函数,返回资源ID值对应的字符串数组。 | 1022 1023**错误码:** 1024 1025以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1026 1027| 错误码ID | 错误信息 | 1028| -------- | ---------------------------------------- | 1029| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1030| 9001001 | Invalid resource ID. | 1031| 9001002 | No matching resource is found based on the resource ID. | 1032| 9001006 | The resource is referenced cyclically. | 1033 1034**示例:** 1035 ```json 1036 // 资源文件路径: src/main/resources/base/element/strarray.json 1037 { 1038 "strarray": [ 1039 { 1040 "name": "test", 1041 "value": [ 1042 { 1043 "value": "I'm one of the array's values." 1044 } 1045 ] 1046 } 1047 ] 1048 } 1049 ``` 1050 ```ts 1051import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 1052import { BusinessError } from '@kit.BasicServicesKit'; 1053 1054export default class EntryAbility extends UIAbility { 1055 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1056 // 'app.strarray.test'仅作示例,请替换为实际使用的资源 1057 this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id, 1058 (error: BusinessError, value: Array<string>) => { 1059 if (error != null) { 1060 console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`); 1061 } else { 1062 console.log(`getStringArrayValue, result: ${value[0]}`); 1063 // 打印输出结果: getStringArrayValue, result: I'm one of the array's values. 1064 } 1065 }); 1066 } 1067} 1068 ``` 1069 1070### getStringArrayValue<sup>9+</sup> 1071 1072getStringArrayValue(resId: number): Promise<Array<string>> 1073 1074获取指定资源ID对应的字符串数组,使用Promise异步回调。 1075 1076**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1077 1078**系统能力:** SystemCapability.Global.ResourceManager 1079 1080**参数:** 1081 1082| 参数名 | 类型 | 必填 | 说明 | 1083| ----- | ------ | ---- | ----- | 1084| resId | number | 是 | 资源ID值。 | 1085 1086**返回值:** 1087 1088| 类型 | 说明 | 1089| ---------------------------------- | ------------- | 1090| Promise<Array<string>> | Promise对象,返回资源ID值对应的字符串数组。 | 1091 1092**错误码:** 1093 1094以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1095 1096| 错误码ID | 错误信息 | 1097| -------- | ---------------------------------------- | 1098| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1099| 9001001 | Invalid resource ID. | 1100| 9001002 | No matching resource is found based on the resource ID. | 1101| 9001006 | The resource is referenced cyclically. | 1102 1103**示例:** 1104 ```json 1105 // 资源文件路径: src/main/resources/base/element/strarray.json 1106 { 1107 "strarray": [ 1108 { 1109 "name": "test", 1110 "value": [ 1111 { 1112 "value": "I'm one of the array's values." 1113 } 1114 ] 1115 } 1116 ] 1117 } 1118 ``` 1119 ```ts 1120import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 1121import { BusinessError } from '@kit.BasicServicesKit'; 1122 1123export default class EntryAbility extends UIAbility { 1124 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1125 // 'app.strarray.test'仅作示例,请替换为实际使用的资源 1126 this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id) 1127 .then((value: Array<string>) => { 1128 console.log(`getStringArrayValue, result: ${value[0]}`); 1129 // 打印输出结果: getStringArrayValue, result: I'm one of the array's values. 1130 }) 1131 .catch((error: BusinessError) => { 1132 console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`); 1133 }); 1134 } 1135} 1136 ``` 1137 1138### getStringArrayByName<sup>9+</sup> 1139 1140getStringArrayByName(resName: string, callback: _AsyncCallback<Array<string>>): void 1141 1142获取指定资源名称对应的字符串数组,使用callback异步回调。 1143 1144**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1145 1146**系统能力:** SystemCapability.Global.ResourceManager 1147 1148**参数:** 1149 1150| 参数名 | 类型 | 必填 | 说明 | 1151| -------- | ---------------------------------------- | ---- | ----------------- | 1152| resName | string | 是 | 资源名称。 | 1153| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Array<string>> | 是 | 回调函数,返回资源名称对应的字符串数组。 | 1154 1155**错误码:** 1156 1157以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1158 1159| 错误码ID | 错误信息 | 1160| -------- | ---------------------------------------- | 1161| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1162| 9001003 | Invalid resource name. | 1163| 9001004 | No matching resource is found based on the resource name. | 1164| 9001006 | The resource is referenced cyclically. | 1165 1166**示例:** 1167 ```json 1168 // 资源文件路径: src/main/resources/base/element/strarray.json 1169 { 1170 "strarray": [ 1171 { 1172 "name": "test", 1173 "value": [ 1174 { 1175 "value": "I'm one of the array's values." 1176 } 1177 ] 1178 } 1179 ] 1180 } 1181 ``` 1182 ```ts 1183import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 1184import { BusinessError } from '@kit.BasicServicesKit'; 1185 1186export default class EntryAbility extends UIAbility { 1187 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1188 // "test"仅作示例,请替换为实际使用的资源 1189 this.context.resourceManager.getStringArrayByName("test", (error: BusinessError, value: Array<string>) => { 1190 if (error != null) { 1191 console.error(`callback getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`); 1192 } else { 1193 let strArray = value; 1194 console.log(`getStringArrayByName, result: ${value[0]}`); 1195 // 打印输出结果: getStringArrayByName, result: I'm one of the array's values. 1196 } 1197 }); 1198 } 1199} 1200 ``` 1201 1202### getStringArrayByName<sup>9+</sup> 1203 1204getStringArrayByName(resName: string): Promise<Array<string>> 1205 1206获取指定资源名称对应的字符串数组,使用Promise异步回调。 1207 1208**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1209 1210**系统能力:** SystemCapability.Global.ResourceManager 1211 1212**参数:** 1213 1214| 参数名 | 类型 | 必填 | 说明 | 1215| ------- | ------ | ---- | ---- | 1216| resName | string | 是 | 资源名称。 | 1217 1218**返回值:** 1219 1220| 类型 | 说明 | 1221| ---------------------------------- | ------------ | 1222| Promise<Array<string>> | Promise对象,返回资源名称对应的字符串数组。 | 1223 1224**错误码:** 1225 1226以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1227 1228| 错误码ID | 错误信息 | 1229| -------- | ---------------------------------------- | 1230| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1231| 9001003 | Invalid resource name. | 1232| 9001004 | No matching resource is found based on the resource name. | 1233| 9001006 | The resource is referenced cyclically. | 1234 1235**示例:** 1236 ```json 1237 // 资源文件路径: src/main/resources/base/element/strarray.json 1238 { 1239 "strarray": [ 1240 { 1241 "name": "test", 1242 "value": [ 1243 { 1244 "value": "I'm one of the array's values." 1245 } 1246 ] 1247 } 1248 ] 1249 } 1250 ``` 1251 ```ts 1252import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 1253import { BusinessError } from '@kit.BasicServicesKit'; 1254 1255export default class EntryAbility extends UIAbility { 1256 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1257 // "test"仅作示例,请替换为实际使用的资源 1258 this.context.resourceManager.getStringArrayByName("test") 1259 .then((value: Array<string>) => { 1260 console.log(`getStringArrayByName, result: ${value[0]}`); 1261 // 打印输出结果: getStringArrayByName, result: I'm one of the array's values. 1262 }) 1263 .catch((error: BusinessError) => { 1264 console.error(`promise getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`); 1265 }); 1266 } 1267} 1268 ``` 1269 1270### getIntPluralStringValueSync<sup>18+</sup> 1271 1272getIntPluralStringValueSync(resId: number, num: number, ...args: Array<string | number>): string 1273 1274获取指定资源ID对应的[单复数](../../internationalization/l10n-singular-plural.md)字符串,并根据args参数对字符串进行格式化,使用同步方式返回。 1275 1276> **说明** 1277> 1278> 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 1279 1280**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 1281 1282**系统能力:** SystemCapability.Global.ResourceManager 1283 1284**参数:** 1285 1286| 参数名 | 类型 | 必填 | 说明 | 1287| ------- | ----------------------- | ---- | ------------------------------------------------------------ | 1288| resId | number | 是 | 资源ID值。 | 1289| num | number | 是 | 数量值(整数)。根据当前语言的[单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)获取该数量值对应的字符串。 | 1290| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:`%d`、`%f`、`%s`、`%%`、`%数字$d`、`%数字$f`、`%数字$s`。<br>说明:`%%`转义为`%`; `%数字$d`中的数字表示使用args中的第几个参数。<br>举例:`%%d`格式化后为`%d`字符串; `%1$d`表示使用第一个参数。 | 1291 1292**返回值:** 1293 1294| 类型 | 说明 | 1295| ------ | ---------------------- | 1296| string | 资源ID值对应的格式化单复数字符串。 | 1297 1298**错误码:** 1299 1300以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 1301 1302| 错误码ID | 错误信息 | 1303| -------- | ------------------------------------------------------------ | 1304| 9001001 | Invalid resource ID. | 1305| 9001002 | No matching resource is found based on the resource ID. | 1306| 9001006 | The resource is referenced cyclically. | 1307| 9001007 | Failed to format the resource obtained based on the resource ID. | 1308 1309**示例:** 1310 ```json 1311 // 资源文件路径: src/main/resources/base/element/plural.json 1312 { 1313 "plural": [ 1314 { 1315 "name": "format_test", 1316 "value": [ 1317 { 1318 "quantity": "one", 1319 "value": "There is %d apple in the %s, the total amount is %f kg." 1320 }, 1321 { 1322 "quantity": "other", 1323 "value": "There are %d apples in the %s, the total amount is %f kg." 1324 } 1325 ] 1326 } 1327 ] 1328 } 1329 ``` 1330 ```ts 1331import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 1332import { BusinessError } from '@kit.BasicServicesKit'; 1333 1334export default class EntryAbility extends UIAbility { 1335 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1336 try { 1337 // 根据语言单复数规则,参数num取值为1,英文环境下对应单复数类别为one 1338 // 在资源文件中用quantity字段表示单复数类别,因此会获取quantity为one的字符串 1339 // 'app.plural.format_test'仅作示例,请替换为实际使用的资源 1340 let pluralStr = this.context.resourceManager.getIntPluralStringValueSync($r('app.plural.format_test').id, 1, 1, "basket", 0.3); 1341 console.log(`getIntPluralStringValueSync, result: ${pluralStr}`); 1342 // 打印输出结果: getIntPluralStringValueSync, result: There is 1 apple in the basket, the total amount is 0.3 kg. 1343 } catch (error) { 1344 let code = (error as BusinessError).code; 1345 let message = (error as BusinessError).message; 1346 console.error(`getIntPluralStringValueSync failed, error code: ${code}, message: ${message}.`); 1347 } 1348 } 1349} 1350 ``` 1351 1352### getIntPluralStringByNameSync<sup>18+</sup> 1353 1354getIntPluralStringByNameSync(resName: string, num: number, ...args: Array<string | number>): string 1355 1356获取指定资源名称对应的[单复数](../../internationalization/l10n-singular-plural.md)字符串,并根据args参数对字符串进行格式化,使用同步方式返回。 1357 1358> **说明** 1359> 1360> 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 1361 1362**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 1363 1364**系统能力:** SystemCapability.Global.ResourceManager 1365 1366**参数:** 1367 1368| 参数名 | 类型 | 必填 | 说明 | 1369| ------- | ----------------------- | ---- | ------------------------------------------------------------ | 1370| resName | string | 是 | 资源名称。 | 1371| num | number | 是 | 数量值(整数)。根据当前语言的[单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)获取该数量值对应的字符串。 | 1372| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:`%d`、`%f`、`%s`、`%%`、`%数字$d`、`%数字$f`、`%数字$s`。<br>说明:`%%`转义为`%`; `%数字$d`中的数字表示使用args中的第几个参数。<br>举例:`%%d`格式化后为`%d`字符串; `%1$d`表示使用第一个参数。 | 1373 1374**返回值:** 1375 1376| 类型 | 说明 | 1377| ------ | ------------------------------------ | 1378| string | 资源名称对应的格式化单复数字符串。 | 1379 1380**错误码:** 1381 1382以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 1383 1384| 错误码ID | 错误信息 | 1385| -------- | ------------------------------------------------------------ | 1386| 9001003 | Invalid resource name. | 1387| 9001004 | No matching resource is found based on the resource name. | 1388| 9001006 | The resource is referenced cyclically. | 1389| 9001008 | Failed to format the resource obtained based on the resource name. | 1390 1391**示例:** 1392 ```json 1393 // 资源文件路径: src/main/resources/base/element/plural.json 1394 { 1395 "plural": [ 1396 { 1397 "name": "format_test", 1398 "value": [ 1399 { 1400 "quantity": "one", 1401 "value": "There is %d apple in the %s, the total amount is %f kg." 1402 }, 1403 { 1404 "quantity": "other", 1405 "value": "There are %d apples in the %s, the total amount is %f kg." 1406 } 1407 ] 1408 } 1409 ] 1410 } 1411 ``` 1412 ```ts 1413import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 1414import { BusinessError } from '@kit.BasicServicesKit'; 1415 1416export default class EntryAbility extends UIAbility { 1417 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1418 try { 1419 // 根据语言单复数规则,参数num取值为1,英文环境下对应单复数类别为one 1420 // 在资源文件中用quantity字段表示单复数类别,因此会获取quantity为one的字符串 1421 // "format_test"仅作示例,请替换为实际使用的资源 1422 let pluralStr = this.context.resourceManager.getIntPluralStringByNameSync("format_test", 1, 1, "basket", 0.3); 1423 console.log(`getIntPluralStringByNameSync, result: ${pluralStr}`); 1424 // 打印输出结果: getIntPluralStringByNameSync, result: There is 1 apple in the basket, the total amount is 0.3 kg. 1425 } catch (error) { 1426 let code = (error as BusinessError).code; 1427 let message = (error as BusinessError).message; 1428 console.error(`getIntPluralStringByNameSync failed, error code: ${code}, message: ${message}.`); 1429 } 1430 } 1431} 1432 ``` 1433 1434### getDoublePluralStringValueSync<sup>18+</sup> 1435 1436getDoublePluralStringValueSync(resId: number, num: number, ...args: Array<string | number>): string 1437 1438获取指定资源ID对应的[单复数](../../internationalization/l10n-singular-plural.md)字符串,并根据args参数对字符串进行格式化,使用同步方式返回。 1439 1440> **说明** 1441> 1442> 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 1443 1444**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 1445 1446**系统能力:** SystemCapability.Global.ResourceManager 1447 1448**参数:** 1449 1450| 参数名 | 类型 | 必填 | 说明 | 1451| ------- | ----------------------- | ---- | ------------------------------------------------------------ | 1452| resId | number | 是 | 资源ID值。 | 1453| num | number | 是 | 数量值(浮点数)。根据当前语言的[单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)获取该数量值对应的字符串。 | 1454| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:`%d`、`%f`、`%s`、`%%`、`%数字$d`、`%数字$f`、`%数字$s`。<br>说明:`%%`转义为`%`; `%数字$d`中的数字表示使用args中的第几个参数。<br>举例:`%%d`格式化后为`%d`字符串; `%1$d`表示使用第一个参数。 | 1455 1456**返回值:** 1457 1458| 类型 | 说明 | 1459| ------ | -------------------------------- | 1460| string | 资源ID值对应的格式化单复数字符串。 | 1461 1462**错误码:** 1463 1464以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 1465 1466| 错误码ID | 错误信息 | 1467| -------- | ------------------------------------------------------------ | 1468| 9001001 | Invalid resource ID. | 1469| 9001002 | No matching resource is found based on the resource ID. | 1470| 9001006 | The resource is referenced cyclically. | 1471| 9001007 | Failed to format the resource obtained based on the resource ID. | 1472 1473**示例:** 1474 ```json 1475 // 资源文件路径: src/main/resources/base/element/plural.json 1476 { 1477 "plural": [ 1478 { 1479 "name": "format_test", 1480 "value": [ 1481 { 1482 "quantity": "one", 1483 "value": "There is %d apple in the %s, the total amount is %f kg." 1484 }, 1485 { 1486 "quantity": "other", 1487 "value": "There are %d apples in the %s, the total amount is %f kg." 1488 } 1489 ] 1490 } 1491 ] 1492 } 1493 ``` 1494 ```ts 1495import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 1496import { BusinessError } from '@kit.BasicServicesKit'; 1497 1498export default class EntryAbility extends UIAbility { 1499 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1500 try { 1501 // 根据语言单复数规则,参数num取值为2.1,英文环境下对应单复数类别为other 1502 // 在资源文件中用quantity字段表示单复数类别,因此会获取quantity为other的字符串 1503 // 'app.plural.format_test'仅作示例,请替换为实际使用的资源 1504 let pluralStr = this.context.resourceManager.getDoublePluralStringValueSync($r('app.plural.format_test').id, 2.1, 2, "basket", 0.6); 1505 console.log(`getDoublePluralStringValueSync, result: ${pluralStr}`); 1506 // 打印输出结果: getDoublePluralStringValueSync, result: There are 2 apples in the basket, the total amount is 0.6 kg. 1507 } catch (error) { 1508 let code = (error as BusinessError).code; 1509 let message = (error as BusinessError).message; 1510 console.error(`getDoublePluralStringValueSync failed, error code: ${code}, message: ${message}.`); 1511 } 1512 } 1513} 1514 ``` 1515 1516### getDoublePluralStringByNameSync<sup>18+</sup> 1517 1518getDoublePluralStringByNameSync(resName: string, num: number, ...args: Array<string | number>): string 1519 1520获取指定资源名称对应的[单复数](../../internationalization/l10n-singular-plural.md)字符串,并根据args参数对字符串进行格式化,使用同步方式返回。 1521 1522> **说明** 1523> 1524> 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 1525 1526**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 1527 1528**系统能力:** SystemCapability.Global.ResourceManager 1529 1530**参数:** 1531 1532| 参数名 | 类型 | 必填 | 说明 | 1533| ------- | ----------------------- | ---- | ------------------------------------------------------------ | 1534| resName | string | 是 | 资源名称。 | 1535| num | number | 是 | 数量值(浮点数)。根据当前语言的[单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)获取该数量值对应的字符串。 | 1536| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:`%d`、`%f`、`%s`、`%%`、`%数字$d`、`%数字$f`、`%数字$s`。<br>说明:`%%`转义为`%`; `%数字$d`中的数字表示使用args中的第几个参数。<br>举例:`%%d`格式化后为`%d`字符串; `%1$d`表示使用第一个参数。 | 1537 1538**返回值:** 1539 1540| 类型 | 说明 | 1541| ------ | -------------------------------- | 1542| string | 资源名称对应的格式化单复数字符串。 | 1543 1544**错误码:** 1545 1546以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 1547 1548| 错误码ID | 错误信息 | 1549| -------- | ------------------------------------------------------------ | 1550| 9001003 | Invalid resource name. | 1551| 9001004 | No matching resource is found based on the resource name. | 1552| 9001006 | The resource is referenced cyclically. | 1553| 9001008 | Failed to format the resource obtained based on the resource name. | 1554 1555**示例:** 1556 ```json 1557 // 资源文件路径: src/main/resources/base/element/plural.json 1558 { 1559 "plural": [ 1560 { 1561 "name": "format_test", 1562 "value": [ 1563 { 1564 "quantity": "one", 1565 "value": "There is %d apple in the %s, the total amount is %f kg." 1566 }, 1567 { 1568 "quantity": "other", 1569 "value": "There are %d apples in the %s, the total amount is %f kg." 1570 } 1571 ] 1572 } 1573 ] 1574 } 1575 ``` 1576 ```ts 1577import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 1578import { BusinessError } from '@kit.BasicServicesKit'; 1579 1580export default class EntryAbility extends UIAbility { 1581 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1582 try { 1583 // 根据语言单复数规则,参数num取值为2.1,英文环境下对应单复数类别为other 1584 // 在资源文件中用quantity字段表示单复数类别,因此会获取quantity为other的字符串 1585 // "format_test"仅作示例,请替换为实际使用的资源 1586 let pluralStr = this.context.resourceManager.getDoublePluralStringByNameSync("format_test", 2.1, 2, "basket", 0.6); 1587 console.log(`getDoublePluralStringByNameSync, result: ${pluralStr}`); 1588 // 打印输出结果: getIntPluralStringValueSync, result: There are 2 apples in the basket, the total amount is 0.6 kg. 1589 } catch (error) { 1590 let code = (error as BusinessError).code; 1591 let message = (error as BusinessError).message; 1592 console.error(`getDoublePluralStringByNameSync failed, error code: ${code}, message: ${message}.`); 1593 } 1594 } 1595} 1596 ``` 1597 1598### getMediaContentSync<sup>10+</sup> 1599 1600getMediaContentSync(resId: number, density?: number): Uint8Array 1601 1602获取指定资源ID对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回。 1603 1604**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1605 1606**系统能力:** SystemCapability.Global.ResourceManager 1607 1608**参数:** 1609 1610| 参数名 | 类型 | 必填 | 说明 | 1611| ----- | ------ | ---- | ----- | 1612| resId | number | 是 | 资源ID值。 | 1613| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 1614 1615**返回值:** 1616 1617| 类型 | 说明 | 1618| -------- | ----------- | 1619| Uint8Array | 资源ID对应的媒体文件内容。 | 1620 1621**错误码:** 1622 1623以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1624 1625| 错误码ID | 错误信息 | 1626| -------- | ---------------------------------------- | 1627| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 1628| 9001001 | Invalid resource ID. | 1629| 9001002 | No matching resource is found based on the resource ID. | 1630 1631**示例:** 1632 ```ts 1633import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 1634import { BusinessError } from '@kit.BasicServicesKit'; 1635 1636export default class EntryAbility extends UIAbility { 1637 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1638 try { 1639 // 'app.media.test'仅作示例,请替换为实际使用的资源 1640 this.context.resourceManager.getMediaContentSync($r('app.media.test').id); // 默认屏幕密度 1641 } catch (error) { 1642 let code = (error as BusinessError).code; 1643 let message = (error as BusinessError).message; 1644 console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`); 1645 } 1646 1647 try { 1648 // 'app.media.test'仅作示例,请替换为实际使用的资源 1649 this.context.resourceManager.getMediaContentSync($r('app.media.test').id, 120); // 指定屏幕密度 1650 } catch (error) { 1651 let code = (error as BusinessError).code; 1652 let message = (error as BusinessError).message; 1653 console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`); 1654 } 1655 } 1656} 1657 ``` 1658 1659### getMediaByNameSync<sup>10+</sup> 1660 1661getMediaByNameSync(resName: string, density?: number): Uint8Array 1662 1663获取指定资源名称对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回。 1664 1665**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1666 1667**系统能力:** SystemCapability.Global.ResourceManager 1668 1669**参数:** 1670 1671| 参数名 | 类型 | 必填 | 说明 | 1672| ----- | ------ | ---- | ----- | 1673| resName | string | 是 | 资源名称。 | 1674| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 1675 1676**返回值:** 1677 1678| 类型 | 说明 | 1679| --------------------- | ----------- | 1680| Uint8Array | 资源名称对应的媒体文件内容。 | 1681 1682**错误码:** 1683 1684以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1685 1686| 错误码ID | 错误信息 | 1687| -------- | ---------------------------------------- | 1688| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 1689| 9001003 | Invalid resource name. | 1690| 9001004 | No matching resource is found based on the resource name. | 1691 1692**示例:** 1693 ```ts 1694import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 1695import { BusinessError } from '@kit.BasicServicesKit'; 1696 1697export default class EntryAbility extends UIAbility { 1698 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1699 try { 1700 // "test"仅作示例,请替换为实际使用的资源 1701 this.context.resourceManager.getMediaByNameSync("test"); // 默认屏幕密度 1702 } catch (error) { 1703 let code = (error as BusinessError).code; 1704 let message = (error as BusinessError).message; 1705 console.error(`getMediaByNameSync failed, error code: ${code}, message: ${message}.`); 1706 } 1707 1708 try { 1709 // "test"仅作示例,请替换为实际使用的资源 1710 this.context.resourceManager.getMediaByNameSync("test", 120); // 指定屏幕密度 1711 } catch (error) { 1712 let code = (error as BusinessError).code; 1713 let message = (error as BusinessError).message; 1714 console.error(`getMediaByNameSync failed, error code: ${code}, message: ${message}.`); 1715 } 1716 } 1717} 1718 ``` 1719 1720### getMediaContent<sup>9+</sup> 1721 1722getMediaContent(resId: number, callback: _AsyncCallback<Uint8Array>): void 1723 1724获取指定资源ID对应的媒体文件内容,使用callback异步回调。 1725 1726**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1727 1728**系统能力:** SystemCapability.Global.ResourceManager 1729 1730**参数:** 1731 1732| 参数名 | 类型 | 必填 | 说明 | 1733| -------- | ------------------------------- | ---- | ------------------ | 1734| resId | number | 是 | 资源ID值。 | 1735| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Uint8Array> | 是 | 回调函数,返回资源ID对应的媒体文件内容。 | 1736 1737**错误码:** 1738 1739以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1740 1741| 错误码ID | 错误信息 | 1742| -------- | ---------------------------------------- | 1743| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1744| 9001001 | Invalid resource ID. | 1745| 9001002 | No matching resource is found based on the resource ID. | 1746 1747**示例:** 1748 ```ts 1749import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 1750import { BusinessError } from '@kit.BasicServicesKit'; 1751 1752export default class EntryAbility extends UIAbility { 1753 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1754 try { 1755 // 'app.media.test'仅作示例,请替换为实际使用的资源 1756 this.context.resourceManager.getMediaContent($r('app.media.test').id, 1757 (error: BusinessError, value: Uint8Array) => { 1758 if (error != null) { 1759 console.error("error is " + error); 1760 } else { 1761 let media = value; 1762 } 1763 }); 1764 } catch (error) { 1765 let code = (error as BusinessError).code; 1766 let message = (error as BusinessError).message; 1767 console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`); 1768 } 1769 } 1770} 1771 ``` 1772 1773### getMediaContent<sup>10+</sup> 1774 1775getMediaContent(resId: number, density: number, callback: _AsyncCallback<Uint8Array>): void 1776 1777获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback异步回调。 1778 1779**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1780 1781**系统能力:** SystemCapability.Global.ResourceManager 1782 1783**参数:** 1784 1785| 参数名 | 类型 | 必填 | 说明 | 1786| -------- | ------------------------------- | ---- | ------------------ | 1787| resId | number | 是 | 资源ID值。 | 1788| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 1789| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Uint8Array> | 是 | 回调函数,返回资源ID对应的媒体文件内容。 | 1790 1791**错误码:** 1792 1793以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1794 1795| 错误码ID | 错误信息 | 1796| -------- | ---------------------------------------- | 1797| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 1798| 9001001 | Invalid resource ID. | 1799| 9001002 | No matching resource is found based on the resource ID. | 1800 1801**示例:** 1802 ```ts 1803import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 1804import { BusinessError } from '@kit.BasicServicesKit'; 1805 1806export default class EntryAbility extends UIAbility { 1807 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1808 try { 1809 // 'app.media.test'仅作示例,请替换为实际使用的资源 1810 this.context.resourceManager.getMediaContent($r('app.media.test').id, 120, (error: BusinessError, value: Uint8Array) => { 1811 if (error != null) { 1812 console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); 1813 } else { 1814 let media = value; 1815 } 1816 }); 1817 } catch (error) { 1818 let code = (error as BusinessError).code; 1819 let message = (error as BusinessError).message; 1820 console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`); 1821 } 1822 } 1823} 1824 ``` 1825 1826### getMediaContent<sup>9+</sup> 1827 1828getMediaContent(resId: number): Promise<Uint8Array> 1829 1830获取指定资源ID对应的媒体文件内容,使用Promise异步回调。 1831 1832**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1833 1834**系统能力:** SystemCapability.Global.ResourceManager 1835 1836**参数:** 1837 1838| 参数名 | 类型 | 必填 | 说明 | 1839| ----- | ------ | ---- | ----- | 1840| resId | number | 是 | 资源ID值。 | 1841 1842**返回值:** 1843 1844| 类型 | 说明 | 1845| ------------------------- | -------------- | 1846| Promise<Uint8Array> | Promise对象,返回资源ID值对应的媒体文件内容。 | 1847 1848**错误码:** 1849 1850以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1851 1852| 错误码ID | 错误信息 | 1853| -------- | ---------------------------------------- | 1854| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1855| 9001001 | Invalid resource ID. | 1856| 9001002 | No matching resource is found based on the resource ID. | 1857 1858**示例:** 1859 ```ts 1860import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 1861import { BusinessError } from '@kit.BasicServicesKit'; 1862 1863export default class EntryAbility extends UIAbility { 1864 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1865 try { 1866 // 'app.media.test'仅作示例,请替换为实际使用的资源 1867 this.context.resourceManager.getMediaContent($r('app.media.test').id).then((value: Uint8Array) => { 1868 let media = value; 1869 }).catch((error: BusinessError) => { 1870 console.error("getMediaContent promise error is " + error); 1871 }); 1872 } catch (error) { 1873 let code = (error as BusinessError).code; 1874 let message = (error as BusinessError).message; 1875 console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`); 1876 } 1877 } 1878} 1879 ``` 1880 1881### getMediaContent<sup>10+</sup> 1882 1883getMediaContent(resId: number, density: number): Promise<Uint8Array> 1884 1885获取指定资源ID对应的指定屏幕密度媒体文件内容,使用Promise异步回调。 1886 1887**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1888 1889**系统能力:** SystemCapability.Global.ResourceManager 1890 1891**参数:** 1892 1893| 参数名 | 类型 | 必填 | 说明 | 1894| ----- | ------ | ---- | ----- | 1895| resId | number | 是 | 资源ID值。 | 1896| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 1897 1898**返回值:** 1899 1900| 类型 | 说明 | 1901| ------------------------- | -------------- | 1902| Promise<Uint8Array> | Promise对象,返回资源ID值对应的媒体文件内容。 | 1903 1904**错误码:** 1905 1906以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1907 1908| 错误码ID | 错误信息 | 1909| -------- | ---------------------------------------- | 1910| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 1911| 9001001 | Invalid resource ID. | 1912| 9001002 | No matching resource is found based on the resource ID. | 1913 1914**示例:** 1915 ```ts 1916import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 1917import { BusinessError } from '@kit.BasicServicesKit'; 1918 1919export default class EntryAbility extends UIAbility { 1920 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1921 try { 1922 // 'app.media.test'仅作示例,请替换为实际使用的资源 1923 this.context.resourceManager.getMediaContent($r('app.media.test').id, 120).then((value: Uint8Array) => { 1924 let media = value; 1925 }).catch((error: BusinessError) => { 1926 console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); 1927 }); 1928 } catch (error) { 1929 let code = (error as BusinessError).code; 1930 let message = (error as BusinessError).message; 1931 console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`); 1932 } 1933 } 1934} 1935 ``` 1936 1937### getMediaByName<sup>9+</sup> 1938 1939getMediaByName(resName: string, callback: _AsyncCallback<Uint8Array>): void 1940 1941获取指定资源名称对应的媒体文件内容,使用callback异步回调。 1942 1943**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1944 1945**系统能力:** SystemCapability.Global.ResourceManager 1946 1947**参数:** 1948 1949| 参数名 | 类型 | 必填 | 说明 | 1950| -------- | ------------------------------- | ---- | ------------------ | 1951| resName | string | 是 | 资源名称。 | 1952| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Uint8Array> | 是 | 回调函数,返回资源名称对应的媒体文件内容。 | 1953 1954**错误码:** 1955 1956以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1957 1958| 错误码ID | 错误信息 | 1959| -------- | ---------------------------------------- | 1960| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1961| 9001003 | Invalid resource name. | 1962| 9001004 | No matching resource is found based on the resource name. | 1963 1964**示例:** 1965 ```ts 1966import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 1967import { BusinessError } from '@kit.BasicServicesKit'; 1968 1969export default class EntryAbility extends UIAbility { 1970 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1971 try { 1972 // "test"仅作示例,请替换为实际使用的资源 1973 this.context.resourceManager.getMediaByName("test", (error: BusinessError, value: Uint8Array) => { 1974 if (error != null) { 1975 console.error("error is " + error); 1976 } else { 1977 let media = value; 1978 } 1979 }); 1980 } catch (error) { 1981 let code = (error as BusinessError).code; 1982 let message = (error as BusinessError).message; 1983 console.error(`callback getMediaByName failed, error code: ${code}, message: ${message}.`); 1984 } 1985 } 1986} 1987 ``` 1988 1989### getMediaByName<sup>10+</sup> 1990 1991getMediaByName(resName: string, density: number, callback: _AsyncCallback<Uint8Array>): void 1992 1993获取指定资源名称对应的指定屏幕密度媒体文件内容,使用callback异步回调。 1994 1995**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1996 1997**系统能力:** SystemCapability.Global.ResourceManager 1998 1999**参数:** 2000 2001| 参数名 | 类型 | 必填 | 说明 | 2002| -------- | ------------------------------- | ---- | ------------------ | 2003| resName | string | 是 | 资源名称。 | 2004| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 2005| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Uint8Array> | 是 | 回调函数,返回资源名称对应的媒体文件内容。 | 2006 2007**错误码:** 2008 2009以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2010 2011| 错误码ID | 错误信息 | 2012| -------- | ---------------------------------------- | 2013| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2014| 9001003 | Invalid resource name. | 2015| 9001004 | No matching resource is found based on the resource name. | 2016 2017**示例:** 2018 ```ts 2019import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2020import { BusinessError } from '@kit.BasicServicesKit'; 2021 2022export default class EntryAbility extends UIAbility { 2023 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2024 try { 2025 // "test"仅作示例,请替换为实际使用的资源 2026 this.context.resourceManager.getMediaByName("test", 120, (error: BusinessError, value: Uint8Array) => { 2027 if (error != null) { 2028 console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); 2029 } else { 2030 let media = value; 2031 } 2032 }); 2033 } catch (error) { 2034 let code = (error as BusinessError).code; 2035 let message = (error as BusinessError).message; 2036 console.error(`callback getMediaByName failed, error code: ${code}, message: ${message}.`); 2037 } 2038 } 2039} 2040 ``` 2041 2042### getMediaByName<sup>9+</sup> 2043 2044getMediaByName(resName: string): Promise<Uint8Array> 2045 2046获取指定资源名称对应的媒体文件内容,使用Promise异步回调。 2047 2048**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2049 2050**系统能力:** SystemCapability.Global.ResourceManager 2051 2052**参数:** 2053 2054| 参数名 | 类型 | 必填 | 说明 | 2055| ------- | ------ | ---- | ---- | 2056| resName | string | 是 | 资源名称。 | 2057 2058**返回值:** 2059 2060| 类型 | 说明 | 2061| ------------------------- | ------------- | 2062| Promise<Uint8Array> | Promise对象,返回资源名称对应的媒体文件内容。 | 2063 2064**错误码:** 2065 2066以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2067 2068| 错误码ID | 错误信息 | 2069| -------- | ---------------------------------------- | 2070| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 2071| 9001003 | Invalid resource name. | 2072| 9001004 | No matching resource is found based on the resource name. | 2073 2074**示例:** 2075 ```ts 2076import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2077import { BusinessError } from '@kit.BasicServicesKit'; 2078 2079export default class EntryAbility extends UIAbility { 2080 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2081 try { 2082 // "test"仅作示例,请替换为实际使用的资源 2083 this.context.resourceManager.getMediaByName("test").then((value: Uint8Array) => { 2084 let media = value; 2085 }).catch((error: BusinessError) => { 2086 console.error("getMediaByName promise error is " + error); 2087 }); 2088 } catch (error) { 2089 let code = (error as BusinessError).code; 2090 let message = (error as BusinessError).message; 2091 console.error(`promise getMediaByName failed, error code: ${code}, message: ${message}.`); 2092 } 2093 } 2094} 2095 ``` 2096 2097### getMediaByName<sup>10+</sup> 2098 2099getMediaByName(resName: string, density: number): Promise<Uint8Array> 2100 2101获取指定资源名称对应的指定屏幕密度媒体文件内容,使用Promise异步回调。 2102 2103**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2104 2105**系统能力:** SystemCapability.Global.ResourceManager 2106 2107**参数:** 2108 2109| 参数名 | 类型 | 必填 | 说明 | 2110| ------- | ------ | ---- | ---- | 2111| resName | string | 是 | 资源名称。 | 2112| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 2113 2114**返回值:** 2115 2116| 类型 | 说明 | 2117| ------------------------- | ------------- | 2118| Promise<Uint8Array> | Promise对象,返回资源名称对应的媒体文件内容。 | 2119 2120**错误码:** 2121 2122以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2123 2124| 错误码ID | 错误信息 | 2125| -------- | ---------------------------------------- | 2126| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2127| 9001003 | Invalid resource name. | 2128| 9001004 | No matching resource is found based on the resource name. | 2129 2130**示例:** 2131 ```ts 2132import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2133import { BusinessError } from '@kit.BasicServicesKit'; 2134 2135export default class EntryAbility extends UIAbility { 2136 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2137 try { 2138 // "test"仅作示例,请替换为实际使用的资源 2139 this.context.resourceManager.getMediaByName("test", 120).then((value: Uint8Array) => { 2140 let media = value; 2141 }).catch((error: BusinessError) => { 2142 console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); 2143 }); 2144 } catch (error) { 2145 let code = (error as BusinessError).code; 2146 let message = (error as BusinessError).message; 2147 console.error(`promise getMediaByName failed, error code: ${code}, message: ${message}.`); 2148 } 2149 } 2150} 2151 ``` 2152 2153### getMediaContentBase64Sync<sup>10+</sup> 2154 2155getMediaContentBase64Sync(resId: number, density?: number): string 2156 2157获取指定资源ID对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回。 2158 2159**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2160 2161**系统能力:** SystemCapability.Global.ResourceManager 2162 2163**参数:** 2164 2165| 参数名 | 类型 | 必填 | 说明 | 2166| ----- | ------ | ---- | ----- | 2167| resId | number | 是 | 资源ID值。 | 2168| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 2169 2170**返回值:** 2171 2172| 类型 | 说明 | 2173| -------- | ----------- | 2174| string | 资源ID对应的图片资源Base64编码。 | 2175 2176**错误码:** 2177 2178以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2179 2180| 错误码ID | 错误信息 | 2181| -------- | ---------------------------------------- | 2182| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2183| 9001001 | Invalid resource ID. | 2184| 9001002 | No matching resource is found based on the resource ID. | 2185 2186**示例:** 2187 ```ts 2188import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2189import { BusinessError } from '@kit.BasicServicesKit'; 2190 2191export default class EntryAbility extends UIAbility { 2192 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2193 try { 2194 // 'app.media.test'仅作示例,请替换为实际使用的资源 2195 this.context.resourceManager.getMediaContentBase64Sync($r('app.media.test').id); // 默认屏幕密度 2196 } catch (error) { 2197 let code = (error as BusinessError).code; 2198 let message = (error as BusinessError).message; 2199 console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`); 2200 } 2201 2202 try { 2203 // 'app.media.test'仅作示例,请替换为实际使用的资源 2204 this.context.resourceManager.getMediaContentBase64Sync($r('app.media.test').id, 120); // 指定屏幕密度 2205 } catch (error) { 2206 let code = (error as BusinessError).code; 2207 let message = (error as BusinessError).message; 2208 console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`); 2209 } 2210 } 2211} 2212 ``` 2213 2214### getMediaBase64ByNameSync<sup>10+</sup> 2215 2216getMediaBase64ByNameSync(resName: string, density?: number): string 2217 2218获取指定资源名称对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回。 2219 2220**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2221 2222**系统能力:** SystemCapability.Global.ResourceManager 2223 2224**参数:** 2225 2226| 参数名 | 类型 | 必填 | 说明 | 2227| ----- | ------ | ---- | ----- | 2228| resName | string | 是 | 资源名称。 | 2229| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 2230 2231**返回值:** 2232 2233| 类型 | 说明 | 2234| --------------------- | ----------- | 2235| string | 资源名称对应的图片资源Base64编码。 | 2236 2237**错误码:** 2238 2239以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2240 2241| 错误码ID | 错误信息 | 2242| -------- | ---------------------------------------- | 2243| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2244| 9001003 | Invalid resource name. | 2245| 9001004 | No matching resource is found based on the resource name. | 2246 2247**示例:** 2248 ```ts 2249import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2250import { BusinessError } from '@kit.BasicServicesKit'; 2251 2252export default class EntryAbility extends UIAbility { 2253 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2254 try { 2255 // "test"仅作示例,请替换为实际使用的资源 2256 this.context.resourceManager.getMediaBase64ByNameSync("test"); // 默认屏幕密度 2257 } catch (error) { 2258 let code = (error as BusinessError).code; 2259 let message = (error as BusinessError).message; 2260 console.error(`getMediaBase64ByNameSync failed, error code: ${code}, message: ${message}.`); 2261 } 2262 2263 try { 2264 // "test"仅作示例,请替换为实际使用的资源 2265 this.context.resourceManager.getMediaBase64ByNameSync("test", 120); // 指定屏幕密度 2266 } catch (error) { 2267 let code = (error as BusinessError).code; 2268 let message = (error as BusinessError).message; 2269 console.error(`getMediaBase64ByNameSync failed, error code: ${code}, message: ${message}.`); 2270 } 2271 } 2272} 2273 ``` 2274 2275### getMediaContentBase64<sup>9+</sup> 2276 2277getMediaContentBase64(resId: number, callback: _AsyncCallback<string>): void 2278 2279获取指定资源ID对应的图片资源Base64编码,使用callback异步回调。 2280 2281**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2282 2283**系统能力:** SystemCapability.Global.ResourceManager 2284 2285**参数:** 2286 2287| 参数名 | 类型 | 必填 | 说明 | 2288| -------- | --------------------------- | ---- | ------------------------ | 2289| resId | number | 是 | 资源ID值。 | 2290| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 回调函数,返回资源ID值对应的图片资源Base64编码。 | 2291 2292**错误码:** 2293 2294以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2295 2296| 错误码ID | 错误信息 | 2297| -------- | ---------------------------------------- | 2298| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 2299| 9001001 | Invalid resource ID. | 2300| 9001002 | No matching resource is found based on the resource ID. | 2301 2302**示例:** 2303 ```ts 2304import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2305import { BusinessError } from '@kit.BasicServicesKit'; 2306 2307export default class EntryAbility extends UIAbility { 2308 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2309 try { 2310 // 'app.media.test'仅作示例,请替换为实际使用的资源 2311 this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, (error: BusinessError, value: string) => { 2312 if (error != null) { 2313 console.error("error is " + error); 2314 } else { 2315 let media = value; 2316 } 2317 }); 2318 } catch (error) { 2319 let code = (error as BusinessError).code; 2320 let message = (error as BusinessError).message; 2321 console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2322 } 2323 } 2324} 2325 ``` 2326 2327### getMediaContentBase64<sup>10+</sup> 2328 2329getMediaContentBase64(resId: number, density: number, callback: _AsyncCallback<string>): void 2330 2331获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用callback异步回调。 2332 2333**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2334 2335**系统能力:** SystemCapability.Global.ResourceManager 2336 2337**参数:** 2338 2339| 参数名 | 类型 | 必填 | 说明 | 2340| -------- | --------------------------- | ---- | ------------------------ | 2341| resId | number | 是 | 资源ID值。 | 2342| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 2343| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 回调函数,返回资源ID值对应的图片资源Base64编码。 | 2344 2345**错误码:** 2346 2347以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2348 2349| 错误码ID | 错误信息 | 2350| -------- | ---------------------------------------- | 2351| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2352| 9001001 | Invalid resource ID. | 2353| 9001002 | No matching resource is found based on the resource ID. | 2354 2355**示例:** 2356 ```ts 2357import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2358import { BusinessError } from '@kit.BasicServicesKit'; 2359 2360export default class EntryAbility extends UIAbility { 2361 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2362 try { 2363 // 'app.media.test'仅作示例,请替换为实际使用的资源 2364 this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120, (error: BusinessError, value: string) => { 2365 if (error != null) { 2366 console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); 2367 } else { 2368 let media = value; 2369 } 2370 }); 2371 } catch (error) { 2372 let code = (error as BusinessError).code; 2373 let message = (error as BusinessError).message; 2374 console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2375 } 2376 } 2377} 2378 ``` 2379 2380### getMediaContentBase64<sup>9+</sup> 2381 2382getMediaContentBase64(resId: number): Promise<string> 2383 2384获取指定资源ID对应的图片资源Base64编码,使用Promise异步回调。 2385 2386**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2387 2388**系统能力:** SystemCapability.Global.ResourceManager 2389 2390**参数:** 2391 2392| 参数名 | 类型 | 必填 | 说明 | 2393| ----- | ------ | ---- | ----- | 2394| resId | number | 是 | 资源ID值。 | 2395 2396**返回值:** 2397 2398| 类型 | 说明 | 2399| --------------------- | -------------------- | 2400| Promise<string> | Promise对象,返回资源ID值对应的图片资源Base64编码。 | 2401 2402**错误码:** 2403 2404以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2405 2406| 错误码ID | 错误信息 | 2407| -------- | ---------------------------------------- | 2408| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 2409| 9001001 | Invalid resource ID. | 2410| 9001002 | No matching resource is found based on the resource ID. | 2411 2412**示例:** 2413 ```ts 2414import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2415import { BusinessError } from '@kit.BasicServicesKit'; 2416 2417export default class EntryAbility extends UIAbility { 2418 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2419 try { 2420 // 'app.media.test'仅作示例,请替换为实际使用的资源 2421 this.context.resourceManager.getMediaContentBase64($r('app.media.test').id).then((value: string) => { 2422 let media = value; 2423 }).catch((error: BusinessError) => { 2424 console.error("getMediaContentBase64 promise error is " + error); 2425 }); 2426 } catch (error) { 2427 let code = (error as BusinessError).code; 2428 let message = (error as BusinessError).message; 2429 console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2430 } 2431 } 2432} 2433 ``` 2434 2435### getMediaContentBase64<sup>10+</sup> 2436 2437getMediaContentBase64(resId: number, density: number): Promise<string> 2438 2439获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用Promise异步回调。 2440 2441**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2442 2443**系统能力:** SystemCapability.Global.ResourceManager 2444 2445**参数:** 2446 2447| 参数名 | 类型 | 必填 | 说明 | 2448| ----- | ------ | ---- | ----- | 2449| resId | number | 是 | 资源ID值。 | 2450| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 2451 2452**返回值:** 2453 2454| 类型 | 说明 | 2455| --------------------- | -------------------- | 2456| Promise<string> | Promise对象,返回资源ID值对应的图片资源Base64编码。 | 2457 2458**错误码:** 2459 2460以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2461 2462| 错误码ID | 错误信息 | 2463| -------- | ---------------------------------------- | 2464| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2465| 9001001 | Invalid resource ID. | 2466| 9001002 | No matching resource is found based on the resource ID. | 2467 2468**示例:** 2469 ```ts 2470import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2471import { BusinessError } from '@kit.BasicServicesKit'; 2472 2473export default class EntryAbility extends UIAbility { 2474 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2475 try { 2476 // 'app.media.test'仅作示例,请替换为实际使用的资源 2477 this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120).then((value: string) => { 2478 let media = value; 2479 }).catch((error: BusinessError) => { 2480 console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); 2481 }); 2482 } catch (error) { 2483 let code = (error as BusinessError).code; 2484 let message = (error as BusinessError).message; 2485 console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2486 } 2487 } 2488} 2489 ``` 2490 2491### getMediaBase64ByName<sup>9+</sup> 2492 2493getMediaBase64ByName(resName: string, callback: _AsyncCallback<string>): void 2494 2495获取指定资源名称对应的图片资源Base64编码,使用callback异步回调。 2496 2497**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2498 2499**系统能力:** SystemCapability.Global.ResourceManager 2500 2501**参数:** 2502 2503| 参数名 | 类型 | 必填 | 说明 | 2504| -------- | --------------------------- | ---- | ------------------------ | 2505| resName | string | 是 | 资源名称。 | 2506| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 回调函数,返回资源名称的图片资源Base64编码。 | 2507 2508**错误码:** 2509 2510以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2511 2512| 错误码ID | 错误信息 | 2513| -------- | ---------------------------------------- | 2514| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 2515| 9001003 | Invalid resource name. | 2516| 9001004 | No matching resource is found based on the resource name. | 2517 2518**示例:** 2519 ```ts 2520import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2521import { BusinessError } from '@kit.BasicServicesKit'; 2522 2523export default class EntryAbility extends UIAbility { 2524 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2525 try { 2526 // "test"仅作示例,请替换为实际使用的资源 2527 this.context.resourceManager.getMediaBase64ByName("test", (error: BusinessError, value: string) => { 2528 if (error != null) { 2529 console.error("error is " + error); 2530 } else { 2531 let media = value; 2532 } 2533 }); 2534 } catch (error) { 2535 let code = (error as BusinessError).code; 2536 let message = (error as BusinessError).message; 2537 console.error(`callback getMediaBase64ByName failed, error code: ${code}, message: ${message}.`); 2538 } 2539 } 2540} 2541 ``` 2542 2543### getMediaBase64ByName<sup>10+</sup> 2544 2545getMediaBase64ByName(resName: string, density: number, callback: _AsyncCallback<string>): void 2546 2547获取指定资源名称对应的指定屏幕密度图片资源Base64编码,使用callback异步回调。 2548 2549> **说明** 2550> 2551> 推荐使用[getMediaBase64ByName](#getmediacontentbase6410)或[getMediaContentBase64](#getmediacontentbase6410)接口,具体请参考[ResourceManager](#resourcemanager)的说明。 2552 2553**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2554 2555**系统能力:** SystemCapability.Global.ResourceManager 2556 2557**参数:** 2558 2559| 参数名 | 类型 | 必填 | 说明 | 2560| -------- | --------------------------- | ---- | ------------------------ | 2561| resName | string | 是 | 资源名称。 | 2562| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 2563| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 回调函数,返回资源名称的图片资源Base64编码。 | 2564 2565**错误码:** 2566 2567以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2568 2569| 错误码ID | 错误信息 | 2570| -------- | ---------------------------------------- | 2571| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2572| 9001003 | Invalid resource name. | 2573| 9001004 | No matching resource is found based on the resource name. | 2574 2575**示例:** 2576 ```ts 2577import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2578import { BusinessError } from '@kit.BasicServicesKit'; 2579 2580export default class EntryAbility extends UIAbility { 2581 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2582 try { 2583 // "test"仅作示例,请替换为实际使用的资源 2584 this.context.resourceManager.getMediaBase64ByName("test", 120, (error: BusinessError, value: string) => { 2585 if (error != null) { 2586 console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); 2587 } else { 2588 let media = value; 2589 } 2590 }); 2591 } catch (error) { 2592 let code = (error as BusinessError).code; 2593 let message = (error as BusinessError).message; 2594 console.error(`callback getMediaBase64ByName failed, error code: ${code}, message: ${message}.`); 2595 } 2596 } 2597} 2598 ``` 2599 2600### getMediaBase64ByName<sup>9+</sup> 2601 2602getMediaBase64ByName(resName: string): Promise<string> 2603 2604获取指定资源名称对应的图片资源Base64编码,使用Promise异步回调。 2605 2606**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2607 2608**系统能力:** SystemCapability.Global.ResourceManager 2609 2610**参数:** 2611 2612| 参数名 | 类型 | 必填 | 说明 | 2613| ------- | ------ | ---- | ---- | 2614| resName | string | 是 | 资源名称。 | 2615 2616**返回值:** 2617 2618| 类型 | 说明 | 2619| --------------------- | ------------------- | 2620| Promise<string> | Promise对象,返回资源名称对应的图片资源Base64编码。 | 2621 2622**错误码:** 2623 2624以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2625 2626| 错误码ID | 错误信息 | 2627| -------- | ---------------------------------------- | 2628| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 2629| 9001003 | Invalid resource name. | 2630| 9001004 | No matching resource is found based on the resource name. | 2631 2632**示例:** 2633 ```ts 2634import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2635import { BusinessError } from '@kit.BasicServicesKit'; 2636 2637export default class EntryAbility extends UIAbility { 2638 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2639 try { 2640 // "test"仅作示例,请替换为实际使用的资源 2641 this.context.resourceManager.getMediaBase64ByName("test").then((value: string) => { 2642 let media = value; 2643 }).catch((error: BusinessError) => { 2644 console.error("getMediaBase64ByName promise error is " + error); 2645 }); 2646 } catch (error) { 2647 let code = (error as BusinessError).code; 2648 let message = (error as BusinessError).message; 2649 console.error(`promise getMediaBase64ByName failed, error code: ${code}, message: ${message}.`); 2650 } 2651 } 2652} 2653 ``` 2654 2655### getMediaBase64ByName<sup>10+</sup> 2656 2657getMediaBase64ByName(resName: string, density: number): Promise<string> 2658 2659获取指定资源名称对应的指定屏幕密度图片资源Base64编码,使用Promise异步回调。 2660 2661**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2662 2663**系统能力:** SystemCapability.Global.ResourceManager 2664 2665**参数:** 2666 2667| 参数名 | 类型 | 必填 | 说明 | 2668| ------- | ------ | ---- | ---- | 2669| resName | string | 是 | 资源名称。 | 2670| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 2671 2672**返回值:** 2673 2674| 类型 | 说明 | 2675| --------------------- | ------------------- | 2676| Promise<string> | Promise对象,返回资源名称对应的图片资源Base64编码。| 2677 2678**错误码:** 2679 2680以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2681 2682| 错误码ID | 错误信息 | 2683| -------- | ---------------------------------------- | 2684| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2685| 9001003 | Invalid resource name. | 2686| 9001004 | No matching resource is found based on the resource name. | 2687 2688**示例:** 2689 ```ts 2690import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2691import { BusinessError } from '@kit.BasicServicesKit'; 2692 2693export default class EntryAbility extends UIAbility { 2694 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2695 try { 2696 // "test"仅作示例,请替换为实际使用的资源 2697 this.context.resourceManager.getMediaBase64ByName("test", 120).then((value: string) => { 2698 let media = value; 2699 }).catch((error: BusinessError) => { 2700 console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); 2701 }); 2702 } catch (error) { 2703 let code = (error as BusinessError).code; 2704 let message = (error as BusinessError).message; 2705 console.error(`promise getMediaBase64ByName failed, error code: ${code}, message: ${message}.`); 2706 } 2707 } 2708} 2709 ``` 2710 2711### getDrawableDescriptor<sup>10+</sup> 2712 2713getDrawableDescriptor(resId: number, density?: number, type?: number): DrawableDescriptor 2714 2715获取指定资源ID对应的DrawableDescriptor对象,用于图标的显示,使用同步方式返回。 2716 2717**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2718 2719**系统能力:** SystemCapability.Global.ResourceManager 2720 2721**参数:** 2722 2723| 参数名 | 类型 | 必填 | 说明 | 2724| ----- | ------ | ---- | ----- | 2725| resId | number | 是 | 资源ID值。 | 2726| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 2727| type<sup>11+</sup> | number | 否 | - 1表示获取主题资源包中应用的分层图标资源。<br> - 0或缺省表示获取应用自身图标资源。 | 2728 2729**返回值:** 2730 2731| 类型 | 说明 | 2732| ------ | ---------- | 2733| [DrawableDescriptor](../apis-arkui/js-apis-arkui-drawableDescriptor.md#drawabledescriptor) | 资源ID值对应的DrawableDescriptor对象。| 2734 2735**错误码:** 2736 2737以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2738 2739| 错误码ID | 错误信息 | 2740| -------- | ---------------------------------------- | 2741| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2742| 9001001 | Invalid resource ID. | 2743| 9001002 | No matching resource is found based on the resource ID. | 2744 2745**示例:** 2746 ```ts 2747import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2748import { BusinessError } from '@kit.BasicServicesKit'; 2749import { DrawableDescriptor } from '@kit.ArkUI'; 2750 2751export default class EntryAbility extends UIAbility { 2752 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2753 try { 2754 // 'app.media.icon'仅作示例,请替换为实际使用的资源 2755 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id); 2756 } catch (error) { 2757 let code = (error as BusinessError).code; 2758 let message = (error as BusinessError).message; 2759 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 2760 } 2761 try { 2762 // 'app.media.icon'仅作示例,请替换为实际使用的资源 2763 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id, 120); 2764 } catch (error) { 2765 let code = (error as BusinessError).code; 2766 let message = (error as BusinessError).message; 2767 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 2768 } 2769 try { 2770 // 'app.media.icon'仅作示例,请替换为实际使用的资源 2771 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id, 0, 1); 2772 } catch (error) { 2773 let code = (error as BusinessError).code; 2774 let message = (error as BusinessError).message; 2775 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 2776 } 2777 } 2778} 2779 ``` 2780 2781### getDrawableDescriptorByName<sup>10+</sup> 2782 2783getDrawableDescriptorByName(resName: string, density?: number, type?: number): DrawableDescriptor 2784 2785获取指定资源名称对应的DrawableDescriptor对象,用于图标的显示,使用同步方式返回。 2786 2787**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2788 2789**系统能力:** SystemCapability.Global.ResourceManager 2790 2791**参数:** 2792 2793| 参数名 | 类型 | 必填 | 说明 | 2794| ------- | ------ | ---- | ---- | 2795| resName | string | 是 | 资源名称。 | 2796| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 2797| type<sup>11+</sup> | number | 否 | - 1表示获取主题资源包中应用的分层图标资源。<br> - 0或缺省表示获取应用自身图标资源。 | 2798 2799**返回值:** 2800 2801| 类型 | 说明 | 2802| ------ | --------- | 2803| [DrawableDescriptor](../apis-arkui/js-apis-arkui-drawableDescriptor.md#drawabledescriptor) | 资源名称对应的DrawableDescriptor对象。 | 2804 2805**错误码:** 2806 2807以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2808 2809| 错误码ID | 错误信息 | 2810| -------- | ---------------------------------------- | 2811| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2812| 9001003 | Invalid resource name. | 2813| 9001004 | No matching resource is found based on the resource name. | 2814 2815**示例:** 2816 ```ts 2817import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2818import { BusinessError } from '@kit.BasicServicesKit'; 2819import { DrawableDescriptor } from '@kit.ArkUI'; 2820 2821export default class EntryAbility extends UIAbility { 2822 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2823 try { 2824 // "icon"仅作示例,请替换为实际使用的资源 2825 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptorByName('icon'); 2826 } catch (error) { 2827 let code = (error as BusinessError).code; 2828 let message = (error as BusinessError).message; 2829 console.error(`getDrawableDescriptorByName failed, error code: ${code}, message: ${message}.`); 2830 } 2831 try { 2832 // "icon"仅作示例,请替换为实际使用的资源 2833 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptorByName('icon', 120); 2834 } catch (error) { 2835 let code = (error as BusinessError).code; 2836 let message = (error as BusinessError).message; 2837 console.error(`getDrawableDescriptorByName failed, error code: ${code}, message: ${message}.`); 2838 } 2839 try { 2840 // "icon"仅作示例,请替换为实际使用的资源 2841 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptorByName('icon', 0, 1); 2842 } catch (error) { 2843 let code = (error as BusinessError).code; 2844 let message = (error as BusinessError).message; 2845 console.error(`getDrawableDescriptorByName failed, error code: ${code}, message: ${message}.`); 2846 } 2847 } 2848} 2849 ``` 2850 2851### getBoolean<sup>9+</sup> 2852 2853getBoolean(resId: number): boolean 2854 2855获取指定资源ID值对应的布尔值,使用同步方式返回。 2856 2857**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2858 2859**系统能力:** SystemCapability.Global.ResourceManager 2860 2861**参数:** 2862 2863| 参数名 | 类型 | 必填 | 说明 | 2864| ----- | ------ | ---- | ----- | 2865| resId | number | 是 | 资源ID值。 | 2866 2867**返回值:** 2868 2869| 类型 | 说明 | 2870| ------- | ------------ | 2871| boolean | 资源ID值对应的布尔值。 | 2872 2873**错误码:** 2874 2875以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2876 2877| 错误码ID | 错误信息 | 2878| -------- | ---------------------------------------- | 2879| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 2880| 9001001 | Invalid resource ID. | 2881| 9001002 | No matching resource is found based on the resource ID. | 2882| 9001006 | The resource is referenced cyclically. | 2883 2884**示例:** 2885 ```json 2886 // 资源文件路径: src/main/resources/base/element/boolean.json 2887 { 2888 "boolean": [ 2889 { 2890 "name": "boolean_test", 2891 "value": true 2892 } 2893 ] 2894 } 2895 ``` 2896 ```ts 2897import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2898import { BusinessError } from '@kit.BasicServicesKit'; 2899 2900export default class EntryAbility extends UIAbility { 2901 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2902 try { 2903 // 'app.boolean.boolean_test'仅作示例,请替换为实际使用的资源 2904 let boolTest = this.context.resourceManager.getBoolean($r('app.boolean.boolean_test').id); 2905 console.log(`getBoolean, result: ${boolTest}`); 2906 // 打印输出结果: getBoolean, result: true 2907 } catch (error) { 2908 let code = (error as BusinessError).code; 2909 let message = (error as BusinessError).message; 2910 console.error(`getBoolean failed, error code: ${code}, message: ${message}.`); 2911 } 2912 } 2913} 2914 ``` 2915 2916### getBooleanByName<sup>9+</sup> 2917 2918getBooleanByName(resName: string): boolean 2919 2920获取指定资源名称对应的布尔值,使用同步方式返回。 2921 2922**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2923 2924**系统能力:** SystemCapability.Global.ResourceManager 2925 2926**参数:** 2927 2928| 参数名 | 类型 | 必填 | 说明 | 2929| ------- | ------ | ---- | ---- | 2930| resName | string | 是 | 资源名称。 | 2931 2932**返回值:** 2933 2934| 类型 | 说明 | 2935| ------- | ----------- | 2936| boolean | 资源名称对应的布尔值。 | 2937 2938**错误码:** 2939 2940以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2941 2942| 错误码ID | 错误信息 | 2943| -------- | ---------------------------------------- | 2944| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 2945| 9001003 | Invalid resource name. | 2946| 9001004 | No matching resource is found based on the resource name. | 2947| 9001006 | The resource is referenced cyclically. | 2948 2949**示例:** 2950 ```json 2951 // 资源文件路径: src/main/resources/base/element/boolean.json 2952 { 2953 "boolean": [ 2954 { 2955 "name": "boolean_test", 2956 "value": true 2957 } 2958 ] 2959 } 2960 ``` 2961 ```ts 2962import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 2963import { BusinessError } from '@kit.BasicServicesKit'; 2964 2965export default class EntryAbility extends UIAbility { 2966 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2967 try { 2968 // "boolean_test"仅作示例,请替换为实际使用的资源 2969 let boolTest = this.context.resourceManager.getBooleanByName("boolean_test"); 2970 console.log(`getBooleanByName, result: ${boolTest}`); 2971 // 打印输出结果: getBooleanByName, result: true 2972 } catch (error) { 2973 let code = (error as BusinessError).code; 2974 let message = (error as BusinessError).message; 2975 console.error(`getBooleanByName failed, error code: ${code}, message: ${message}.`); 2976 } 2977 } 2978} 2979 ``` 2980 2981### getNumber<sup>9+</sup> 2982 2983getNumber(resId: number): number 2984 2985获取指定资源ID对应的integer数值或者float数值,使用同步方式返回。 2986 2987**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 2988 2989**系统能力:** SystemCapability.Global.ResourceManager 2990 2991**参数:** 2992 2993| 参数名 | 类型 | 必填 | 说明 | 2994| ----- | ------ | ---- | ----- | 2995| resId | number | 是 | 资源ID值。 | 2996 2997**返回值:** 2998 2999| 类型 | 说明 | 3000| ------ | ---------- | 3001| number | 资源ID值对应的数值。<br>integer对应的是原数值,float不带单位时对应的是原数值,带"vp","fp"单位时对应的是px值,具体参考示例代码。 | 3002 3003**错误码:** 3004 3005以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3006 3007| 错误码ID | 错误信息 | 3008| -------- | ---------------------------------------- | 3009| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3010| 9001001 | Invalid resource ID. | 3011| 9001002 | No matching resource is found based on the resource ID. | 3012| 9001006 | The resource is referenced cyclically. | 3013 3014**示例:** 3015 ```json 3016 // 资源文件路径: src/main/resources/base/element/integer.json 3017 { 3018 "integer": [ 3019 { 3020 "name": "integer_test", 3021 "value": 100 3022 } 3023 ] 3024 } 3025 ``` 3026 3027 ```json 3028 // 资源文件路径: src/main/resources/base/element/float.json 3029 { 3030 "float": [ 3031 { 3032 "name": "float_test", 3033 "value": "30.6vp" 3034 } 3035 ] 3036 } 3037 ``` 3038 ```ts 3039import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3040import { BusinessError } from '@kit.BasicServicesKit'; 3041import { display } from '@kit.ArkUI'; 3042export default class EntryAbility extends UIAbility { 3043 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3044 try { 3045 // integer对应返回的是原数值 3046 // 'app.integer.integer_test'仅作示例,请替换为实际使用的资源 3047 let intValue = this.context.resourceManager.getNumber($r('app.integer.integer_test').id); 3048 console.log(`getNumber, int value: ${intValue}`); 3049 // 打印输出结果: getNumber, int value: 100 3050 } catch (error) { 3051 let code = (error as BusinessError).code; 3052 let message = (error as BusinessError).message; 3053 console.error(`getNumber failed, error code: ${code}, message: ${message}.`); 3054 } 3055 3056 try { 3057 // float对应返回的是真实像素点值,带"vp","fp"单位的像素值 = 原数值 * densityPixels 3058 // 'app.float.float_test'仅作示例,请替换为实际使用的资源 3059 let floatValue = this.context.resourceManager.getNumber($r('app.float.float_test').id); 3060 console.log(`getNumber, densityPixels: ${display.getDefaultDisplaySync().densityPixels}, float value: ${floatValue}`); 3061 // 打印输出结果: getNumber, densityPixels: 3.25, float value: 99.45000457763672 3062 } catch (error) { 3063 let code = (error as BusinessError).code; 3064 let message = (error as BusinessError).message; 3065 console.error(`getNumber failed, error code: ${code}, message: ${message}.`); 3066 } 3067 } 3068} 3069 ``` 3070 3071 3072### getNumberByName<sup>9+</sup> 3073 3074getNumberByName(resName: string): number 3075 3076获取指定资源名称对应的integer数值或者float数值,使用同步方式返回。 3077 3078**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3079 3080**系统能力:** SystemCapability.Global.ResourceManager 3081 3082**参数:** 3083 3084| 参数名 | 类型 | 必填 | 说明 | 3085| ------- | ------ | ---- | ---- | 3086| resName | string | 是 | 资源名称。 | 3087 3088**返回值:** 3089 3090| 类型 | 说明 | 3091| ------ | --------- | 3092| number | 资源名称对应的数值。<br>integer对应的是原数值,float不带单位时对应的是原数值,带"vp","fp"单位时对应的是px值。 | 3093 3094**错误码:** 3095 3096以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3097 3098| 错误码ID | 错误信息 | 3099| -------- | ---------------------------------------- | 3100| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3101| 9001003 | Invalid resource name. | 3102| 9001004 | No matching resource is found based on the resource name. | 3103| 9001006 | The resource is referenced cyclically. | 3104 3105**示例:** 3106 ```json 3107 // 资源文件路径: src/main/resources/base/element/integer.json 3108 { 3109 "integer": [ 3110 { 3111 "name": "integer_test", 3112 "value": 100 3113 } 3114 ] 3115 } 3116 ``` 3117 3118 ```json 3119 // 资源文件路径: src/main/resources/base/element/float.json 3120 { 3121 "float": [ 3122 { 3123 "name": "float_test", 3124 "value": "30.6vp" 3125 } 3126 ] 3127 } 3128 ``` 3129 ```ts 3130import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3131import { BusinessError } from '@kit.BasicServicesKit'; 3132import { display } from '@kit.ArkUI'; 3133export default class EntryAbility extends UIAbility { 3134 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3135 try { 3136 // integer对应返回的是原数值 3137 // "integer_test"仅作示例,请替换为实际使用的资源 3138 let intValue = this.context.resourceManager.getNumberByName("integer_test"); 3139 console.log(`getNumberByName, int value: ${intValue}`); 3140 // 打印输出结果: getNumberByName, int value: 100 3141 } catch (error) { 3142 let code = (error as BusinessError).code; 3143 let message = (error as BusinessError).message; 3144 console.error(`getNumberByName failed, error code: ${code}, message: ${message}.`); 3145 } 3146 3147 try { 3148 // float对应返回的是真实像素点值,带"vp","fp"单位的像素值 = 原数值 * densityPixels 3149 // "float_test"仅作示例,请替换为实际使用的资源 3150 let floatValue = this.context.resourceManager.getNumberByName("float_test"); 3151 console.log(`getNumberByName, densityPixels: ${display.getDefaultDisplaySync().densityPixels}, float value: ${floatValue}`); 3152 // 打印输出结果: getNumberByName, densityPixels: 3.25, float value: 99.45000457763672 3153 } catch (error) { 3154 let code = (error as BusinessError).code; 3155 let message = (error as BusinessError).message; 3156 console.error(`getNumberByName failed, error code: ${code}, message: ${message}.`); 3157 } 3158 } 3159} 3160 ``` 3161 3162### getColorSync<sup>10+</sup> 3163 3164getColorSync(resId: number) : number 3165 3166获取指定资源ID对应的颜色值,使用同步方式返回。 3167 3168**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3169 3170**系统能力:** SystemCapability.Global.ResourceManager 3171 3172**参数:** 3173 3174| 参数名 | 类型 | 必填 | 说明 | 3175| ----- | ------ | ---- | ----- | 3176| resId | number | 是 | 资源ID值。 | 3177 3178**返回值:** 3179 3180| 类型 | 说明 | 3181| ------ | ----------- | 3182| number | 资源ID值对应的颜色值(十进制)。 | 3183 3184**错误码:** 3185 3186以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3187 3188| 错误码ID | 错误信息 | 3189| -------- | ---------------------------------------- | 3190| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3191| 9001001 | Invalid resource ID. | 3192| 9001002 | No matching resource is found based on the resource ID. | 3193| 9001006 | The resource is referenced cyclically. | 3194 3195**示例:** 3196 ```json 3197 // 资源文件路径: src/main/resources/base/element/color.json 3198 { 3199 "color": [ 3200 { 3201 "name": "test", 3202 "value": "#FFFFFF" 3203 } 3204 ] 3205 } 3206 ``` 3207 ```ts 3208import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3209import { BusinessError } from '@kit.BasicServicesKit'; 3210export default class EntryAbility extends UIAbility { 3211 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3212 try { 3213 // 'app.color.test'仅作示例,请替换为实际使用的资源 3214 let colorValue = this.context.resourceManager.getColorSync($r('app.color.test').id); 3215 console.log(`getColorSync, result: ${colorValue}`); 3216 // 打印输出结果: getColorSync, result: 4294967295 3217 } catch (error) { 3218 let code = (error as BusinessError).code; 3219 let message = (error as BusinessError).message; 3220 console.error(`getColorSync failed, error code: ${code}, message: ${message}.`); 3221 } 3222 } 3223} 3224 ``` 3225 3226 3227### getColorByNameSync<sup>10+</sup> 3228 3229getColorByNameSync(resName: string) : number 3230 3231获取指定资源名称对应的颜色值,使用同步方式返回。 3232 3233**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3234 3235**系统能力:** SystemCapability.Global.ResourceManager 3236 3237**参数:** 3238 3239| 参数名 | 类型 | 必填 | 说明 | 3240| ------- | ------ | ---- | ---- | 3241| resName | string | 是 | 资源名称。 | 3242 3243**返回值:** 3244 3245| 类型 | 说明 | 3246| ------ | ---------- | 3247| number | 资源名称对应的颜色值(十进制)。 | 3248 3249**错误码:** 3250 3251以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3252 3253| 错误码ID | 错误信息 | 3254| -------- | ---------------------------------------- | 3255| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3256| 9001003 | Invalid resource name. | 3257| 9001004 | No matching resource is found based on the resource name. | 3258| 9001006 | The resource is referenced cyclically. | 3259 3260**示例:** 3261 ```json 3262 // 资源文件路径: src/main/resources/base/element/color.json 3263 { 3264 "color": [ 3265 { 3266 "name": "test", 3267 "value": "#FFFFFF" 3268 } 3269 ] 3270 } 3271 ``` 3272 ```ts 3273import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3274import { BusinessError } from '@kit.BasicServicesKit'; 3275export default class EntryAbility extends UIAbility { 3276 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3277 try { 3278 // "test"仅作示例,请替换为实际使用的资源 3279 let colorValue = this.context.resourceManager.getColorByNameSync("test"); 3280 console.log(`getColorByNameSync, result: ${colorValue}`); 3281 // 打印输出结果: getColorByNameSync, result: 4294967295 3282 } catch (error) { 3283 let code = (error as BusinessError).code; 3284 let message = (error as BusinessError).message; 3285 console.error(`getColorByNameSync failed, error code: ${code}, message: ${message}.`); 3286 } 3287 } 3288} 3289 ``` 3290 3291### getColor<sup>10+</sup> 3292 3293getColor(resId: number, callback: _AsyncCallback<number>): void 3294 3295获取指定资源ID对应的颜色值,使用callback异步回调。 3296 3297**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3298 3299**系统能力:** SystemCapability.Global.ResourceManager 3300 3301**参数:** 3302 3303| 参数名 | 类型 | 必填 | 说明 | 3304| -------- | --------------------------- | ---- | --------------- | 3305| resId | number | 是 | 资源ID值。 | 3306| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<number> | 是 | 回调函数,返回资源ID值对应的颜色值(十进制)。 | 3307 3308**错误码:** 3309 3310以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3311 3312| 错误码ID | 错误信息 | 3313| -------- | ---------------------------------------- | 3314| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3315| 9001001 | Invalid resource ID. | 3316| 9001002 | No matching resource is found based on the resource ID. | 3317| 9001006 | The resource is referenced cyclically. | 3318 3319**示例Stage:** 3320 ```json 3321 // 资源文件路径: src/main/resources/base/element/color.json 3322 { 3323 "color": [ 3324 { 3325 "name": "test", 3326 "value": "#FFFFFF" 3327 } 3328 ] 3329 } 3330 ``` 3331 ```ts 3332import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3333import { BusinessError } from '@kit.BasicServicesKit'; 3334export default class EntryAbility extends UIAbility { 3335 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3336 // 'app.color.test'仅作示例,请替换为实际使用的资源 3337 this.context.resourceManager.getColor($r('app.color.test').id, (error: BusinessError, value: number) => { 3338 if (error != null) { 3339 console.error(`callback getColor failed, error code: ${error.code}, message: ${error.message}.`); 3340 } else { 3341 console.log(`getColor, result: ${value}`); 3342 // 打印输出结果: getColor, result: 4294967295 3343 } 3344 }); 3345 } 3346} 3347 ``` 3348 3349### getColor<sup>10+</sup> 3350 3351getColor(resId: number): Promise<number> 3352 3353获取指定资源ID对应的颜色值,使用Promise异步回调。 3354 3355**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3356 3357**系统能力:** SystemCapability.Global.ResourceManager 3358 3359**参数:** 3360 3361| 参数名 | 类型 | 必填 | 说明 | 3362| ----- | ------ | ---- | ----- | 3363| resId | number | 是 | 资源ID值。 | 3364 3365**返回值:** 3366 3367| 类型 | 说明 | 3368| --------------------- | ----------- | 3369| Promise<number> | Promise对象,返回资源ID值对应的颜色值(十进制)。 | 3370 3371**错误码:** 3372 3373以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3374 3375| 错误码ID | 错误信息 | 3376| -------- | ---------------------------------------- | 3377| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3378| 9001001 | Invalid resource ID. | 3379| 9001002 | No matching resource is found based on the resource ID. | 3380| 9001006 | The resource is referenced cyclically. | 3381 3382**示例:** 3383 ```json 3384 // 资源文件路径: src/main/resources/base/element/color.json 3385 { 3386 "color": [ 3387 { 3388 "name": "test", 3389 "value": "#FFFFFF" 3390 } 3391 ] 3392 } 3393 ``` 3394 ```ts 3395import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3396import { BusinessError } from '@kit.BasicServicesKit'; 3397export default class EntryAbility extends UIAbility { 3398 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3399 // 'app.color.test'仅作示例,请替换为实际使用的资源 3400 this.context.resourceManager.getColor($r('app.color.test').id) 3401 .then((value: number) => { 3402 console.log(`getColor, result: ${value}`); 3403 // 打印输出结果: getColor, result: 4294967295 3404 }) 3405 .catch((error: BusinessError) => { 3406 console.error(`promise getColor failed, error code: ${error.code}, message: ${error.message}.`); 3407 }); 3408 } 3409} 3410 ``` 3411 3412 3413### getColorByName<sup>10+</sup> 3414 3415getColorByName(resName: string, callback: _AsyncCallback<number>): void 3416 3417获取指定资源名称对应的颜色值,使用callback异步回调。 3418 3419**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3420 3421**系统能力:** SystemCapability.Global.ResourceManager 3422 3423**参数:** 3424 3425| 参数名 | 类型 | 必填 | 说明 | 3426| -------- | --------------------------- | ---- | --------------- | 3427| resName | string | 是 | 资源名称。 | 3428| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<number> | 是 | 回调函数,返回资源名称对应的颜色值(十进制)。 | 3429 3430**错误码:** 3431 3432以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3433 3434| 错误码ID | 错误信息 | 3435| -------- | ---------------------------------------- | 3436| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3437| 9001003 | Invalid resource name. | 3438| 9001004 | No matching resource is found based on the resource name. | 3439| 9001006 | The resource is referenced cyclically. | 3440 3441**示例:** 3442 ```json 3443 // 资源文件路径: src/main/resources/base/element/color.json 3444 { 3445 "color": [ 3446 { 3447 "name": "test", 3448 "value": "#FFFFFF" 3449 } 3450 ] 3451 } 3452 ``` 3453 ```ts 3454import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3455import { BusinessError } from '@kit.BasicServicesKit'; 3456export default class EntryAbility extends UIAbility { 3457 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3458 // "test"仅作示例,请替换为实际使用的资源 3459 this.context.resourceManager.getColorByName("test", (error: BusinessError, value: number) => { 3460 if (error != null) { 3461 console.error(`callback getColorByName failed, error code: ${error.code}, message: ${error.message}.`); 3462 } else { 3463 console.log(`getColorByName, result: ${value}`); 3464 // 打印输出结果: getColorByName, result: 4294967295 3465 } 3466 }); 3467 } 3468} 3469 ``` 3470 3471### getColorByName<sup>10+</sup> 3472 3473getColorByName(resName: string): Promise<number> 3474 3475获取指定资源名称对应的颜色值,使用Promise异步回调。 3476 3477**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3478 3479**系统能力:** SystemCapability.Global.ResourceManager 3480 3481**参数:** 3482 3483| 参数名 | 类型 | 必填 | 说明 | 3484| ------- | ------ | ---- | ---- | 3485| resName | string | 是 | 资源名称。 | 3486 3487**返回值:** 3488 3489| 类型 | 说明 | 3490| --------------------- | ---------- | 3491| Promise<number> | Promise对象,返回资源名称对应的颜色值(十进制)。 | 3492 3493**错误码:** 3494 3495以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3496 3497| 错误码ID | 错误信息 | 3498| -------- | ---------------------------------------- | 3499| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3500| 9001003 | Invalid resource name. | 3501| 9001004 | No matching resource is found based on the resource name. | 3502| 9001006 | The resource is referenced cyclically. | 3503 3504**示例:** 3505 ```json 3506 // 资源文件路径: src/main/resources/base/element/color.json 3507 { 3508 "color": [ 3509 { 3510 "name": "test", 3511 "value": "#FFFFFF" 3512 } 3513 ] 3514 } 3515 ``` 3516 ```ts 3517import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3518import { BusinessError } from '@kit.BasicServicesKit'; 3519export default class EntryAbility extends UIAbility { 3520 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3521 // "test"仅作示例,请替换为实际使用的资源 3522 this.context.resourceManager.getColorByName("test") 3523 .then((value: number) => { 3524 console.log(`getColorByName, result: ${value}`); 3525 // 打印输出结果: getColorByName, result: 4294967295 3526 }) 3527 .catch((error: BusinessError) => { 3528 console.error(`promise getColorByName failed, error code: ${error.code}, message: ${error.message}.`); 3529 }); 3530 } 3531} 3532 ``` 3533 3534### getRawFileContentSync<sup>10+</sup> 3535 3536getRawFileContentSync(path: string): Uint8Array 3537 3538获取resources/rawfile目录下对应的rawfile文件内容,使用同步形式返回。 3539 3540**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3541 3542**系统能力:** SystemCapability.Global.ResourceManager 3543 3544**参数:** 3545 3546| 参数名 | 类型 | 必填 | 说明 | 3547| -------- | ------------------------------- | ---- | ----------------------- | 3548| path | string | 是 | rawfile文件路径。 | 3549 3550**返回值:** 3551 3552| 类型 | 说明 | 3553| --------------------- | ---------- | 3554| Uint8Array | 返回获取的rawfile文件内容。 | 3555 3556**错误码:** 3557 3558以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3559 3560| 错误码ID | 错误信息 | 3561| -------- | ---------------------------------------- | 3562| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3563| 9001005 | Invalid relative path. | 3564 3565**示例:** 3566 ```ts 3567import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3568import { BusinessError } from '@kit.BasicServicesKit'; 3569export default class EntryAbility extends UIAbility { 3570 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3571 try { 3572 // "test.txt"仅作示例,请替换为实际使用的资源 3573 this.context.resourceManager.getRawFileContentSync("test.txt"); 3574 } catch (error) { 3575 let code = (error as BusinessError).code; 3576 let message = (error as BusinessError).message; 3577 console.error(`getRawFileContentSync failed, error code: ${code}, message: ${message}.`); 3578 } 3579 } 3580} 3581 ``` 3582 3583### getRawFileContent<sup>9+</sup> 3584 3585getRawFileContent(path: string, callback: _AsyncCallback<Uint8Array>): void 3586 3587获取resources/rawfile目录下对应的rawfile文件内容,使用callback异步回调。 3588 3589**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3590 3591**系统能力:** SystemCapability.Global.ResourceManager 3592 3593**参数:** 3594 3595| 参数名 | 类型 | 必填 | 说明 | 3596| -------- | ------------------------------- | ---- | ----------------------- | 3597| path | string | 是 | rawfile文件路径。 | 3598| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Uint8Array> | 是 | 回调函数,返回获取的rawfile文件内容。 | 3599 3600**错误码:** 3601 3602以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3603 3604| 错误码ID | 错误信息 | 3605| -------- | ---------------------------------------- | 3606| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3607| 9001005 | Invalid relative path. | 3608 3609**示例:** 3610 ```ts 3611import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3612import { BusinessError } from '@kit.BasicServicesKit'; 3613export default class EntryAbility extends UIAbility { 3614 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3615 try { 3616 // "test.txt"仅作示例,请替换为实际使用的资源 3617 this.context.resourceManager.getRawFileContent("test.txt", (error: BusinessError, value: Uint8Array) => { 3618 if (error != null) { 3619 console.error("error is " + error); 3620 } else { 3621 let rawFile = value; 3622 } 3623 }); 3624 } catch (error) { 3625 let code = (error as BusinessError).code; 3626 let message = (error as BusinessError).message; 3627 console.error(`callback getRawFileContent failed, error code: ${code}, message: ${message}.`); 3628 } 3629 } 3630} 3631 ``` 3632 3633### getRawFileContent<sup>9+</sup> 3634 3635getRawFileContent(path: string): Promise<Uint8Array> 3636 3637获取resources/rawfile目录下对应的rawfile文件内容,使用Promise异步回调。 3638 3639**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3640 3641**系统能力:** SystemCapability.Global.ResourceManager 3642 3643**参数:** 3644 3645| 参数名 | 类型 | 必填 | 说明 | 3646| ---- | ------ | ---- | ----------- | 3647| path | string | 是 | rawfile文件路径。 | 3648 3649**返回值:** 3650 3651| 类型 | 说明 | 3652| ------------------------- | ----------- | 3653| Promise<Uint8Array> | Promise对象,返回获取的rawfile文件内容。 | 3654 3655**错误码:** 3656 3657以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3658 3659| 错误码ID | 错误信息 | 3660| -------- | ---------------------------------------- | 3661| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3662| 9001005 | Invalid relative path. | 3663 3664**示例:** 3665 ```ts 3666import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3667import { BusinessError } from '@kit.BasicServicesKit'; 3668export default class EntryAbility extends UIAbility { 3669 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3670 try { 3671 // "test.txt"仅作示例,请替换为实际使用的资源 3672 this.context.resourceManager.getRawFileContent("test.txt").then((value: Uint8Array) => { 3673 let rawFile = value; 3674 }).catch((error: BusinessError) => { 3675 console.error("getRawFileContent promise error is " + error); 3676 }); 3677 } catch (error) { 3678 let code = (error as BusinessError).code; 3679 let message = (error as BusinessError).message; 3680 console.error(`promise getRawFileContent failed, error code: ${code}, message: ${message}.`); 3681 } 3682 } 3683} 3684 ``` 3685 3686### getRawFileListSync<sup>10+</sup> 3687 3688getRawFileListSync(path: string): Array\<string> 3689 3690获取resources/rawfile目录下文件夹及文件列表,使用同步形式返回。 3691 3692>**说明** 3693> 3694> 若文件夹中无文件,则抛出异常;若文件夹中有文件,则返回文件夹及文件列表。 3695 3696**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3697 3698**系统能力:** SystemCapability.Global.ResourceManager 3699 3700**参数:** 3701 3702| 参数名 | 类型 | 必填 | 说明 | 3703| -------- | ------------------------------- | ---- | ----------------------- | 3704| path | string | 是 | rawfile文件夹路径。 | 3705 3706**返回值:** 3707 3708| 类型 | 说明 | 3709| ------------------------- | ----------- | 3710| Array\<string> | rawfile文件目录下的文件夹及文件列表。 | 3711 3712**错误码:** 3713 3714以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3715 3716| 错误码ID | 错误信息 | 3717| -------- | ---------------------------------------- | 3718| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3719| 9001005 | Invalid relative path. | 3720 3721**示例:** 3722 ```ts 3723import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3724import { BusinessError } from '@kit.BasicServicesKit'; 3725export default class EntryAbility extends UIAbility { 3726 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3727 try { 3728 // 传入""表示获取rawfile根目录下的文件列表,假设rawfile根目录下存在test.txt文件 3729 // 传入""仅作示例,请替换为rawfile目录下实际的文件路径 3730 let fileList: Array<string> = this.context.resourceManager.getRawFileListSync(""); 3731 console.log(`getRawFileListSync, result: ${JSON.stringify(fileList)}`); 3732 // 打印输出结果: getRawFileListSync, result: ["test.txt"] 3733 } catch (error) { 3734 let code = (error as BusinessError).code; 3735 let message = (error as BusinessError).message; 3736 console.error(`getRawFileListSync failed, error code: ${code}, message: ${message}.`); 3737 } 3738 } 3739} 3740 ``` 3741 3742### getRawFileList<sup>10+</sup> 3743 3744getRawFileList(path: string, callback: _AsyncCallback<Array\<string\>>): void 3745 3746获取resources/rawfile目录下文件夹及文件列表,使用callback异步回调。 3747 3748>**说明** 3749> 3750> 若文件夹中无文件,则抛出异常;若文件夹中有文件,则返回文件夹及文件列表。 3751 3752**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3753 3754**系统能力:** SystemCapability.Global.ResourceManager 3755 3756**参数:** 3757 3758| 参数名 | 类型 | 必填 | 说明 | 3759| -------- | ------------------------------- | ---- | ----------------------- | 3760| path | string | 是 | rawfile文件夹路径。 | 3761| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Array\<string\>> | 是 | 回调函数,返回rawfile文件目录下的文件夹及文件列表。 | 3762 3763**错误码:** 3764 3765以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3766 3767| 错误码ID | 错误信息 | 3768| -------- | ---------------------------------------- | 3769| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3770| 9001005 | Invalid relative path. | 3771 3772**示例:** 3773 ```ts 3774import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3775import { BusinessError } from '@kit.BasicServicesKit'; 3776export default class EntryAbility extends UIAbility { 3777 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3778 // 传入""表示获取rawfile根目录下的文件列表,假设rawfile根目录下存在test.txt文件 3779 // 传入""仅作示例,请替换为rawfile目录下实际的文件路径 3780 this.context.resourceManager.getRawFileList("", (error: BusinessError, value: Array<string>) => { 3781 if (error != null) { 3782 console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); 3783 } else { 3784 console.log(`getRawFileListSync, result: ${JSON.stringify(value)}`); 3785 // 打印输出结果: getRawFileListSync, result: ["test.txt"] 3786 } 3787 }); 3788 } 3789} 3790 ``` 3791 3792### getRawFileList<sup>10+</sup> 3793 3794getRawFileList(path: string): Promise<Array\<string\>> 3795 3796获取resources/rawfile目录下文件夹及文件列表,使用Promise异步回调。 3797 3798>**说明** 3799> 3800> 若文件夹中无文件,则抛出异常;若文件夹中有文件,则返回文件夹及文件列表。 3801 3802**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3803 3804**系统能力:** SystemCapability.Global.ResourceManager 3805 3806**参数:** 3807 3808| 参数名 | 类型 | 必填 | 说明 | 3809| ---- | ------ | ---- | ----------- | 3810| path | string | 是 | rawfile文件夹路径。 | 3811 3812**返回值:** 3813 3814| 类型 | 说明 | 3815| ------------------------- | ----------- | 3816| Promise<Array\<string\>> | Promise对象,返回rawfile文件目录下的文件夹及文件列表。 | 3817 3818**错误码:** 3819 3820以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3821 3822| 错误码ID | 错误信息 | 3823| -------- | ---------------------------------------- | 3824| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3825| 9001005 | Invalid relative path. | 3826 3827**示例:** 3828 ```ts 3829import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3830import { BusinessError } from '@kit.BasicServicesKit'; 3831export default class EntryAbility extends UIAbility { 3832 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3833 // 传入""表示获取rawfile根目录下的文件列表,假设rawfile根目录下存在test.txt文件 3834 // 传入""仅作示例,请替换为rawfile目录下实际的文件路径 3835 this.context.resourceManager.getRawFileList("") 3836 .then((value: Array<string>) => { 3837 console.log(`getRawFileListSync, result: ${JSON.stringify(value)}`); 3838 // 打印输出结果: getRawFileListSync, result: ["test.txt"] 3839 }) 3840 .catch((error: BusinessError) => { 3841 console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); 3842 }); 3843 } 3844} 3845 ``` 3846 3847### getRawFdSync<sup>10+</sup> 3848 3849getRawFdSync(path: string): RawFileDescriptor 3850 3851获取resources/rawfile目录下rawfile文件所在HAP的文件描述符(fd)。 3852 3853> **说明** 3854> 3855> 文件描述符(fd)使用完毕后需调用[closeRawFdSync](#closerawfdsync10)或[closeRawFd](#closerawfd9)关闭fd,避免资源泄露。 3856 3857**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3858 3859**系统能力:** SystemCapability.Global.ResourceManager 3860 3861**参数:** 3862 3863| 参数名 | 类型 | 必填 | 说明 | 3864| -------- | ---------------------------------------- | ---- | -------------------------------- | 3865| path | string | 是 | rawfile文件路径。 | 3866 3867**返回值:** 3868 3869| 类型 | 说明 | 3870| ------------------------- | ----------- | 3871| [RawFileDescriptor](#rawfiledescriptor9) | rawfile文件所在HAP的文件描述符(fd)。 | 3872 3873**错误码:** 3874 3875以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3876 3877| 错误码ID | 错误信息 | 3878| -------- | ---------------------------------------- | 3879| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3880| 9001005 | Invalid relative path. | 3881 3882**示例:** 3883 ```ts 3884import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3885import { BusinessError } from '@kit.BasicServicesKit'; 3886export default class EntryAbility extends UIAbility { 3887 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3888 try { 3889 // "test.txt"仅作示例,请替换为实际使用的资源 3890 this.context.resourceManager.getRawFdSync("test.txt"); 3891 } catch (error) { 3892 let code = (error as BusinessError).code; 3893 let message = (error as BusinessError).message; 3894 console.error(`getRawFdSync failed, error code: ${code}, message: ${message}.`); 3895 } 3896 } 3897} 3898 ``` 3899 3900### getRawFd<sup>9+</sup> 3901 3902getRawFd(path: string, callback: _AsyncCallback<RawFileDescriptor>): void 3903 3904获取resources/rawfile目录下对应rawfile文件所在HAP的文件描述符(fd)。 3905 3906> **说明** 3907> 3908> 文件描述符(fd)使用完毕后需调用[closeRawFdSync](#closerawfdsync10)或[closeRawFd](#closerawfd9)关闭fd,避免资源泄露。 3909 3910**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3911 3912**系统能力:** SystemCapability.Global.ResourceManager 3913 3914**参数:** 3915 3916| 参数名 | 类型 | 必填 | 说明 | 3917| -------- | ---------------------------------------- | ---- | -------------------------------- | 3918| path | string | 是 | rawfile文件路径。 | 3919| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<[RawFileDescriptor](#rawfiledescriptor9)> | 是 | 回调函数,返回的rawfile文件所在HAP的文件描述符(fd)。 | 3920 3921**错误码:** 3922 3923以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3924 3925| 错误码ID | 错误信息 | 3926| -------- | ---------------------------------------- | 3927| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3928| 9001005 | Invalid relative path. | 3929 3930**示例:** 3931 ```ts 3932import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3933import { BusinessError } from '@kit.BasicServicesKit'; 3934import { resourceManager } from '@kit.LocalizationKit'; 3935export default class EntryAbility extends UIAbility { 3936 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3937 try { 3938 // "test.txt"仅作示例,请替换为实际使用的资源 3939 this.context.resourceManager.getRawFd("test.txt", (error: BusinessError, value: resourceManager.RawFileDescriptor) => { 3940 if (error != null) { 3941 console.error(`callback getRawFd failed error code: ${error.code}, message: ${error.message}.`); 3942 } else { 3943 let fd = value.fd; 3944 let offset = value.offset; 3945 let length = value.length; 3946 } 3947 }); 3948 } catch (error) { 3949 let code = (error as BusinessError).code; 3950 let message = (error as BusinessError).message; 3951 console.error(`callback getRawFd failed, error code: ${code}, message: ${message}.`); 3952 } 3953 } 3954} 3955 ``` 3956 3957### getRawFd<sup>9+</sup> 3958 3959getRawFd(path: string): Promise<RawFileDescriptor> 3960 3961获取resources/rawfile目录下rawfile文件所在HAP的文件描述符(fd)。 3962 3963> **说明** 3964> 3965> 文件描述符(fd)使用完毕后需调用[closeRawFdSync](#closerawfdsync10)或[closeRawFd](#closerawfd9)关闭fd,避免资源泄露。 3966 3967**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 3968 3969**系统能力:** SystemCapability.Global.ResourceManager 3970 3971**参数:** 3972 3973| 参数名 | 类型 | 必填 | 说明 | 3974| ---- | ------ | ---- | ----------- | 3975| path | string | 是 | rawfile文件路径。 | 3976 3977**返回值:** 3978 3979| 类型 | 说明 | 3980| ---------------------------------------- | ------------------- | 3981| Promise<[RawFileDescriptor](#rawfiledescriptor9)> | Promise对象,返回rawfile文件所在HAP的文件描述符(fd)。 | 3982 3983**错误码:** 3984 3985以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3986 3987| 错误码ID | 错误信息 | 3988| -------- | ---------------------------------------- | 3989| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3990| 9001005 | Invalid relative path. | 3991 3992**示例:** 3993 ```ts 3994import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 3995import { BusinessError } from '@kit.BasicServicesKit'; 3996import { resourceManager } from '@kit.LocalizationKit'; 3997export default class EntryAbility extends UIAbility { 3998 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 3999 try { 4000 // "test.txt"仅作示例,请替换为实际使用的资源 4001 this.context.resourceManager.getRawFd("test.txt").then((value: resourceManager.RawFileDescriptor) => { 4002 let fd = value.fd; 4003 let offset = value.offset; 4004 let length = value.length; 4005 }).catch((error: BusinessError) => { 4006 console.error(`promise getRawFd error error code: ${error.code}, message: ${error.message}.`); 4007 }); 4008 } catch (error) { 4009 let code = (error as BusinessError).code; 4010 let message = (error as BusinessError).message; 4011 console.error(`promise getRawFd failed, error code: ${code}, message: ${message}.`); 4012 } 4013 } 4014} 4015 ``` 4016 4017### closeRawFdSync<sup>10+</sup> 4018 4019closeRawFdSync(path: string): void 4020 4021关闭resources/rawfile目录下rawfile文件所在HAP的文件描述符(fd)。 4022 4023**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4024 4025**系统能力:** SystemCapability.Global.ResourceManager 4026 4027**参数:** 4028 4029| 参数名 | 类型 | 必填 | 说明 | 4030| -------- | ------------------------- | ---- | ----------- | 4031| path | string | 是 | rawfile文件路径 。| 4032 4033**错误码:** 4034 4035以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 4036 4037| 错误码ID | 错误信息 | 4038| -------- | ---------------------------------------- | 4039| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4040| 9001005 | Invalid relative path. | 4041 4042**示例:** 4043 ```ts 4044import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4045import { BusinessError } from '@kit.BasicServicesKit'; 4046export default class EntryAbility extends UIAbility { 4047 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4048 try { 4049 // "test.txt"仅作示例,请替换为实际使用的资源 4050 this.context.resourceManager.closeRawFdSync("test.txt"); 4051 } catch (error) { 4052 let code = (error as BusinessError).code; 4053 let message = (error as BusinessError).message; 4054 console.error(`closeRawFd failed, error code: ${code}, message: ${message}.`); 4055 } 4056 } 4057} 4058 ``` 4059 4060### closeRawFd<sup>9+</sup> 4061 4062closeRawFd(path: string, callback: _AsyncCallback<void>): void 4063 4064关闭resources/rawfile目录下rawfile文件所在HAP的文件描述符(fd),使用callback异步回调。 4065 4066**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4067 4068**系统能力:** SystemCapability.Global.ResourceManager 4069 4070**参数:** 4071 4072| 参数名 | 类型 | 必填 | 说明 | 4073| -------- | ------------------------- | ---- | ----------- | 4074| path | string | 是 | rawfile文件路径。 | 4075| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<void> | 是 | 回调函数。当关闭rawfile所在HAP的文件描述符(fd)成功,err为undefined,否则为错误对象。| 4076 4077**错误码:** 4078 4079以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 4080 4081| 错误码ID | 错误信息 | 4082| -------- | ---------------------------------------- | 4083| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4084| 9001005 | Invalid relative path. | 4085 4086**示例:** 4087 ```ts 4088import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4089import { BusinessError } from '@kit.BasicServicesKit'; 4090export default class EntryAbility extends UIAbility { 4091 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4092 try { 4093 // "test.txt"仅作示例,请替换为实际使用的资源 4094 this.context.resourceManager.closeRawFd("test.txt", (error: BusinessError) => { 4095 if (error != null) { 4096 console.error("error is " + error); 4097 } 4098 }); 4099 } catch (error) { 4100 let code = (error as BusinessError).code; 4101 let message = (error as BusinessError).message; 4102 console.error(`callback closeRawFd failed, error code: ${code}, message: ${message}.`); 4103 } 4104 } 4105} 4106 ``` 4107 4108### closeRawFd<sup>9+</sup> 4109 4110closeRawFd(path: string): Promise<void> 4111 4112关闭resources/rawfile目录下rawfile文件所在HAP的文件描述符(fd),使用Promise异步回调。 4113 4114**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4115 4116**系统能力:** SystemCapability.Global.ResourceManager 4117 4118**参数:** 4119 4120| 参数名 | 类型 | 必填 | 说明 | 4121| ---- | ------ | ---- | ----------- | 4122| path | string | 是 | rawfile文件路径。 | 4123 4124**返回值:** 4125 4126| 类型 | 说明 | 4127| ------------------- | ---- | 4128| Promise<void> | Promise对象。无返回结果的Promise对象。 | 4129 4130**错误码:** 4131 4132以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 4133 4134| 错误码ID | 错误信息 | 4135| -------- | ---------------------------------------- | 4136| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4137| 9001005 | Invalid relative path. | 4138 4139**示例:** 4140 ```ts 4141import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4142import { BusinessError } from '@kit.BasicServicesKit'; 4143export default class EntryAbility extends UIAbility { 4144 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4145 try { 4146 // "test.txt"仅作示例,请替换为实际使用的资源 4147 this.context.resourceManager.closeRawFd("test.txt"); 4148 } catch (error) { 4149 let code = (error as BusinessError).code; 4150 let message = (error as BusinessError).message; 4151 console.error(`promise closeRawFd failed, error code: ${code}, message: ${message}.`); 4152 } 4153 } 4154} 4155 ``` 4156 4157### getConfigurationSync<sup>10+</sup> 4158 4159getConfigurationSync(): Configuration 4160 4161获取设备的Configuration,使用同步形式返回。 4162 4163**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4164 4165**系统能力:** SystemCapability.Global.ResourceManager 4166 4167**返回值:** 4168 4169| 类型 | 说明 | 4170| ---------------------------------------- | ---------------- | 4171| [Configuration](#configuration) | 设备的Configuration。 | 4172 4173**示例:** 4174 ```ts 4175import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4176export default class EntryAbility extends UIAbility { 4177 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4178 try { 4179 let value = this.context.resourceManager.getConfigurationSync(); 4180 let direction = value.direction; 4181 let locale = value.locale; 4182 } catch (error) { 4183 console.error("getConfigurationSync error is " + error); 4184 } 4185 } 4186} 4187 ``` 4188 4189### getConfiguration 4190 4191getConfiguration(callback: _AsyncCallback<Configuration>): void 4192 4193获取设备的Configuration,使用callback异步回调。 4194 4195**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4196 4197**系统能力:** SystemCapability.Global.ResourceManager 4198 4199**参数:** 4200 4201| 参数名 | 类型 | 必填 | 说明 | 4202| -------- | ---------------------------------------- | ---- | ------------------------- | 4203| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<[Configuration](#configuration)> | 是 | 回调函数,返回设备的Configuration。 | 4204 4205**示例:** 4206 ```ts 4207import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4208import { BusinessError } from '@kit.BasicServicesKit'; 4209import { resourceManager } from '@kit.LocalizationKit'; 4210 4211export default class EntryAbility extends UIAbility { 4212 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4213 try { 4214 this.context.resourceManager.getConfiguration((error: BusinessError, value: resourceManager.Configuration) => { 4215 if (error != null) { 4216 console.error("getConfiguration callback error is " + error); 4217 } else { 4218 let direction = value.direction; 4219 let locale = value.locale; 4220 } 4221 }); 4222 } catch (error) { 4223 console.error("getConfiguration callback error is " + error); 4224 } 4225 } 4226} 4227 ``` 4228 4229### getConfiguration 4230 4231getConfiguration(): Promise<Configuration> 4232 4233获取设备的Configuration,使用Promise异步回调。 4234 4235**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4236 4237**系统能力:** SystemCapability.Global.ResourceManager 4238 4239**返回值:** 4240 4241| 类型 | 说明 | 4242| ---------------------------------------- | ---------------- | 4243| Promise<[Configuration](#configuration)> | Promise对象,返回设备的Configuration。 | 4244 4245**示例:** 4246 ```ts 4247import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4248import { BusinessError } from '@kit.BasicServicesKit'; 4249import { resourceManager } from '@kit.LocalizationKit'; 4250 4251export default class EntryAbility extends UIAbility { 4252 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4253 try { 4254 this.context.resourceManager.getConfiguration().then((value: resourceManager.Configuration) => { 4255 let direction = value.direction; 4256 let locale = value.locale; 4257 }).catch((error: BusinessError) => { 4258 console.error("getConfiguration promise error is " + error); 4259 }); 4260 } catch (error) { 4261 console.error("getConfiguration promise error is " + error); 4262 } 4263 } 4264} 4265 ``` 4266 4267### getDeviceCapabilitySync<sup>10+</sup> 4268 4269getDeviceCapabilitySync(): DeviceCapability 4270 4271获取设备的DeviceCapability,使用同步形式返回。 4272 4273**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4274 4275**系统能力:** SystemCapability.Global.ResourceManager 4276 4277**返回值:** 4278 4279| 类型 | 说明 | 4280| ---------------------------------------- | ------------------- | 4281| [DeviceCapability](#devicecapability) | 设备的DeviceCapability。 | 4282 4283**示例:** 4284 ```ts 4285import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4286 4287export default class EntryAbility extends UIAbility { 4288 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4289 try { 4290 let value = this.context.resourceManager.getDeviceCapabilitySync(); 4291 let screenDensity = value.screenDensity; 4292 let deviceType = value.deviceType; 4293 } catch (error) { 4294 console.error("getDeviceCapabilitySync error is " + error); 4295 } 4296 } 4297} 4298 ``` 4299 4300### getDeviceCapability 4301 4302getDeviceCapability(callback: _AsyncCallback<DeviceCapability>): void 4303 4304获取设备的DeviceCapability,使用callback异步回调。 4305 4306**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4307 4308**系统能力:** SystemCapability.Global.ResourceManager 4309 4310**参数:** 4311 4312| 参数名 | 类型 | 必填 | 说明 | 4313| -------- | ---------------------------------------- | ---- | ---------------------------- | 4314| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<[DeviceCapability](#devicecapability)> | 是 | 回调函数,返回设备的DeviceCapability。 | 4315 4316**示例:** 4317 ```ts 4318import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4319import { BusinessError } from '@kit.BasicServicesKit'; 4320import { resourceManager } from '@kit.LocalizationKit'; 4321 4322export default class EntryAbility extends UIAbility { 4323 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4324 try { 4325 this.context.resourceManager.getDeviceCapability((error: BusinessError, value: resourceManager.DeviceCapability) => { 4326 if (error != null) { 4327 console.error("getDeviceCapability callback error is " + error); 4328 } else { 4329 let screenDensity = value.screenDensity; 4330 let deviceType = value.deviceType; 4331 } 4332 }); 4333 } catch (error) { 4334 console.error("getDeviceCapability callback error is " + error); 4335 } 4336 } 4337} 4338 ``` 4339 4340### getDeviceCapability 4341 4342getDeviceCapability(): Promise<DeviceCapability> 4343 4344获取设备的DeviceCapability,使用Promise异步回调。 4345 4346**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4347 4348**系统能力:** SystemCapability.Global.ResourceManager 4349 4350**返回值:** 4351 4352| 类型 | 说明 | 4353| ---------------------------------------- | ------------------- | 4354| Promise<[DeviceCapability](#devicecapability)> | Promise对象,返回设备的DeviceCapability。 | 4355 4356**示例:** 4357 ```ts 4358import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4359import { BusinessError } from '@kit.BasicServicesKit'; 4360import { resourceManager } from '@kit.LocalizationKit'; 4361 4362export default class EntryAbility extends UIAbility { 4363 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4364 try { 4365 this.context.resourceManager.getDeviceCapability().then((value: resourceManager.DeviceCapability) => { 4366 let screenDensity = value.screenDensity; 4367 let deviceType = value.deviceType; 4368 }).catch((error: BusinessError) => { 4369 console.error("getDeviceCapability promise error is " + error); 4370 }); 4371 } catch (error) { 4372 console.error("getDeviceCapability promise error is " + error); 4373 } 4374 } 4375} 4376 ``` 4377 4378### addResource<sup>10+</sup> 4379 4380addResource(path: string): void 4381 4382应用运行时加载指定的资源路径,实现资源覆盖。 4383 4384> **说明** 4385> 4386> rawfile和resfile目录不支持资源覆盖。 4387 4388**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4389 4390**系统能力:** SystemCapability.Global.ResourceManager 4391 4392**参数:** 4393 4394| 参数名 | 类型 | 必填 | 说明 | 4395| -------- | ---------------------- | ---- | ---- | 4396| path | string | 是 | 资源路径。 | 4397 4398**错误码:** 4399 4400以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 4401 4402| 错误码ID | 错误信息 | 4403| -------- | ---------------------------------------- | 4404| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4405| 9001010 | Invalid overlay path. | 4406 4407**示例:** 4408 ```ts 4409import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4410import { BusinessError } from '@kit.BasicServicesKit'; 4411 4412export default class EntryAbility extends UIAbility { 4413 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4414 // "/library1-default-signed.hsp"仅作示例,请替换为实际的文件路径 4415 let path = this.context.bundleCodeDir + "/library1-default-signed.hsp"; 4416 try { 4417 this.context.resourceManager.addResource(path); 4418 } catch (error) { 4419 let code = (error as BusinessError).code; 4420 let message = (error as BusinessError).message; 4421 console.error(`addResource failed, error code: ${code}, message: ${message}.`); 4422 } 4423 } 4424} 4425 ``` 4426 4427### removeResource<sup>10+</sup> 4428 4429removeResource(path: string): void 4430 4431应用运行时移除指定的资源路径,还原被覆盖前的资源。 4432 4433> **说明** 4434> 4435> rawfile和resfile目录不支持资源覆盖。 4436 4437**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4438 4439**系统能力:** SystemCapability.Global.ResourceManager 4440 4441**参数:** 4442 4443| 参数名 | 类型 | 必填 | 说明 | 4444| -------- | ---------------------- | ---- | ---- | 4445| path | string | 是 | 资源路径。 | 4446 4447**错误码:** 4448 4449以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 4450 4451| 错误码ID | 错误信息 | 4452| -------- | ---------------------------------------- | 4453| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4454| 9001010 | Invalid overlay path. | 4455 4456**示例:** 4457 ```ts 4458import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4459import { BusinessError } from '@kit.BasicServicesKit'; 4460 4461export default class EntryAbility extends UIAbility { 4462 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4463 // "/library1-default-signed.hsp"仅作示例,请替换为实际的文件路径 4464 let path = this.context.bundleCodeDir + "/library1-default-signed.hsp"; 4465 try { 4466 this.context.resourceManager.removeResource(path); 4467 } catch (error) { 4468 let code = (error as BusinessError).code; 4469 let message = (error as BusinessError).message; 4470 console.error(`removeResource failed, error code: ${code}, message: ${message}.`); 4471 } 4472 } 4473} 4474 ``` 4475 4476### getLocales<sup>11+</sup> 4477 4478getLocales(includeSystem?: boolean): Array\<string> 4479 4480获取应用的语言列表。 4481 4482**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4483 4484**系统能力:** SystemCapability.Global.ResourceManager 4485 4486**参数:** 4487 4488| 参数名 | 类型 | 必填 | 说明 | 4489| -------------- | ------- | ------ | -------------------- | 4490| includeSystem | boolean | 否 | 是否包含系统资源,默认值为false。 <br> - false:表示仅获取应用资源的语言列表。 <br> - true:表示获取系统资源和应用资源的语言列表。 <br>当使用系统资源管理对象获取语言列表时,includeSystem值无效,始终返回系统资源语言列表。| 4491 4492**返回值:** 4493 4494| 类型 | 说明 | 4495| ------------------------- | ----------- | 4496| Array\<string> | 返回获取的语言列表,列表中的字符串由语言、脚本(可选)、地区(可选),按照顺序使用中划线“-”连接组成。| 4497 4498**错误码:** 4499 4500以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 4501 4502| 错误码ID | 错误信息 | 4503| -------- | ------------------------------------------------------------ | 4504| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4505 4506**示例:** 4507 ```ts 4508import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4509import { BusinessError } from '@kit.BasicServicesKit'; 4510import { resourceManager } from '@kit.LocalizationKit'; 4511 4512export default class EntryAbility extends UIAbility { 4513 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4514 try { 4515 this.context.resourceManager.getLocales(); // 仅获取应用资源语言列表 4516 } catch (error) { 4517 let code = (error as BusinessError).code; 4518 let message = (error as BusinessError).message; 4519 console.error(`getLocales failed, error code: ${code}, message: ${message}.`); 4520 } 4521 4522 try { 4523 resourceManager.getSysResourceManager().getLocales(); // 仅获取系统资源语言列表 4524 } catch (error) { 4525 let code = (error as BusinessError).code; 4526 let message = (error as BusinessError).message; 4527 console.error(`getLocales failed, error code: ${code}, message: ${message}.`); 4528 } 4529 4530 try { 4531 this.context.resourceManager.getLocales(true); // 获取应用资源和系统资源语言列表 4532 } catch (error) { 4533 let code = (error as BusinessError).code; 4534 let message = (error as BusinessError).message; 4535 console.error(`getLocales failed, error code: ${code}, message: ${message}.`); 4536 } 4537 } 4538} 4539 ``` 4540 4541### getSymbol<sup>11+</sup> 4542 4543getSymbol(resId: number): number 4544 4545获取指定资源ID对应的[Symbol字符](https://developer.huawei.com/consumer/cn/design/harmonyos-symbol)Unicode码,使用同步方式返回。 4546 4547**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4548 4549**系统能力:** SystemCapability.Global.ResourceManager 4550 4551**参数:** 4552 4553| 参数名 | 类型 | 必填 | 说明 | 4554| ----- | ------ | ---- | ----- | 4555| resId | number | 是 | 资源ID值。 | 4556 4557**返回值:** 4558 4559| 类型 | 说明 | 4560| ------ | ----------- | 4561| number | 资源ID值对应的Symbol字符Unicode码(十进制)。 | 4562 4563**错误码:** 4564 4565以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 4566 4567| 错误码ID | 错误信息 | 4568| -------- | ---------------------------------------- | 4569| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4570| 9001001 | Invalid resource ID. | 4571| 9001002 | No matching resource is found based on the resource ID. | 4572| 9001006 | The resource is referenced cyclically. | 4573 4574**示例:** 4575 ```ts 4576import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4577import { BusinessError } from '@kit.BasicServicesKit'; 4578 4579export default class EntryAbility extends UIAbility { 4580 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4581 try { 4582 // 'sys.symbol.message'仅作示例,请替换为实际使用的资源 4583 let symbolValue = this.context.resourceManager.getSymbol($r('sys.symbol.message').id); 4584 console.log(`getSymbol, result: ${symbolValue}`); 4585 // 打印输出结果: getSymbol, result: 983183 4586 } catch (error) { 4587 let code = (error as BusinessError).code; 4588 let message = (error as BusinessError).message; 4589 console.error(`getSymbol failed, error code: ${code}, message: ${message}.`); 4590 } 4591 } 4592} 4593 ``` 4594 4595 4596### getSymbolByName<sup>11+</sup> 4597 4598getSymbolByName(resName: string): number 4599 4600获取指定资源名称对应的[Symbol字符](https://developer.huawei.com/consumer/cn/design/harmonyos-symbol)Unicode码,使用同步方式返回。 4601 4602**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4603 4604**系统能力:** SystemCapability.Global.ResourceManager 4605 4606**参数:** 4607 4608| 参数名 | 类型 | 必填 | 说明 | 4609| ------- | ------ | ---- | ---- | 4610| resName | string | 是 | 资源名称。 | 4611 4612**返回值:** 4613 4614| 类型 | 说明 | 4615| ------ | ---------- | 4616| number | 资源名称对应的Symbol字符Unicode码(十进制)。 | 4617 4618**错误码:** 4619 4620以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 4621 4622| 错误码ID | 错误信息 | 4623| -------- | ---------------------------------------- | 4624| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4625| 9001003 | Invalid resource name. | 4626| 9001004 | No matching resource is found based on the resource name. | 4627| 9001006 | The resource is referenced cyclically. | 4628 4629**示例:** 4630 ```ts 4631import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4632import { BusinessError } from '@kit.BasicServicesKit'; 4633 4634export default class EntryAbility extends UIAbility { 4635 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4636 try { 4637 // "message"仅作示例,请替换为实际使用的资源 4638 let symbolValue = this.context.resourceManager.getSymbolByName("message"); 4639 console.log(`getSymbolByName, result: ${symbolValue}`); 4640 // 打印输出结果: getSymbolByName, result: 983183 4641 } catch (error) { 4642 let code = (error as BusinessError).code; 4643 let message = (error as BusinessError).message; 4644 console.error(`getSymbolByName failed, error code: ${code}, message: ${message}.`); 4645 } 4646 } 4647} 4648 ``` 4649 4650### isRawDir<sup>12+</sup> 4651 4652isRawDir(path: string): boolean 4653 4654判断指定路径是否为rawfile下的目录,使用同步方式返回。 4655 4656**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4657 4658**系统能力:** SystemCapability.Global.ResourceManager 4659 4660**参数:** 4661 4662| 参数名 | 类型 | 必填 | 说明 | 4663| ------- | ------ | ---- | ---- | 4664| path | string | 是 | rawfile路径。 | 4665 4666**返回值:** 4667 4668| 类型 | 说明 | 4669| ------ | ---------- | 4670| boolean |是否为rawfile下的目录。<br> - true:表示是rawfile下的目录。 <br> - false:表示非rawfile下的目录。| 4671 4672**错误码:** 4673 4674以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 4675 4676| 错误码ID | 错误信息 | 4677| -------- | ---------------------------------------- | 4678| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4679| 9001005 | Invalid relative path. | 4680 4681**示例:** 4682 ```ts 4683import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4684import { BusinessError } from '@kit.BasicServicesKit'; 4685 4686export default class EntryAbility extends UIAbility { 4687 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4688 try { 4689 // 假设rawfile根目录下存在非空文件夹sub,则isRawDir返回结果为true 4690 // "sub"仅作示例,请替换为实际使用的目录名称 4691 let isRawDir = this.context.resourceManager.isRawDir("sub"); 4692 // 打印输出结果: sub isRawDir, result: true 4693 console.log(`sub isRawDir, result: ${isRawDir}`); 4694 4695 // 假设rawfile根目录下存在test.txt文件,则isRawDir返回结果为false 4696 // "test.txt"仅作示例,请替换为实际使用的资源 4697 isRawDir = this.context.resourceManager.isRawDir("test.txt"); 4698 // 打印输出结果: test.txt isRawDir, result: false 4699 console.log(`test.txt isRawDir, result: ${isRawDir}`); 4700 } catch (error) { 4701 let code = (error as BusinessError).code; 4702 let message = (error as BusinessError).message; 4703 console.error(`isRawDir failed, error code: ${code}, message: ${message}.`); 4704 } 4705 } 4706} 4707 ``` 4708 4709### getOverrideResourceManager<sup>12+</sup> 4710 4711getOverrideResourceManager(configuration?: Configuration): ResourceManager 4712 4713获取可以加载差异化资源的资源管理对象,使用同步方式返回。 4714 4715普通的资源管理对象获取的资源的配置(语言、深浅色、分辨率、横竖屏等)是由系统决定的,而通过该接口返回的对象,应用可以获取符合指定配置的资源,即差异化资源,比如在浅色模式时可以获取深色资源。 4716 4717**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4718 4719**系统能力:** SystemCapability.Global.ResourceManager 4720 4721**参数:** 4722 4723| 参数名 | 类型 | 必填 | 说明 | 4724| ------------- | ------------------------------- | ---- | ------------------------------------------------------------ | 4725| configuration | [Configuration](#configuration) | 否 | 指定想要获取的资源配置。<br>通过[getOverrideConfiguration](#getoverrideconfiguration12)获取差异化配置后,根据需求修改配置项,再作为参数传入该函数。<br>若缺省则表示使用当前系统的configuration。 | 4726 4727**返回值:** 4728 4729| 类型 | 说明 | 4730| --------------- | ---------------------------------- | 4731| ResourceManager | 可以加载差异化资源的资源管理对象。 | 4732 4733**错误码:** 4734 4735以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 4736 4737| 错误码ID | 错误信息 | 4738| -------- | ------------------------------------------------------------ | 4739| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4740 4741**示例:** 4742 4743 ```ts 4744import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4745import { BusinessError } from '@kit.BasicServicesKit'; 4746import { resourceManager } from '@kit.LocalizationKit'; 4747 4748export default class EntryAbility extends UIAbility { 4749 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4750 try { 4751 let resMgr = this.context.resourceManager; 4752 let overrideConfig = resMgr.getOverrideConfiguration(); 4753 overrideConfig.colorMode = resourceManager.ColorMode.DARK; 4754 let overrideResMgr = resMgr.getOverrideResourceManager(overrideConfig); 4755 } catch (error) { 4756 let code = (error as BusinessError).code; 4757 let message = (error as BusinessError).message; 4758 console.error(`getOverrideResourceManager failed, error code: ${code}, message: ${message}.`); 4759 } 4760 } 4761} 4762 ``` 4763 4764### getOverrideConfiguration<sup>12+</sup> 4765 4766getOverrideConfiguration(): Configuration 4767 4768获取差异化资源的配置,使用同步方式返回。普通资源管理对象与通过它的[getOverrideResourceManager](#getoverrideresourcemanager12)接口获取的差异化资源管理对象调用该方法可获得相同的返回值。 4769 4770**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4771 4772**系统能力:** SystemCapability.Global.ResourceManager 4773 4774**返回值:** 4775 4776| 类型 | 说明 | 4777| ------------------------------- | ---------------- | 4778| [Configuration](#configuration) | 差异化资源的配置。 | 4779 4780**示例:** 4781 4782 ```ts 4783import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4784import { BusinessError } from '@kit.BasicServicesKit'; 4785import { resourceManager } from '@kit.LocalizationKit'; 4786 4787export default class EntryAbility extends UIAbility { 4788 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4789 try { 4790 let resMgr = this.context.resourceManager; 4791 let overrideConfig = resMgr.getOverrideConfiguration(); 4792 overrideConfig.colorMode = resourceManager.ColorMode.DARK; 4793 let overrideResMgr = resMgr.getOverrideResourceManager(overrideConfig); 4794 } catch (error) { 4795 let code = (error as BusinessError).code; 4796 let message = (error as BusinessError).message; 4797 console.error(`getOverrideResourceManager failed, error code: ${code}, message: ${message}.`); 4798 } 4799 } 4800} 4801 ``` 4802 4803### updateOverrideConfiguration<sup>12+</sup> 4804 4805updateOverrideConfiguration(configuration: Configuration): void 4806 4807更新差异化资源配置。普通资源管理对象与通过它的[getOverrideResourceManager](#getoverrideresourcemanager12)接口获取的差异化资源管理对象调用该方法均可更新差异化资源管理对象的配置。 4808 4809**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4810 4811**系统能力:** SystemCapability.Global.ResourceManager 4812 4813**参数:** 4814 4815| 参数名 | 类型 | 必填 | 说明 | 4816| ------------- | ------------------------------- | ---- | ------------------------------------------------------------ | 4817| configuration | [Configuration](#configuration) | 是 | 指定差异化资源的配置。通过[getOverrideConfiguration](#getoverrideconfiguration12)获取差异化配置后,根据需求修改配置项,再作为参数传入。 | 4818 4819**错误码:** 4820 4821以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 4822 4823| 错误码ID | 错误信息 | 4824| -------- | ------------------------------------------------------------ | 4825| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4826 4827**示例:** 4828 4829 ```ts 4830import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 4831import { BusinessError } from '@kit.BasicServicesKit'; 4832import { resourceManager } from '@kit.LocalizationKit'; 4833 4834export default class EntryAbility extends UIAbility { 4835 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 4836 try { 4837 let resMgr = this.context.resourceManager; 4838 let overrideConfig = resMgr.getOverrideConfiguration(); 4839 overrideConfig.colorMode = resourceManager.ColorMode.DARK; 4840 let overrideResMgr = resMgr.updateOverrideConfiguration(overrideConfig); 4841 } catch (error) { 4842 let code = (error as BusinessError).code; 4843 let message = (error as BusinessError).message; 4844 console.error(`updateOverrideConfiguration failed, error code: ${code}, message: ${message}.`); 4845 } 4846 } 4847} 4848 ``` 4849 4850### release<sup>(deprecated)</sup> 4851 4852release() 4853 4854释放创建的resourceManager, 此接口暂不支持。 4855 4856> **说明** 4857> 4858> 从API version 7开始支持,从API version 12开始废弃。 4859 4860**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4861 4862**系统能力:** SystemCapability.Global.ResourceManager 4863 4864**示例:** 4865 ```ts 4866 try { 4867 this.context.resourceManager.release(); 4868 } catch (error) { 4869 console.error("release error is " + error); 4870 } 4871 ``` 4872 4873### getString<sup>(deprecated)</sup> 4874 4875getString(resId: number, callback: AsyncCallback<string>): void 4876 4877获取指定资源ID对应的字符串,使用callback异步回调。 4878 4879> **说明** 4880> 4881> 从API version 6开始支持,从API version 9开始废弃,建议使用[getStringValue](#getstringvalue9)替代。 4882 4883**系统能力:** SystemCapability.Global.ResourceManager 4884 4885**参数:** 4886 4887| 参数名 | 类型 | 必填 | 说明 | 4888| -------- | --------------------------- | ---- | --------------- | 4889| resId | number | 是 | 资源ID值。 | 4890| callback | [AsyncCallback](#asynccallbackdeprecated)<string> | 是 | 回调函数,返回资源ID值对应的字符串。 | 4891 4892**示例:** 4893 ```ts 4894 resourceManager.getResourceManager((error, mgr) => { 4895 mgr.getString($r('app.string.test').id, (error: Error, value: string) => { 4896 if (error != null) { 4897 console.error("error is " + error); 4898 } else { 4899 let str = value; 4900 } 4901 }); 4902 }); 4903 ``` 4904 4905 4906### getString<sup>(deprecated)</sup> 4907 4908getString(resId: number): Promise<string> 4909 4910获取指定资源ID对应的字符串,使用Promise异步回调。 4911 4912> **说明** 4913> 4914> 从API version 6开始支持,从API version 9开始废弃,建议使用[getStringValue](#getstringvalue9-1)替代。 4915 4916**系统能力:** SystemCapability.Global.ResourceManager 4917 4918**参数:** 4919 4920| 参数名 | 类型 | 必填 | 说明 | 4921| ----- | ------ | ---- | ----- | 4922| resId | number | 是 | 资源ID值。 | 4923 4924**返回值:** 4925 4926| 类型 | 说明 | 4927| --------------------- | ----------- | 4928| Promise<string> | Promise对象,返回资源ID值对应的字符串。 | 4929 4930**示例:** 4931 ```ts 4932 import { BusinessError } from '@kit.BasicServicesKit'; 4933 4934 resourceManager.getResourceManager((error, mgr) => { 4935 mgr.getString($r('app.string.test').id).then((value: string) => { 4936 let str = value; 4937 }).catch((error: BusinessError) => { 4938 console.error("getstring promise error is " + error); 4939 }); 4940 }); 4941 ``` 4942### getStringSync<sup>(deprecated)</sup> 4943 4944getStringSync(resource: Resource): string 4945 4946获取指定resource对象对应的字符串,使用同步方式返回。 4947 4948> **说明** 4949> 4950> 从API version 9开始支持,从API version 20开始废弃,建议使用[getStringByNameSync](#getstringbynamesync9)或[getStringSync](#getstringsync9)替代。 4951 4952**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 4953 4954**系统能力:** SystemCapability.Global.ResourceManager 4955 4956**模型约束:** 此接口仅可在Stage模型下使用。 4957 4958**参数:** 4959 4960| 参数名 | 类型 | 必填 | 说明 | 4961| -------- | ---------------------- | ---- | ---- | 4962| resource | [Resource](#resource9) | 是 | 资源信息。 | 4963 4964**返回值:** 4965 4966| 类型 | 说明 | 4967| ------ | ---------------- | 4968| string | resource对象对应的字符串。 | 4969 4970**错误码:** 4971 4972以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 4973 4974| 错误码ID | 错误信息 | 4975| -------- | ---------------------------------------- | 4976| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4977| 9001001 | Invalid resource ID. | 4978| 9001002 | No matching resource is found based on the resource ID. | 4979| 9001006 | The resource is referenced cyclically. | 4980 4981**示例:** 4982 ```json 4983 // 资源文件路径: src/main/resources/base/element/string.json 4984 { 4985 "string": [ 4986 { 4987 "name": "test", 4988 "value": "I'm a test string resource." 4989 } 4990 ] 4991 } 4992 ``` 4993 ```ts 4994 import { resourceManager } from '@kit.LocalizationKit'; 4995 import { BusinessError } from '@kit.BasicServicesKit'; 4996 4997 let resource: resourceManager.Resource = { 4998 bundleName: "com.example.myapplication", 4999 moduleName: "entry", 5000 id: $r('app.string.test').id 5001 }; 5002 try { 5003 let testStr = this.context.resourceManager.getStringSync(resource); 5004 console.log(`getStringSync, result: ${testStr}`); 5005 // 打印输出结果: getStringSync, result: I'm a test string resource. 5006 } catch (error) { 5007 let code = (error as BusinessError).code; 5008 let message = (error as BusinessError).message; 5009 console.error(`getStringSync failed, error code: ${code}, message: ${message}.`); 5010 } 5011 ``` 5012 5013### getStringSync<sup>(deprecated)</sup> 5014 5015getStringSync(resource: Resource, ...args: Array<string | number>): string 5016 5017获取指定resource对象对应的字符串,并根据args参数对字符串进行格式化,使用同步方式返回。 5018 5019> **说明** 5020> 5021> 从API version 10开始支持,从API version 20开始废弃,建议使用[getStringByNameSync](#getstringbynamesync10)或[getStringSync](#getstringsync10)替代。 5022 5023**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 5024 5025**系统能力:** SystemCapability.Global.ResourceManager 5026 5027**模型约束:** 此接口仅可在Stage模型下使用。 5028 5029**参数:** 5030 5031| 参数名 | 类型 | 必填 | 说明 | 5032| -------- | ---------------------- | ---- | ---- | 5033| resource | [Resource](#resource9) | 是 | 资源信息。 | 5034| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:`%d`、`%f`、`%s`、`%%`、`%数字$d`、`%数字$f`、`%数字$s`。<br>说明:`%%`转义为`%`; `%数字$d`中的数字表示使用args中的第几个参数。<br>举例:`%%d`格式化后为`%d`字符串; `%1$d`表示使用第一个参数。| 5035 5036**返回值:** 5037 5038| 类型 | 说明 | 5039| ------ | ---------------------------- | 5040| string | resource对象对应的格式化字符串。| 5041 5042**错误码:** 5043 5044以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 5045 5046| 错误码ID | 错误信息 | 5047| -------- | ---------------------------------------- | 5048| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5049| 9001001 | Invalid resource ID. | 5050| 9001002 | No matching resource is found based on the resource ID. | 5051| 9001006 | The resource is referenced cyclically. | 5052| 9001007 | Failed to format the resource obtained based on the resource ID. | 5053 5054**示例:** 5055 ```json 5056 // 资源文件路径: src/main/resources/base/element/string.json 5057 { 5058 "string": [ 5059 { 5060 "name": "test", 5061 "value": "I'm a %1$s, format int: %2$d, format float: %3$f." 5062 } 5063 ] 5064 } 5065 ``` 5066 ```ts 5067 import { resourceManager } from '@kit.LocalizationKit'; 5068 import { BusinessError } from '@kit.BasicServicesKit'; 5069 5070 let resource: resourceManager.Resource = { 5071 bundleName: "com.example.myapplication", 5072 moduleName: "entry", 5073 id: $r('app.string.test').id 5074 }; 5075 try { 5076 let testStr = this.context.resourceManager.getStringSync(resource, "format string", 10, 98.78); 5077 console.log(`getStringSync, result: ${testStr}`); 5078 // 打印输出结果: getStringSync, result: I'm a format string, format int: 10, format float: 98.78. 5079 } catch (error) { 5080 let code = (error as BusinessError).code; 5081 let message = (error as BusinessError).message; 5082 console.error(`getStringSync failed, error code: ${code}, message: ${message}.`); 5083 } 5084 ``` 5085### getStringValue<sup>(deprecated)</sup> 5086 5087getStringValue(resource: Resource, callback: _AsyncCallback<string>): void 5088 5089获取指定resource对象对应的字符串,使用callback异步回调。 5090 5091> **说明** 5092> 5093> 从API version 9开始支持,从API version 20开始废弃,建议使用[getStringByName](#getstringbyname9)或[getStringValue](#getstringvalue9)替代。 5094 5095**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 5096 5097**系统能力:** SystemCapability.Global.ResourceManager 5098 5099**模型约束:** 此接口仅可在Stage模型下使用。 5100 5101**参数:** 5102 5103| 参数名 | 类型 | 必填 | 说明 | 5104| -------- | --------------------------- | ---- | --------------- | 5105| resource | [Resource](#resource9) | 是 | 资源信息。 | 5106| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 回调函数,返回resource对象对应的字符串。 | 5107 5108**错误码:** 5109 5110以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 5111 5112| 错误码ID | 错误信息 | 5113| -------- | ---------------------------------------- | 5114| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5115| 9001001 | Invalid resource ID. | 5116| 9001002 | No matching resource is found based on the resource ID. | 5117| 9001006 | The resource is referenced cyclically. | 5118 5119**示例:** 5120 ```json 5121 // 资源文件路径: src/main/resources/base/element/string.json 5122 { 5123 "string": [ 5124 { 5125 "name": "test", 5126 "value": "I'm a test string resource." 5127 } 5128 ] 5129 } 5130 ``` 5131 ```ts 5132 import { resourceManager } from '@kit.LocalizationKit'; 5133 import { BusinessError } from '@kit.BasicServicesKit'; 5134 5135 let resource: resourceManager.Resource = { 5136 bundleName: "com.example.myapplication", 5137 moduleName: "entry", 5138 id: $r('app.string.test').id 5139 }; 5140 this.context.resourceManager.getStringValue(resource, (error: BusinessError, value: string) => { 5141 if (error != null) { 5142 console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`); 5143 } else { 5144 console.log(`getStringValue, result: ${value}`); 5145 // 打印输出结果: getStringValue, result: I'm a test string resource. 5146 } 5147 }); 5148 ``` 5149 5150### getStringValue<sup>(deprecated)</sup> 5151 5152getStringValue(resource: Resource): Promise<string> 5153 5154获取指定resource对象对应的字符串,使用Promise异步回调。 5155 5156> **说明** 5157> 5158> 从API version 9开始支持,从API version 20开始废弃,建议使用[getStringByName](#getstringbyname9-1)或[getStringValue](#getstringvalue9-1)替代。 5159 5160**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 5161 5162**系统能力:** SystemCapability.Global.ResourceManager 5163 5164**模型约束:** 此接口仅可在Stage模型下使用。 5165 5166**参数:** 5167 5168| 参数名 | 类型 | 必填 | 说明 | 5169| -------- | ---------------------- | ---- | ---- | 5170| resource | [Resource](#resource9) | 是 | 资源信息。 | 5171 5172**返回值:** 5173 5174| 类型 | 说明 | 5175| --------------------- | ---------------- | 5176| Promise<string> | Promise对象,返回resource对象对应的字符串。 | 5177 5178**错误码:** 5179 5180以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 5181 5182| 错误码ID | 错误信息 | 5183| -------- | ---------------------------------------- | 5184| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5185| 9001001 | Invalid resource ID. | 5186| 9001002 | No matching resource is found based on the resource ID. | 5187| 9001006 | The resource is referenced cyclically. | 5188 5189**示例:** 5190 ```ts 5191 import { resourceManager } from '@kit.LocalizationKit'; 5192 import { BusinessError } from '@kit.BasicServicesKit'; 5193 5194 let resource: resourceManager.Resource = { 5195 bundleName: "com.example.myapplication", 5196 moduleName: "entry", 5197 id: $r('app.string.test').id 5198 }; 5199 this.context.resourceManager.getStringValue(resource, (error: BusinessError, value: string) => { 5200 if (error != null) { 5201 console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`); 5202 } else { 5203 console.log(`getStringValue, result: ${value}`); 5204 // 打印输出结果: getStringValue, result: I'm a test string resource. 5205 } 5206 }); 5207 ``` 5208 5209### getStringArray<sup>(deprecated)</sup> 5210 5211getStringArray(resId: number, callback: AsyncCallback<Array<string>>): void 5212 5213获取指定资源ID对应的字符串数组,使用callback异步回调。 5214 5215> **说明** 5216> 5217> 从API version 6开始支持,从API version 9开始废弃,建议使用[getStringArrayValue](#getstringarrayvalue9)替代。 5218 5219**系统能力:** SystemCapability.Global.ResourceManager 5220 5221**参数:** 5222 5223| 参数名 | 类型 | 必填 | 说明 | 5224| -------- | ---------------------------------------- | ---- | ----------------- | 5225| resId | number | 是 | 资源ID值。 | 5226| callback | [AsyncCallback](#asynccallbackdeprecated)<Array<string>> | 是 | 回调函数,返回资源ID值对应的字符串数组。 | 5227 5228**示例:** 5229 ```ts 5230 resourceManager.getResourceManager((error, mgr) => { 5231 mgr.getStringArray($r('app.strarray.test').id, (error: Error, value: Array<string>) => { 5232 if (error != null) { 5233 console.error("error is " + error); 5234 } else { 5235 let strArray = value; 5236 } 5237 }); 5238 }); 5239 ``` 5240 5241 5242### getStringArray<sup>(deprecated)</sup> 5243 5244getStringArray(resId: number): Promise<Array<string>> 5245 5246获取指定资源ID对应的字符串数组,使用Promise异步回调。 5247 5248> **说明** 5249> 5250> 从API version 6开始支持,从API version 9开始废弃,建议使用[getStringArrayValue](#getstringarrayvalue9-1)替代。 5251 5252**系统能力:** SystemCapability.Global.ResourceManager 5253 5254**参数:** 5255 5256| 参数名 | 类型 | 必填 | 说明 | 5257| ----- | ------ | ---- | ----- | 5258| resId | number | 是 | 资源ID值。 | 5259 5260**返回值:** 5261 5262| 类型 | 说明 | 5263| ---------------------------------- | ------------- | 5264| Promise<Array<string>> | Promise对象,返回资源ID值对应的字符串数组。 | 5265 5266**示例:** 5267 ```ts 5268 import { BusinessError } from '@kit.BasicServicesKit'; 5269 5270 resourceManager.getResourceManager((error, mgr) => { 5271 mgr.getStringArray($r('app.strarray.test').id).then((value: Array<string>) => { 5272 let strArray = value; 5273 }).catch((error: BusinessError) => { 5274 console.error("getStringArray promise error is " + error); 5275 }); 5276 }); 5277 ``` 5278 5279### getStringArrayValueSync<sup>(deprecated)</sup> 5280 5281getStringArrayValueSync(resource: Resource): Array<string> 5282 5283获取指定resource对象对应的字符串数组,使用同步方式返回。 5284 5285> **说明** 5286> 5287> 从API version 10开始支持,从API version 20开始废弃,建议使用[getStringArrayByNameSync](#getstringarraybynamesync10)或[getStringArrayValueSync](#getstringarrayvaluesync10)替代。 5288 5289**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 5290 5291**系统能力:** SystemCapability.Global.ResourceManager 5292 5293**模型约束:** 此接口仅可在Stage模型下使用。 5294 5295**参数:** 5296 5297| 参数名 | 类型 | 必填 | 说明 | 5298| ----- | ------ | ---- | ----- | 5299| resource | [Resource](#resource9) | 是 | 资源信息。 | 5300 5301**返回值:** 5302 5303| 类型 | 说明 | 5304| --------------------- | ----------- | 5305| Array<string> | resource对象对应的字符串数组。 | 5306 5307**错误码:** 5308 5309以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 5310 5311| 错误码ID | 错误信息 | 5312| -------- | ---------------------------------------- | 5313| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5314| 9001001 | Invalid resource ID. | 5315| 9001002 | No matching resource is found based on the resource ID. | 5316| 9001006 | The resource is referenced cyclically. | 5317 5318**示例:** 5319 ```json 5320 // 资源文件路径: src/main/resources/base/element/strarray.json 5321 { 5322 "strarray": [ 5323 { 5324 "name": "test", 5325 "value": [ 5326 { 5327 "value": "I'm one of the array's values." 5328 } 5329 ] 5330 } 5331 ] 5332 } 5333 ``` 5334 ```ts 5335 import { resourceManager } from '@kit.LocalizationKit'; 5336 import { BusinessError } from '@kit.BasicServicesKit'; 5337 5338 let resource: resourceManager.Resource = { 5339 bundleName: "com.example.myapplication", 5340 moduleName: "entry", 5341 id: $r('app.strarray.test').id 5342 }; 5343 try { 5344 let strArray: Array<string> = this.context.resourceManager.getStringArrayValueSync(resource); 5345 console.log(`getStringArrayValueSync, result: ${strArray[0]}`); 5346 // 打印输出结果: getStringArrayValueSync, result: I'm one of the array's values. 5347 } catch (error) { 5348 let code = (error as BusinessError).code; 5349 let message = (error as BusinessError).message; 5350 console.error(`getStringArrayValueSync failed, error code: ${code}, message: ${message}.`); 5351 } 5352 ``` 5353### getStringArrayValue<sup>(deprecated)</sup> 5354 5355getStringArrayValue(resource: Resource, callback: _AsyncCallback<Array<string>>): void 5356 5357获取指定resource对象对应的字符串数组,使用callback异步回调。 5358 5359> **说明** 5360> 5361> 从API version 9开始支持,从API version 20开始废弃,建议使用[getStringArrayByName](#getstringarraybyname9)或[getStringArrayValue](#getstringarrayvalue9)替代。 5362 5363**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 5364 5365**系统能力:** SystemCapability.Global.ResourceManager 5366 5367**模型约束:** 此接口仅可在Stage模型下使用。 5368 5369**参数:** 5370 5371| 参数名 | 类型 | 必填 | 说明 | 5372| -------- | ---------------------------------------- | ---- | ----------------- | 5373| resource | [Resource](#resource9) | 是 | 资源信息。 | 5374| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Array<string>> | 是 | 回调函数,返回resource对象对应的字符串数组。| 5375 5376**错误码:** 5377 5378以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 5379 5380| 错误码ID | 错误信息 | 5381| -------- | ---------------------------------------- | 5382| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5383| 9001001 | Invalid resource ID. | 5384| 9001002 | No matching resource is found based on the resource ID. | 5385| 9001006 | The resource is referenced cyclically. | 5386 5387**示例:** 5388 ```json 5389 // 资源文件路径: src/main/resources/base/element/strarray.json 5390 { 5391 "strarray": [ 5392 { 5393 "name": "test", 5394 "value": [ 5395 { 5396 "value": "I'm one of the array's values." 5397 } 5398 ] 5399 } 5400 ] 5401 } 5402 ``` 5403 ```ts 5404 import { resourceManager } from '@kit.LocalizationKit'; 5405 import { BusinessError } from '@kit.BasicServicesKit'; 5406 5407 let resource: resourceManager.Resource = { 5408 bundleName: "com.example.myapplication", 5409 moduleName: "entry", 5410 id: $r('app.strarray.test').id 5411 }; 5412 this.context.resourceManager.getStringArrayValue(resource, (error: BusinessError, value: Array<string>) => { 5413 if (error != null) { 5414 console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`); 5415 } else { 5416 console.log(`getStringArrayValue, result: ${value[0]}`); 5417 // 打印输出结果: getStringArrayValue, result: I'm one of the array's values. 5418 } 5419 }); 5420 ``` 5421 5422### getStringArrayValue<sup>(deprecated)</sup> 5423 5424getStringArrayValue(resource: Resource): Promise<Array<string>> 5425 5426获取指定resource对象对应的字符串数组,使用Promise异步回调。 5427 5428> **说明** 5429> 5430> 从API version 9开始支持,从API version 20开始废弃,建议使用[getStringArrayByName](#getstringarraybyname9-1)或[getStringArrayValue](#getstringarrayvalue9-1)替代。 5431 5432**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 5433 5434**系统能力:** SystemCapability.Global.ResourceManager 5435 5436**模型约束:** 此接口仅可在Stage模型下使用。 5437 5438**参数:** 5439 5440| 参数名 | 类型 | 必填 | 说明 | 5441| -------- | ---------------------- | ---- | ---- | 5442| resource | [Resource](#resource9) | 是 | 资源信息。 | 5443 5444**返回值:** 5445 5446| 类型 | 说明 | 5447| ---------------------------------- | ------------------ | 5448| Promise<Array<string>> | Promise对象,返回resource对象对应的字符串数组。 | 5449 5450**错误码:** 5451 5452以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 5453 5454| 错误码ID | 错误信息 | 5455| -------- | ---------------------------------------- | 5456| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5457| 9001001 | Invalid resource ID. | 5458| 9001002 | No matching resource is found based on the resource ID. | 5459| 9001006 | The resource is referenced cyclically. | 5460 5461**示例:** 5462 ```json 5463 // 资源文件路径: src/main/resources/base/element/strarray.json 5464 { 5465 "strarray": [ 5466 { 5467 "name": "test", 5468 "value": [ 5469 { 5470 "value": "I'm one of the array's values." 5471 } 5472 ] 5473 } 5474 ] 5475 } 5476 ``` 5477 ```ts 5478 import { resourceManager } from '@kit.LocalizationKit'; 5479 import { BusinessError } from '@kit.BasicServicesKit'; 5480 5481 let resource: resourceManager.Resource = { 5482 bundleName: "com.example.myapplication", 5483 moduleName: "entry", 5484 id: $r('app.strarray.test').id 5485 }; 5486 this.context.resourceManager.getStringArrayValue(resource) 5487 .then((value: Array<string>) => { 5488 console.log(`getStringArrayValue, result: ${value[0]}`); 5489 // 打印输出结果: getStringArrayValue, result: I'm one of the array's values. 5490 }) 5491 .catch((error: BusinessError) => { 5492 console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`); 5493 }); 5494 ``` 5495 5496### getMedia<sup>(deprecated)</sup> 5497 5498getMedia(resId: number, callback: AsyncCallback<Uint8Array>): void 5499 5500获取指定资源ID对应的媒体文件内容,使用callback异步回调。 5501 5502> **说明** 5503> 5504> 从API version 6开始支持,从API version 9开始废弃,建议使用[getMediaContent](#getmediacontent9)替代。 5505 5506**系统能力:** SystemCapability.Global.ResourceManager 5507 5508**参数:** 5509 5510| 参数名 | 类型 | 必填 | 说明 | 5511| -------- | ------------------------------- | ---- | ------------------ | 5512| resId | number | 是 | 资源ID值。 | 5513| callback | [AsyncCallback](#asynccallbackdeprecated)<Uint8Array> | 是 | 回调函数,返回资源ID值对应的媒体文件内容。 | 5514 5515**示例:** 5516 ```ts 5517 resourceManager.getResourceManager((error, mgr) => { 5518 mgr.getMedia($r('app.media.test').id, (error: Error, value: Uint8Array) => { 5519 if (error != null) { 5520 console.error("error is " + error); 5521 } else { 5522 let media = value; 5523 } 5524 }); 5525 }); 5526 ``` 5527 5528### getMedia<sup>(deprecated)</sup> 5529 5530getMedia(resId: number): Promise<Uint8Array> 5531 5532获取指定资源ID对应的媒体文件内容,使用Promise异步回调。 5533 5534> **说明** 5535> 5536> 从API version 6开始支持,从API version 9开始废弃,建议使用[getMediaContent](#getmediacontent9-1)替代。 5537 5538**系统能力:** SystemCapability.Global.ResourceManager 5539 5540**参数:** 5541 5542| 参数名 | 类型 | 必填 | 说明 | 5543| ----- | ------ | ---- | ----- | 5544| resId | number | 是 | 资源ID值。 | 5545 5546**返回值:** 5547 5548| 类型 | 说明 | 5549| ------------------------- | -------------- | 5550| Promise<Uint8Array> | Promise对象,返回资源ID值对应的媒体文件内容。 | 5551 5552**示例:** 5553 ```ts 5554 import { BusinessError } from '@kit.BasicServicesKit'; 5555 5556 resourceManager.getResourceManager((error, mgr) => { 5557 mgr.getMedia($r('app.media.test').id).then((value: Uint8Array) => { 5558 let media = value; 5559 }).catch((error: BusinessError) => { 5560 console.error("getMedia promise error is " + error); 5561 }); 5562 }); 5563 ``` 5564 5565### getMediaContentSync<sup>(deprecated)</sup> 5566 5567getMediaContentSync(resource: Resource, density?: number): Uint8Array 5568 5569获取指定resource对象对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回。 5570 5571> **说明** 5572> 5573> 从API version 10开始支持,从API version 20开始废弃,建议使用[getMediaByNameSync](#getmediabynamesync10)或[getMediaContentSync](#getmediacontentsync10)替代。 5574 5575 5576**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 5577 5578**系统能力:** SystemCapability.Global.ResourceManager 5579 5580**模型约束:** 此接口仅可在Stage模型下使用。 5581 5582**参数:** 5583 5584| 参数名 | 类型 | 必填 | 说明 | 5585| ----- | ------ | ---- | ----- | 5586| resource | [Resource](#resource9) | 是 | 资源信息。 | 5587| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 5588 5589**返回值:** 5590 5591| 类型 | 说明 | 5592| --------------------- | ----------- | 5593| Uint8Array | resource对象对应的媒体文件内容。 | 5594 5595**错误码:** 5596 5597以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 5598 5599| 错误码ID | 错误信息 | 5600| -------- | ---------------------------------------- | 5601| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 5602| 9001001 | Invalid resource ID. | 5603| 9001002 | No matching resource is found based on the resource ID. | 5604 5605**示例:** 5606 ```ts 5607 import { resourceManager } from '@kit.LocalizationKit'; 5608 import { BusinessError } from '@kit.BasicServicesKit'; 5609 5610 let resource: resourceManager.Resource = { 5611 bundleName: "com.example.myapplication", 5612 moduleName: "entry", 5613 id: $r('app.media.test').id 5614 }; 5615 try { 5616 this.context.resourceManager.getMediaContentSync(resource); // 默认屏幕密度 5617 } catch (error) { 5618 let code = (error as BusinessError).code; 5619 let message = (error as BusinessError).message; 5620 console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`); 5621 } 5622 5623 try { 5624 this.context.resourceManager.getMediaContentSync(resource, 120); // 指定屏幕密度 5625 } catch (error) { 5626 let code = (error as BusinessError).code; 5627 let message = (error as BusinessError).message; 5628 console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`); 5629 } 5630 ``` 5631 5632### getMediaContent<sup>(deprecated)</sup> 5633 5634getMediaContent(resource: Resource, callback: _AsyncCallback<Uint8Array>): void 5635 5636获取指定resource对象对应的媒体文件内容,使用callback异步回调。 5637 5638> **说明** 5639> 5640> 从API version 9开始支持,从API version 20开始废弃,建议使用[getMediaByName](#getmediabyname9)或[getMediaContent](#getmediacontent9)替代。 5641 5642**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 5643 5644**系统能力:** SystemCapability.Global.ResourceManager 5645 5646**模型约束:** 此接口仅可在Stage模型下使用。 5647 5648**参数:** 5649 5650| 参数名 | 类型 | 必填 | 说明 | 5651| -------- | ------------------------------- | ---- | ------------------ | 5652| resource | [Resource](#resource9) | 是 | 资源信息。 | 5653| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Uint8Array> | 是 | 回调函数,返回resource对象对应的媒体文件内容。 | 5654 5655**错误码:** 5656 5657以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 5658 5659| 错误码ID | 错误信息 | 5660| -------- | ---------------------------------------- | 5661| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5662| 9001001 | Invalid resource ID. | 5663| 9001002 | No matching resource is found based on the resource ID. | 5664 5665**示例:** 5666 ```ts 5667 import { resourceManager } from '@kit.LocalizationKit'; 5668 import { BusinessError } from '@kit.BasicServicesKit'; 5669 5670 let resource: resourceManager.Resource = { 5671 bundleName: "com.example.myapplication", 5672 moduleName: "entry", 5673 id: $r('app.media.test').id 5674 }; 5675 try { 5676 this.context.resourceManager.getMediaContent(resource, (error: BusinessError, value: Uint8Array) => { 5677 if (error != null) { 5678 console.error("error is " + error); 5679 } else { 5680 let media = value; 5681 } 5682 }); 5683 } catch (error) { 5684 let code = (error as BusinessError).code; 5685 let message = (error as BusinessError).message; 5686 console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`); 5687 } 5688 ``` 5689 5690### getMediaContent<sup>(deprecated)</sup> 5691 5692getMediaContent(resource: Resource, density: number, callback: _AsyncCallback<Uint8Array>): void 5693 5694获取指定resource对象对应的指定屏幕密度媒体文件内容,使用callback异步回调。 5695 5696> **说明** 5697> 5698> 从API version 10开始支持,从API version 20开始废弃,建议使用[getMediaByName](#getmediabyname10)或[getMediaContent](#getmediacontent10)替代。 5699 5700**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 5701 5702**系统能力:** SystemCapability.Global.ResourceManager 5703 5704**模型约束:** 此接口仅可在Stage模型下使用。 5705 5706**参数:** 5707 5708| 参数名 | 类型 | 必填 | 说明 | 5709| -------- | ------------------------------- | ---- | ------------------ | 5710| resource | [Resource](#resource9) | 是 | 资源信息。 | 5711| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 5712| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Uint8Array> | 是 | 回调函数,返回resource对象对应的媒体文件内容。 | 5713 5714**错误码:** 5715 5716以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 5717 5718| 错误码ID | 错误信息 | 5719| -------- | ---------------------------------------- | 5720| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 5721| 9001001 | Invalid resource ID. | 5722| 9001002 | No matching resource is found based on the resource ID. | 5723 5724**示例:** 5725 ```ts 5726 import { resourceManager } from '@kit.LocalizationKit'; 5727 import { BusinessError } from '@kit.BasicServicesKit'; 5728 5729 let resource: resourceManager.Resource = { 5730 bundleName: "com.example.myapplication", 5731 moduleName: "entry", 5732 id: $r('app.media.test').id 5733 }; 5734 try { 5735 this.context.resourceManager.getMediaContent(resource, 120, (error: BusinessError, value: Uint8Array) => { 5736 if (error != null) { 5737 console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); 5738 } else { 5739 let media = value; 5740 } 5741 }); 5742 } catch (error) { 5743 let code = (error as BusinessError).code; 5744 let message = (error as BusinessError).message; 5745 console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`); 5746 } 5747 ``` 5748 5749### getMediaContent<sup>(deprecated)</sup> 5750 5751getMediaContent(resource: Resource): Promise<Uint8Array> 5752 5753获取指定resource对象对应的媒体文件内容,使用Promise异步回调。 5754 5755> **说明** 5756> 5757> 从API version 9开始支持,从API version 20开始废弃,建议使用[getMediaByName](#getmediabyname9-1)或[getMediaContent](#getmediacontent9-1)替代。 5758 5759**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 5760 5761**系统能力:** SystemCapability.Global.ResourceManager 5762 5763**模型约束:** 此接口仅可在Stage模型下使用。 5764 5765**参数:** 5766 5767| 参数名 | 类型 | 必填 | 说明 | 5768| -------- | ---------------------- | ---- | ---- | 5769| resource | [Resource](#resource9) | 是 | 资源信息。 | 5770 5771**返回值:** 5772 5773| 类型 | 说明 | 5774| ------------------------- | ------------------- | 5775| Promise<Uint8Array> | Promise对象,返回resource对象对应的媒体文件内容。 | 5776 5777**错误码:** 5778 5779以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 5780 5781| 错误码ID | 错误信息 | 5782| -------- | ---------------------------------------- | 5783| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5784| 9001001 | Invalid resource ID. | 5785| 9001002 | No matching resource is found based on the resource ID. | 5786 5787**示例:** 5788 ```ts 5789 import { resourceManager } from '@kit.LocalizationKit'; 5790 import { BusinessError } from '@kit.BasicServicesKit'; 5791 5792 let resource: resourceManager.Resource = { 5793 bundleName: "com.example.myapplication", 5794 moduleName: "entry", 5795 id: $r('app.media.test').id 5796 }; 5797 try { 5798 this.context.resourceManager.getMediaContent(resource).then((value: Uint8Array) => { 5799 let media = value; 5800 }).catch((error: BusinessError) => { 5801 console.error("getMediaContent promise error is " + error); 5802 }); 5803 } catch (error) { 5804 let code = (error as BusinessError).code; 5805 let message = (error as BusinessError).message; 5806 console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`); 5807 } 5808 ``` 5809 5810### getMediaContent<sup>(deprecated)</sup> 5811 5812getMediaContent(resource: Resource, density: number): Promise<Uint8Array> 5813 5814获取指定resource对象对应的指定屏幕密度媒体文件内容,使用Promise异步回调。 5815 5816> **说明** 5817> 5818> 从API version 10开始支持,从API version 20开始废弃,建议使用[getMediaByName](#getmediabyname10-1)或[getMediaContent](#getmediacontent10-1)替代。 5819 5820**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 5821 5822**系统能力:** SystemCapability.Global.ResourceManager 5823 5824**模型约束:** 此接口仅可在Stage模型下使用。 5825 5826**参数:** 5827 5828| 参数名 | 类型 | 必填 | 说明 | 5829| -------- | ---------------------- | ---- | ---- | 5830| resource | [Resource](#resource9) | 是 | 资源信息。 | 5831| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 5832 5833**返回值:** 5834 5835| 类型 | 说明 | 5836| ------------------------- | ------------------- | 5837| Promise<Uint8Array> | Promise对象,返回resource对象对应的媒体文件内容。 | 5838 5839**错误码:** 5840 5841以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 5842 5843| 错误码ID | 错误信息 | 5844| -------- | ---------------------------------------- | 5845| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 5846| 9001001 | Invalid resource ID. | 5847| 9001002 | No matching resource is found based on the resource ID. | 5848 5849**示例:** 5850 ```ts 5851 import { resourceManager } from '@kit.LocalizationKit'; 5852 import { BusinessError } from '@kit.BasicServicesKit'; 5853 5854 let resource: resourceManager.Resource = { 5855 bundleName: "com.example.myapplication", 5856 moduleName: "entry", 5857 id: $r('app.media.test').id 5858 }; 5859 try { 5860 this.context.resourceManager.getMediaContent(resource, 120).then((value: Uint8Array) => { 5861 let media = value; 5862 }).catch((error: BusinessError) => { 5863 console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); 5864 }); 5865 } catch (error) { 5866 let code = (error as BusinessError).code; 5867 let message = (error as BusinessError).message; 5868 console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`); 5869 } 5870 ``` 5871 5872### getMediaBase64<sup>(deprecated)</sup> 5873 5874getMediaBase64(resId: number, callback: AsyncCallback<string>): void 5875 5876获取指定资源ID对应的图片资源Base64编码,使用callback异步回调。 5877 5878> **说明** 5879> 5880> 从API version 6开始支持,从API version 9开始废弃,建议使用[getMediaContentBase64](#getmediacontentbase649)替代。 5881 5882**系统能力:** SystemCapability.Global.ResourceManager 5883 5884**参数:** 5885 5886| 参数名 | 类型 | 必填 | 说明 | 5887| -------- | --------------------------- | ---- | ------------------------ | 5888| resId | number | 是 | 资源ID值。 | 5889| callback | [AsyncCallback](#asynccallbackdeprecated)<string> | 是 | 回调函数,返回资源ID值对应的图片资源Base64编码。 | 5890 5891**示例:** 5892 ```ts 5893 resourceManager.getResourceManager((error, mgr) => { 5894 mgr.getMediaBase64($r('app.media.test').id, ((error: Error, value: string) => { 5895 if (error != null) { 5896 console.error("error is " + error); 5897 } else { 5898 let media = value; 5899 } 5900 }); 5901 }); 5902 ``` 5903 5904 5905### getMediaBase64<sup>(deprecated)</sup> 5906 5907getMediaBase64(resId: number): Promise<string> 5908 5909获取指定资源ID对应的图片资源Base64编码,使用Promise异步回调。 5910 5911> **说明** 5912> 5913> 从API version 6开始支持,从API version 9开始废弃,建议使用[getMediaContentBase64](#getmediacontentbase649-1)替代。 5914 5915**系统能力:** SystemCapability.Global.ResourceManager 5916 5917**参数:** 5918 5919| 参数名 | 类型 | 必填 | 说明 | 5920| ----- | ------ | ---- | ----- | 5921| resId | number | 是 | 资源ID值。 | 5922 5923**返回值:** 5924 5925| 类型 | 说明 | 5926| --------------------- | -------------------- | 5927| Promise<string> | Promise对象,返回资源ID值对应的图片资源Base64编码。 | 5928 5929**示例:** 5930 ```ts 5931 import { BusinessError } from '@kit.BasicServicesKit'; 5932 5933 resourceManager.getResourceManager((error, mgr) => { 5934 mgr.getMediaBase64($r('app.media.test').id).then((value: string) => { 5935 let media = value; 5936 }).catch((error: BusinessError) => { 5937 console.error("getMediaBase64 promise error is " + error); 5938 }); 5939 }); 5940 ``` 5941### getMediaContentBase64Sync<sup>(deprecated)</sup> 5942 5943getMediaContentBase64Sync(resource: Resource, density?: number): string 5944 5945获取指定resource对象对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回。 5946 5947> **说明** 5948> 5949> 从API version 10开始支持,从API version 20开始废弃,建议使用[getMediaBase64ByNameSync](#getmediabase64bynamesync10)或[getMediaContentBase64Sync](#getmediacontentbase64sync10)替代。 5950 5951**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 5952 5953**系统能力:** SystemCapability.Global.ResourceManager 5954 5955**模型约束:** 此接口仅可在Stage模型下使用。 5956 5957**参数:** 5958 5959| 参数名 | 类型 | 必填 | 说明 | 5960| ----- | ------ | ---- | ----- | 5961| resource | [Resource](#resource9) | 是 | 资源信息。 | 5962| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 5963 5964**返回值:** 5965 5966| 类型 | 说明 | 5967| --------------------- | ----------- | 5968| string | resource对象对应的图片资源Base64编码。 | 5969 5970**错误码:** 5971 5972以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 5973 5974| 错误码ID | 错误信息 | 5975| -------- | ---------------------------------------- | 5976| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 5977| 9001001 | Invalid resource ID. | 5978| 9001002 | No matching resource is found based on the resource ID. | 5979 5980**示例:** 5981 ```ts 5982 import { resourceManager } from '@kit.LocalizationKit'; 5983 import { BusinessError } from '@kit.BasicServicesKit'; 5984 5985 let resource: resourceManager.Resource = { 5986 bundleName: "com.example.myapplication", 5987 moduleName: "entry", 5988 id: $r('app.media.test').id 5989 }; 5990 try { 5991 this.context.resourceManager.getMediaContentBase64Sync(resource); // 默认屏幕密度 5992 } catch (error) { 5993 let code = (error as BusinessError).code; 5994 let message = (error as BusinessError).message; 5995 console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`); 5996 } 5997 5998 try { 5999 this.context.resourceManager.getMediaContentBase64Sync(resource, 120); // 指定屏幕密度 6000 } catch (error) { 6001 let code = (error as BusinessError).code; 6002 let message = (error as BusinessError).message; 6003 console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`); 6004 } 6005 ``` 6006### getMediaContentBase64<sup>(deprecated)</sup> 6007 6008getMediaContentBase64(resource: Resource, callback: _AsyncCallback<string>): void 6009 6010获取指定resource对象对应的图片资源Base64编码,使用callback异步回调。 6011 6012> **说明** 6013> 6014> 从API version 9开始支持,从API version 20开始废弃,建议使用[getMediaBase64ByName](#getmediabase64byname9)或[getMediaContentBase64](#getmediacontentbase649)替代。 6015 6016**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 6017 6018**系统能力:** SystemCapability.Global.ResourceManager 6019 6020**模型约束:** 此接口仅可在Stage模型下使用。 6021 6022**参数:** 6023 6024| 参数名 | 类型 | 必填 | 说明 | 6025| -------- | --------------------------- | ---- | ------------------------ | 6026| resource | [Resource](#resource9) | 是 | 资源信息。 | 6027| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 回调函数,返回resource对象对应的图片资源Base64编码。 | 6028 6029**错误码:** 6030 6031以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 6032 6033| 错误码ID | 错误信息 | 6034| -------- | ---------------------------------------- | 6035| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 6036| 9001001 | Invalid resource ID. | 6037| 9001002 | No matching resource is found based on the resource ID. | 6038 6039**示例:** 6040 ```ts 6041 import { resourceManager } from '@kit.LocalizationKit'; 6042 import { BusinessError } from '@kit.BasicServicesKit'; 6043 6044 let resource: resourceManager.Resource = { 6045 bundleName: "com.example.myapplication", 6046 moduleName: "entry", 6047 id: $r('app.media.test').id 6048 }; 6049 try { 6050 this.context.resourceManager.getMediaContentBase64(resource, (error: BusinessError, value: string) => { 6051 if (error != null) { 6052 console.error("error is " + error); 6053 } else { 6054 let media = value; 6055 } 6056 }); 6057 } catch (error) { 6058 let code = (error as BusinessError).code; 6059 let message = (error as BusinessError).message; 6060 console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 6061 } 6062 ``` 6063 6064### getMediaContentBase64<sup>(deprecated)</sup> 6065 6066getMediaContentBase64(resource: Resource, density: number, callback: _AsyncCallback<string>): void 6067 6068获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用callback异步回调。 6069 6070> **说明** 6071> 6072> 从API version 10开始支持,从API version 20开始废弃,建议使用[getMediaBase64ByName](#getmediabase64byname10)或[getMediaContentBase64](#getmediacontentbase6410)替代。 6073 6074**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 6075 6076**系统能力:** SystemCapability.Global.ResourceManager 6077 6078**模型约束:** 此接口仅可在Stage模型下使用。 6079 6080**参数:** 6081 6082| 参数名 | 类型 | 必填 | 说明 | 6083| -------- | --------------------------- | ---- | ------------------------ | 6084| resource | [Resource](#resource9) | 是 | 资源信息。 | 6085| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 6086| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 回调函数,返回resource对象对应的图片资源Base64编码。 | 6087 6088**错误码:** 6089 6090以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 6091 6092| 错误码ID | 错误信息 | 6093| -------- | ---------------------------------------- | 6094| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 6095| 9001001 | Invalid resource ID. | 6096| 9001002 | No matching resource is found based on the resource ID. | 6097 6098**示例:** 6099 ```ts 6100 import { resourceManager } from '@kit.LocalizationKit'; 6101 import { BusinessError } from '@kit.BasicServicesKit'; 6102 6103 let resource: resourceManager.Resource = { 6104 bundleName: "com.example.myapplication", 6105 moduleName: "entry", 6106 id: $r('app.media.test').id 6107 }; 6108 try { 6109 this.context.resourceManager.getMediaContentBase64(resource, 120, (error: BusinessError, value: string) => { 6110 if (error != null) { 6111 console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); 6112 } else { 6113 let media = value; 6114 } 6115 }); 6116 } catch (error) { 6117 let code = (error as BusinessError).code; 6118 let message = (error as BusinessError).message; 6119 console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 6120 } 6121 ``` 6122 6123### getMediaContentBase64<sup>(deprecated)</sup> 6124 6125getMediaContentBase64(resource: Resource): Promise<string> 6126 6127获取指定resource对象对应的图片资源Base64编码,使用Promise异步回调。 6128 6129> **说明** 6130> 6131> 从API version 9开始支持,从API version 20开始废弃,建议使用[getMediaBase64ByName](#getmediabase64byname9-1)或[getMediaContentBase64](#getmediacontentbase649-1)替代。 6132 6133**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 6134 6135**系统能力:** SystemCapability.Global.ResourceManager 6136 6137**模型约束:** 此接口仅可在Stage模型下使用。 6138 6139**参数:** 6140 6141| 参数名 | 类型 | 必填 | 说明 | 6142| -------- | ---------------------- | ---- | ---- | 6143| resource | [Resource](#resource9) | 是 | 资源信息。 | 6144 6145**返回值:** 6146 6147| 类型 | 说明 | 6148| --------------------- | ------------------------- | 6149| Promise<string> | Promise对象,返回resource对象对应的图片资源Base64编码。 | 6150 6151**错误码:** 6152 6153以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 6154 6155| 错误码ID | 错误信息 | 6156| -------- | ---------------------------------------- | 6157| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 6158| 9001001 | Invalid resource ID. | 6159| 9001002 | No matching resource is found based on the resource ID. | 6160 6161**示例:** 6162 ```ts 6163 import { resourceManager } from '@kit.LocalizationKit'; 6164 import { BusinessError } from '@kit.BasicServicesKit'; 6165 6166 let resource: resourceManager.Resource = { 6167 bundleName: "com.example.myapplication", 6168 moduleName: "entry", 6169 id: $r('app.media.test').id 6170 }; 6171 try { 6172 this.context.resourceManager.getMediaContentBase64(resource).then((value: string) => { 6173 let media = value; 6174 }).catch((error: BusinessError) => { 6175 console.error("getMediaContentBase64 promise error is " + error); 6176 }); 6177 } catch (error) { 6178 let code = (error as BusinessError).code; 6179 let message = (error as BusinessError).message; 6180 console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 6181 } 6182 ``` 6183 6184### getMediaContentBase64<sup>(deprecated)</sup> 6185 6186getMediaContentBase64(resource: Resource, density: number): Promise<string> 6187 6188获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用Promise异步回调。 6189 6190> **说明** 6191> 6192> 从API version 10开始支持,从API version 20开始废弃,建议使用[getMediaBase64ByName](#getmediabase64byname10-1)或[getMediaContentBase64](#getmediacontentbase6410-1)替代。 6193 6194**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 6195 6196**系统能力:** SystemCapability.Global.ResourceManager 6197 6198**模型约束:** 此接口仅可在Stage模型下使用。 6199 6200**参数:** 6201 6202| 参数名 | 类型 | 必填 | 说明 | 6203| -------- | ---------------------- | ---- | ---- | 6204| resource | [Resource](#resource9) | 是 | 资源信息。 | 6205| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 6206 6207**返回值:** 6208 6209| 类型 | 说明 | 6210| --------------------- | ------------------------- | 6211| Promise<string> | Promise对象,返回resource对象对应的图片资源Base64编码。 | 6212 6213**错误码:** 6214 6215以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 6216 6217| 错误码ID | 错误信息 | 6218| -------- | ---------------------------------------- | 6219| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 6220| 9001001 | Invalid resource ID. | 6221| 9001002 | No matching resource is found based on the resource ID. | 6222 6223**示例:** 6224 ```ts 6225 import { resourceManager } from '@kit.LocalizationKit'; 6226 import { BusinessError } from '@kit.BasicServicesKit'; 6227 6228 let resource: resourceManager.Resource = { 6229 bundleName: "com.example.myapplication", 6230 moduleName: "entry", 6231 id: $r('app.media.test').id 6232 }; 6233 try { 6234 this.context.resourceManager.getMediaContentBase64(resource, 120).then((value: string) => { 6235 let media = value; 6236 }).catch((error: BusinessError) => { 6237 console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); 6238 }); 6239 } catch (error) { 6240 let code = (error as BusinessError).code; 6241 let message = (error as BusinessError).message; 6242 console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 6243 } 6244 ``` 6245### getDrawableDescriptor<sup>(deprecated)</sup> 6246 6247getDrawableDescriptor(resource: Resource, density?: number, type?: number): DrawableDescriptor 6248 6249获取指定resource对应的DrawableDescriptor对象,用于图标的显示,使用同步方式返回。 6250 6251> **说明** 6252> 6253> 从API version 10开始支持,从API version 20开始废弃,建议使用[getDrawableDescriptorByName](#getdrawabledescriptorbyname10)或[getDrawableDescriptor](#getdrawabledescriptor10)替代。 6254 6255**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 6256 6257**系统能力:** SystemCapability.Global.ResourceManager 6258 6259**模型约束:** 此接口仅可在Stage模型下使用。 6260 6261**参数:** 6262 6263| 参数名 | 类型 | 必填 | 说明 | 6264| -------- | ---------------------- | ---- | ---- | 6265| resource | [Resource](#resource9) | 是 | 资源信息。 | 6266| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 6267| type<sup>11+</sup> | number | 否 | - 1表示获取主题资源包中应用的分层图标资源。<br> - 0或缺省表示获取应用自身图标资源。 | 6268 6269**返回值:** 6270 6271| 类型 | 说明 | 6272| ------- | ----------------- | 6273| [DrawableDescriptor](../apis-arkui/js-apis-arkui-drawableDescriptor.md#drawabledescriptor) | 资源ID值对应的DrawableDescriptor对象。 | 6274 6275**错误码:** 6276 6277以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 6278 6279| 错误码ID | 错误信息 | 6280| -------- | ---------------------------------------- | 6281| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 6282| 9001001 | Invalid resource ID. | 6283| 9001002 | No matching resource is found based on the resource ID. | 6284 6285**示例:** 6286 ```ts 6287 import { resourceManager } from '@kit.LocalizationKit'; 6288 import { BusinessError } from '@kit.BasicServicesKit'; 6289 import { DrawableDescriptor } from '@kit.ArkUI'; 6290 6291 let resource: resourceManager.Resource = { 6292 bundleName: "com.example.myapplication", 6293 moduleName: "entry", 6294 id: $r('app.media.icon').id 6295 }; 6296 try { 6297 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor(resource); 6298 } catch (error) { 6299 let code = (error as BusinessError).code; 6300 let message = (error as BusinessError).message; 6301 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 6302 } 6303 try { 6304 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor(resource, 120); 6305 } catch (error) { 6306 let code = (error as BusinessError).code; 6307 let message = (error as BusinessError).message; 6308 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 6309 } 6310 try { 6311 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor(resource, 0, 1); 6312 } catch (error) { 6313 let code = (error as BusinessError).code; 6314 let message = (error as BusinessError).message; 6315 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 6316 } 6317 ``` 6318 6319### getIntPluralStringValueSync<sup>(deprecated)</sup> 6320 6321getIntPluralStringValueSync(resource: Resource, num: number, ...args: Array<string | number>): string 6322 6323获取指定resource对象对应的[单复数](../../internationalization/l10n-singular-plural.md)字符串,并根据args参数对字符串进行格式化,使用同步方式返回。 6324 6325> **说明** 6326> 6327> - 从API version 18开始支持,从API version 20开始废弃,建议使用[getIntPluralStringByNameSync](#getintpluralstringbynamesync18)或[getIntPluralStringValueSync](#getintpluralstringvaluesync18)替代。 6328> 6329> - 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 6330 6331**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 6332 6333**系统能力:** SystemCapability.Global.ResourceManager 6334 6335**模型约束:** 此接口仅可在Stage模型下使用。 6336 6337**参数:** 6338 6339| 参数名 | 类型 | 必填 | 说明 | 6340| -------- | ----------------------- | ---- | ------------------------------------------------------------ | 6341| resource | [Resource](#resource9) | 是 | 资源信息。 | 6342| num | number | 是 | 数量值(整数)。根据当前语言的[单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)获取该数量值对应的字符串。 | 6343| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:`%d`、`%f`、`%s`、`%%`、`%数字$d`、`%数字$f`、`%数字$s`。<br>说明:`%%`转义为`%`; `%数字$d`中的数字表示使用args中的第几个参数。<br>举例:`%%d`格式化后为`%d`字符串; `%1$d`表示使用第一个参数。 | 6344 6345**返回值:** 6346 6347| 类型 | 说明 | 6348| ------ | ------------------------------------ | 6349| string | resource对象对应的格式化单复数字符串。 | 6350 6351**错误码:** 6352 6353以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 6354 6355| 错误码ID | 错误信息 | 6356| -------- | ------------------------------------------------------------ | 6357| 9001001 | Invalid resource ID. | 6358| 9001002 | No matching resource is found based on the resource ID. | 6359| 9001006 | The resource is referenced cyclically. | 6360| 9001007 | Failed to format the resource obtained based on the resource ID. | 6361 6362**示例:** 6363 ```json 6364 // 资源文件路径: src/main/resources/base/element/plural.json 6365 { 6366 "plural": [ 6367 { 6368 "name": "format_test", 6369 "value": [ 6370 { 6371 "quantity": "one", 6372 "value": "There is %d apple in the %s, the total amount is %f kg." 6373 }, 6374 { 6375 "quantity": "other", 6376 "value": "There are %d apples in the %s, the total amount is %f kg." 6377 } 6378 ] 6379 } 6380 ] 6381 } 6382 ``` 6383 ```ts 6384 import { resourceManager } from '@kit.LocalizationKit'; 6385 import { BusinessError } from '@kit.BasicServicesKit'; 6386 6387 let resource: resourceManager.Resource = { 6388 bundleName: "com.example.myapplication", 6389 moduleName: "entry", 6390 id: $r('app.plural.format_test').id 6391 }; 6392 6393 try { 6394 // 根据语言单复数规则,参数num取值为1,英文环境下对应单复数类别为one 6395 // 在资源文件中用quantity字段表示单复数类别,因此会获取quantity为one的字符串 6396 let pluralStr = this.context.resourceManager.getIntPluralStringValueSync(resource, 1, 1, "basket", 0.3); 6397 console.log(`getIntPluralStringValueSync, result: ${pluralStr}`); 6398 // 打印输出结果: getIntPluralStringValueSync, result: There is 1 apple in the basket, the total amount is 0.3 kg. 6399 } catch (error) { 6400 let code = (error as BusinessError).code; 6401 let message = (error as BusinessError).message; 6402 console.error(`getIntPluralStringValueSync failed, error code: ${code}, message: ${message}.`); 6403 } 6404 ``` 6405 6406### getDoublePluralStringValueSync<sup>(deprecated)</sup> 6407 6408getDoublePluralStringValueSync(resource: Resource, num: number, ...args: Array<string | number>): string 6409 6410获取指定resource对象对应的[单复数](../../internationalization/l10n-singular-plural.md)字符串,并根据args参数对字符串进行格式化,使用同步方式返回。 6411 6412> **说明** 6413> 6414> - 从API version 18开始支持,从API version 20开始废弃,建议使用[getDoublePluralStringByNameSync](#getdoublepluralstringbynamesync18)或[getDoublePluralStringValueSync](#getdoublepluralstringvaluesync18)替代。 6415> 6416> - 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 6417 6418**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 6419 6420**系统能力:** SystemCapability.Global.ResourceManager 6421 6422**模型约束:** 此接口仅可在Stage模型下使用。 6423 6424**参数:** 6425 6426| 参数名 | 类型 | 必填 | 说明 | 6427| -------- | ----------------------- | ---- | ------------------------------------------------------------ | 6428| resource | [Resource](#resource9) | 是 | 资源信息。 | 6429| num | number | 是 | 数量值(浮点数)。根据当前语言的[单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)获取该数量值对应的字符串。 | 6430| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:`%d`、`%f`、`%s`、`%%`、`%数字$d`、`%数字$f`、`%数字$s`。<br>说明:`%%`转义为`%`; `%数字$d`中的数字表示使用args中的第几个参数。<br>举例:`%%d`格式化后为`%d`字符串; `%1$d`表示使用第一个参数。 | 6431 6432**返回值:** 6433 6434| 类型 | 说明 | 6435| ------ | ---------------------------------------- | 6436| string | resource对象对应的格式化单复数字符串。 | 6437 6438**错误码:** 6439 6440以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 6441 6442| 错误码ID | 错误信息 | 6443| -------- | ------------------------------------------------------------ | 6444| 9001001 | Invalid resource ID. | 6445| 9001002 | No matching resource is found based on the resource ID. | 6446| 9001006 | The resource is referenced cyclically. | 6447| 9001007 | Failed to format the resource obtained based on the resource ID. | 6448 6449**示例:** 6450 ```json 6451 // 资源文件路径: src/main/resources/base/element/plural.json 6452 { 6453 "plural": [ 6454 { 6455 "name": "format_test", 6456 "value": [ 6457 { 6458 "quantity": "one", 6459 "value": "There is %d apple in the %s, the total amount is %f kg." 6460 }, 6461 { 6462 "quantity": "other", 6463 "value": "There are %d apples in the %s, the total amount is %f kg." 6464 } 6465 ] 6466 } 6467 ] 6468 } 6469 ``` 6470 ```ts 6471 import { resourceManager } from '@kit.LocalizationKit'; 6472 import { BusinessError } from '@kit.BasicServicesKit'; 6473 6474 let resource: resourceManager.Resource = { 6475 bundleName: "com.example.myapplication", 6476 moduleName: "entry", 6477 id: $r('app.plural.format_test').id 6478 }; 6479 6480 try { 6481 // 根据语言单复数规则,参数num取值为2.1,英文环境下对应单复数类别为other 6482 // 在资源文件中用quantity字段表示单复数类别,因此会获取quantity为other的字符串 6483 let pluralStr = this.context.resourceManager.getDoublePluralStringValueSync(resource, 2.1, 2, "basket", 0.6); 6484 console.log(`getDoublePluralStringValueSync, result: ${pluralStr}`); 6485 // 打印输出结果: getIntPluralStringValueSync, result: There are 2 apples in the basket, the total amount is 0.6 kg. 6486 } catch (error) { 6487 let code = (error as BusinessError).code; 6488 let message = (error as BusinessError).message; 6489 console.error(`getDoublePluralStringValueSync failed, error code: ${code}, message: ${message}.`); 6490 } 6491 ``` 6492 6493### getPluralStringValueSync<sup>(deprecated)</sup> 6494 6495getPluralStringValueSync(resId: number, num: number): string 6496 6497获取指定资源ID,指定资源数量的单复数字符串,使用同步方式返回。 6498 6499> **说明** 6500> 6501> 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 6502> 6503> 从API version 10开始支持,从API version 18开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18)替代。 6504 6505**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 6506 6507**系统能力:** SystemCapability.Global.ResourceManager 6508 6509**参数:** 6510 6511| 参数名 | 类型 | 必填 | 说明 | 6512| ------ | ------ | ---- | ------------------------------------------------------------ | 6513| resId | number | 是 | 资源ID值。 | 6514| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 6515 6516**返回值:** 6517 6518| 类型 | 说明 | 6519| ------ | ------------------------------------------------ | 6520| string | 根据指定数量获取指定ID字符串表示的单复数字符串。 | 6521 6522**错误码:** 6523 6524以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 6525 6526| 错误码ID | 错误信息 | 6527| -------- | ------------------------------------------------------------ | 6528| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 6529| 9001001 | Invalid resource ID. | 6530| 9001002 | No matching resource is found based on the resource ID. | 6531| 9001006 | The resource is referenced cyclically. | 6532 6533**示例:** 6534 ```json 6535 // 资源文件路径: src/main/resources/base/element/plural.json 6536 { 6537 "plural": [ 6538 { 6539 "name": "test", 6540 "value": [ 6541 { 6542 "quantity": "one", 6543 "value": "%d apple" 6544 }, 6545 { 6546 "quantity": "other", 6547 "value": "%d apples" 6548 } 6549 ] 6550 } 6551 ] 6552 } 6553 ``` 6554 ```ts 6555 import { BusinessError } from '@kit.BasicServicesKit'; 6556 6557 try { 6558 // 根据语言单复数规则,参数num取值为1,英文环境下对应单复数类别为one 6559 // 在资源文件中用quantity字段表示单复数类别,因此会获取quantity为one的字符串 6560 let pluralValue = this.context.resourceManager.getPluralStringValueSync($r('app.plural.test').id, 1); 6561 console.log(`getPluralStringValueSync, result: ${pluralValue}`); 6562 // 打印输出结果: getPluralStringValueSync, result: 1 apple 6563 } catch (error) { 6564 let code = (error as BusinessError).code; 6565 let message = (error as BusinessError).message; 6566 console.error(`getPluralStringValueSync failed, error code: ${code}, message: ${message}.`); 6567 } 6568 ``` 6569 6570### getPluralStringValueSync<sup>(deprecated)</sup> 6571 6572getPluralStringValueSync(resource: Resource, num: number): string 6573 6574获取指定资源信息,指定资源数量的单复数字符串,使用同步方式返回。 6575 6576> **说明** 6577> 6578> 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 6579> 6580> 从API version 10开始支持,从API version 18开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18)替代。 6581 6582**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 6583 6584**系统能力:** SystemCapability.Global.ResourceManager 6585 6586**模型约束:** 此接口仅可在Stage模型下使用。 6587 6588**参数:** 6589 6590| 参数名 | 类型 | 必填 | 说明 | 6591| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 6592| resource | [Resource](#resource9) | 是 | 资源信息。 | 6593| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 6594 6595**返回值:** 6596 6597| 类型 | 说明 | 6598| ------ | ---------------------------------------------------- | 6599| string | 根据指定数量获取指定resource对象表示的单复数字符串。 | 6600 6601**错误码:** 6602 6603以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 6604 6605| 错误码ID | 错误信息 | 6606| -------- | ------------------------------------------------------------ | 6607| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 6608| 9001001 | Invalid resource ID. | 6609| 9001002 | No matching resource is found based on the resource ID. | 6610| 9001006 | The resource is referenced cyclically. | 6611 6612**示例:** 6613 ```json 6614 // 资源文件路径: src/main/resources/base/element/plural.json 6615 { 6616 "plural": [ 6617 { 6618 "name": "test", 6619 "value": [ 6620 { 6621 "quantity": "one", 6622 "value": "%d apple" 6623 }, 6624 { 6625 "quantity": "other", 6626 "value": "%d apples" 6627 } 6628 ] 6629 } 6630 ] 6631 } 6632 ``` 6633 ```ts 6634 import { resourceManager } from '@kit.LocalizationKit'; 6635 import { BusinessError } from '@kit.BasicServicesKit'; 6636 6637 let resource: resourceManager.Resource = { 6638 bundleName: "com.example.myapplication", 6639 moduleName: "entry", 6640 id: $r('app.plural.test').id 6641 }; 6642 try { 6643 // 根据语言单复数规则,参数num取值为1,英文环境下对应单复数类别为one 6644 // 在资源文件中用quantity字段表示单复数类别,因此会获取quantity为one的字符串 6645 let pluralValue = this.context.resourceManager.getPluralStringValueSync(resource, 1); 6646 console.log(`getPluralStringValueSync, result: ${pluralValue}`); 6647 // 打印输出结果: getPluralStringValueSync, result: 1 apple 6648 } catch (error) { 6649 let code = (error as BusinessError).code; 6650 let message = (error as BusinessError).message; 6651 console.error(`getPluralStringValueSync failed, error code: ${code}, message: ${message}.`); 6652 } 6653 ``` 6654 6655### getPluralStringByNameSync<sup>(deprecated)</sup> 6656 6657getPluralStringByNameSync(resName: string, num: number): string 6658 6659获取指定资源名称,指定资源数量的单复数字符串,使用同步方式返回。 6660 6661> **说明** 6662> 6663> 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 6664> 6665> 从API version 10开始支持,从API version 18开始废弃,建议使用[getIntPluralStringByNameSync](#getintpluralstringbynamesync18)替代。 6666 6667**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 6668 6669**系统能力:** SystemCapability.Global.ResourceManager 6670 6671**参数:** 6672 6673| 参数名 | 类型 | 必填 | 说明 | 6674| ------- | ------ | ---- | ------------------------------------------------------------ | 6675| resName | string | 是 | 资源名称。 | 6676| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 6677 6678**返回值:** 6679 6680| 类型 | 说明 | 6681| ------ | ------------------------------------------------ | 6682| string | 根据指定数量获取指定资源名称表示的单复数字符串。 | 6683 6684**错误码:** 6685 6686以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 6687 6688| 错误码ID | 错误信息 | 6689| -------- | ------------------------------------------------------------ | 6690| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 6691| 9001003 | Invalid resource name. | 6692| 9001004 | No matching resource is found based on the resource name. | 6693| 9001006 | The resource is referenced cyclically. | 6694 6695**示例:** 6696 ```json 6697 // 资源文件路径: src/main/resources/base/element/plural.json 6698 { 6699 "plural": [ 6700 { 6701 "name": "test", 6702 "value": [ 6703 { 6704 "quantity": "one", 6705 "value": "%d apple" 6706 }, 6707 { 6708 "quantity": "other", 6709 "value": "%d apples" 6710 } 6711 ] 6712 } 6713 ] 6714 } 6715 ``` 6716 ```ts 6717 import { BusinessError } from '@kit.BasicServicesKit'; 6718 6719 try { 6720 // 根据语言单复数规则,参数num取值为1,英文环境下对应单复数类别为one 6721 // 在资源文件中用quantity字段表示单复数类别,因此会获取quantity为one的字符串 6722 let pluralValue = this.context.resourceManager.getPluralStringByNameSync("test", 1); 6723 console.log(`getPluralStringByNameSync, result: ${pluralValue}`); 6724 // 打印输出结果: getPluralStringByNameSync, result: 1 apple 6725 } catch (error) { 6726 let code = (error as BusinessError).code; 6727 let message = (error as BusinessError).message; 6728 console.error(`getPluralStringByNameSync failed, error code: ${code}, message: ${message}.`); 6729 } 6730 ``` 6731 6732### getPluralStringValue<sup>(deprecated)</sup> 6733 6734getPluralStringValue(resId: number, num: number, callback: _AsyncCallback<string>): void 6735 6736获取指定资源ID,指定资源数量的单复数字符串,使用callback异步回调。 6737 6738> **说明** 6739> 6740> 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 6741> 6742> 从API version 9开始支持,从API version 18开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18)替代。 6743 6744**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 6745 6746**系统能力:** SystemCapability.Global.ResourceManager 6747 6748**参数:** 6749 6750| 参数名 | 类型 | 必填 | 说明 | 6751| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 6752| resId | number | 是 | 资源ID值。 | 6753| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 6754| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 回调函数,返回资源ID值对应的指定数量的单复数字符串。 | 6755 6756**错误码:** 6757 6758以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 6759 6760| 错误码ID | 错误信息 | 6761| -------- | ------------------------------------------------------------ | 6762| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 6763| 9001001 | Invalid resource ID. | 6764| 9001002 | No matching resource is found based on the resource ID. | 6765| 9001006 | The resource is referenced cyclically. | 6766 6767**示例:** 6768 ```json 6769 // 资源文件路径: src/main/resources/base/element/plural.json 6770 { 6771 "plural": [ 6772 { 6773 "name": "test", 6774 "value": [ 6775 { 6776 "quantity": "one", 6777 "value": "%d apple" 6778 }, 6779 { 6780 "quantity": "other", 6781 "value": "%d apples" 6782 } 6783 ] 6784 } 6785 ] 6786 } 6787 ``` 6788 ```ts 6789 import { BusinessError } from '@kit.BasicServicesKit'; 6790 6791 // 根据语言单复数规则,参数num取值为1,英文环境下对应单复数类别为one 6792 // 在资源文件中用quantity字段表示单复数类别,因此会获取quantity为one的字符串 6793 this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1, 6794 (error: BusinessError, value: string) => { 6795 if (error != null) { 6796 console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`); 6797 } else { 6798 console.log(`getPluralStringValue, result: ${value}`); 6799 // 打印输出结果: getPluralStringValue, result: 1 apple 6800 } 6801 }); 6802 ``` 6803 6804### getPluralStringValue<sup>(deprecated)</sup> 6805 6806getPluralStringValue(resId: number, num: number): Promise<string> 6807 6808获取指定资源ID,指定资源数量的单复数字符串,使用Promise异步回调。 6809 6810> **说明** 6811> 6812> 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 6813> 6814> 从API version 9开始支持,从API version 18开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18)替代。 6815 6816**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 6817 6818**系统能力:** SystemCapability.Global.ResourceManager 6819 6820**参数:** 6821 6822| 参数名 | 类型 | 必填 | 说明 | 6823| ------ | ------ | ---- | ------------------------------------------------------------ | 6824| resId | number | 是 | 资源ID值。 | 6825| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 6826 6827**返回值:** 6828 6829| 类型 | 说明 | 6830| --------------------- | ---------------------------------------------------- | 6831| Promise<string> | Promise对象,返回资源ID值对应的指定数量的单复数字符串。 | 6832 6833**错误码:** 6834 6835以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 6836 6837| 错误码ID | 错误信息 | 6838| -------- | ------------------------------------------------------------ | 6839| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 6840| 9001001 | Invalid resource ID. | 6841| 9001002 | No matching resource is found based on the resource ID. | 6842| 9001006 | The resource is referenced cyclically. | 6843 6844**示例:** 6845 ```json 6846 // 资源文件路径: src/main/resources/base/element/plural.json 6847 { 6848 "plural": [ 6849 { 6850 "name": "test", 6851 "value": [ 6852 { 6853 "quantity": "one", 6854 "value": "%d apple" 6855 }, 6856 { 6857 "quantity": "other", 6858 "value": "%d apples" 6859 } 6860 ] 6861 } 6862 ] 6863 } 6864 ``` 6865 ```ts 6866 import { BusinessError } from '@kit.BasicServicesKit'; 6867 6868 // 根据语言单复数规则,参数num取值为1,英文环境下对应单复数类别为one 6869 // 在资源文件中用quantity字段表示单复数类别,因此会获取quantity为one的字符串 6870 this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1) 6871 .then((value: string) => { 6872 console.log(`getPluralStringValue, result: ${value}`); 6873 // 打印输出结果: getPluralStringValue, result: 1 apple 6874 }) 6875 .catch((error: BusinessError) => { 6876 console.error(`promise getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`); 6877 }); 6878 ``` 6879 6880### getPluralStringValue<sup>(deprecated)</sup> 6881 6882getPluralStringValue(resource: Resource, num: number, callback: _AsyncCallback<string>): void 6883 6884获取指定资源信息,指定资源数量的单复数字符串,使用callback异步回调。 6885 6886> **说明** 6887> 6888> 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 6889> 6890> 从API version 9开始支持,从API version 18开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18)替代。 6891 6892**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 6893 6894**系统能力:** SystemCapability.Global.ResourceManager 6895 6896**模型约束:** 此接口仅可在Stage模型下使用。 6897 6898**参数:** 6899 6900| 参数名 | 类型 | 必填 | 说明 | 6901| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 6902| resource | [Resource](#resource9) | 是 | 资源信息。 | 6903| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 6904| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 回调函数,返回resource对象对应的指定数量的单复数字符串。 | 6905 6906**错误码:** 6907 6908以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 6909 6910| 错误码ID | 错误信息 | 6911| -------- | ------------------------------------------------------------ | 6912| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 6913| 9001001 | Invalid resource ID. | 6914| 9001002 | No matching resource is found based on the resource ID. | 6915| 9001006 | The resource is referenced cyclically. | 6916 6917**示例:** 6918 ```json 6919 // 资源文件路径: src/main/resources/base/element/plural.json 6920 { 6921 "plural": [ 6922 { 6923 "name": "test", 6924 "value": [ 6925 { 6926 "quantity": "one", 6927 "value": "%d apple" 6928 }, 6929 { 6930 "quantity": "other", 6931 "value": "%d apples" 6932 } 6933 ] 6934 } 6935 ] 6936 } 6937 ``` 6938 ```ts 6939 import { resourceManager } from '@kit.LocalizationKit'; 6940 import { BusinessError } from '@kit.BasicServicesKit'; 6941 6942 let resource: resourceManager.Resource = { 6943 bundleName: "com.example.myapplication", 6944 moduleName: "entry", 6945 id: $r('app.plural.test').id 6946 }; 6947 // 根据语言单复数规则,参数num取值为1,英文环境下对应单复数类别为one 6948 // 在资源文件中用quantity字段表示单复数类别,因此会获取quantity为one的字符串 6949 this.context.resourceManager.getPluralStringValue(resource, 1, 6950 (error: BusinessError, value: string) => { 6951 if (error != null) { 6952 console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`); 6953 } else { 6954 console.log(`getPluralStringValue, result: ${value}`); 6955 // 打印输出结果: getPluralStringValue, result: 1 apple 6956 } 6957 }); 6958 ``` 6959 6960### getPluralStringValue<sup>(deprecated)</sup> 6961 6962getPluralStringValue(resource: Resource, num: number): Promise<string> 6963 6964获取指定资源信息,指定资源数量的单复数字符串,使用Promise异步回调。 6965 6966> **说明** 6967> 6968> 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 6969> 6970> 从API version 9开始支持,从API version 18开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18)替代。 6971 6972**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 6973 6974**系统能力:** SystemCapability.Global.ResourceManager 6975 6976**模型约束:** 此接口仅可在Stage模型下使用。 6977 6978**参数:** 6979 6980| 参数名 | 类型 | 必填 | 说明 | 6981| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 6982| resource | [Resource](#resource9) | 是 | 资源信息。 | 6983| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 6984 6985**返回值:** 6986 6987| 类型 | 说明 | 6988| --------------------- | -------------------------------------------------------- | 6989| Promise<string> | Promise对象,返回resource对象对应的指定数量的单复数字符串。 | 6990 6991**错误码:** 6992 6993以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 6994 6995| 错误码ID | 错误信息 | 6996| -------- | ------------------------------------------------------------ | 6997| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 6998| 9001001 | Invalid resource ID. | 6999| 9001002 | No matching resource is found based on the resource ID. | 7000| 9001006 | The resource is referenced cyclically. | 7001 7002**示例:** 7003 ```json 7004 // 资源文件路径: src/main/resources/base/element/plural.json 7005 { 7006 "plural": [ 7007 { 7008 "name": "test", 7009 "value": [ 7010 { 7011 "quantity": "one", 7012 "value": "%d apple" 7013 }, 7014 { 7015 "quantity": "other", 7016 "value": "%d apples" 7017 } 7018 ] 7019 } 7020 ] 7021 } 7022 ``` 7023 ```ts 7024 import { resourceManager } from '@kit.LocalizationKit'; 7025 import { BusinessError } from '@kit.BasicServicesKit'; 7026 7027 let resource: resourceManager.Resource = { 7028 bundleName: "com.example.myapplication", 7029 moduleName: "entry", 7030 id: $r('app.plural.test').id 7031 }; 7032 // 根据语言单复数规则,参数num取值为1,英文环境下对应单复数类别为one 7033 // 在资源文件中用quantity字段表示单复数类别,因此会获取quantity为one的字符串 7034 this.context.resourceManager.getPluralStringValue(resource, 1) 7035 .then((value: string) => { 7036 console.log(`getPluralStringValue, result: ${value}`); 7037 // 打印输出结果: getPluralStringValue, result: 1 apple 7038 }) 7039 .catch((error: BusinessError) => { 7040 console.error(`promise getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`); 7041 }); 7042 ``` 7043 7044### getPluralStringByName<sup>(deprecated)</sup> 7045 7046getPluralStringByName(resName: string, num: number, callback: _AsyncCallback<string>): void 7047 7048获取指定资源名称,指定资源数量的单复数字符串,使用callback异步回调。 7049 7050> **说明** 7051> 7052> 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 7053> 7054> 从API version 9开始支持,从API version 18开始废弃,建议使用[getIntPluralStringByNameSync](#getintpluralstringbynamesync18)替代。 7055 7056**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 7057 7058**系统能力:** SystemCapability.Global.ResourceManager 7059 7060**参数:** 7061 7062| 参数名 | 类型 | 必填 | 说明 | 7063| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 7064| resName | string | 是 | 资源名称。 | 7065| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 7066| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 回调函数,返回资源名称对应的指定数量的单复数字符串。 | 7067 7068**错误码:** 7069 7070以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 7071 7072| 错误码ID | 错误信息 | 7073| -------- | ------------------------------------------------------------ | 7074| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 7075| 9001003 | Invalid resource name. | 7076| 9001004 | No matching resource is found based on the resource name. | 7077| 9001006 | The resource is referenced cyclically. | 7078 7079**示例:** 7080 ```json 7081 // 资源文件路径: src/main/resources/base/element/plural.json 7082 { 7083 "plural": [ 7084 { 7085 "name": "test", 7086 "value": [ 7087 { 7088 "quantity": "one", 7089 "value": "%d apple" 7090 }, 7091 { 7092 "quantity": "other", 7093 "value": "%d apples" 7094 } 7095 ] 7096 } 7097 ] 7098 } 7099 ``` 7100 ```ts 7101 import { BusinessError } from '@kit.BasicServicesKit'; 7102 7103 // 根据语言单复数规则,参数num取值为1,英文环境下对应单复数类别为one 7104 // 在资源文件中用quantity字段表示单复数类别,因此会获取quantity为one的字符串 7105 this.context.resourceManager.getPluralStringByName("test", 1, (error: BusinessError, value: string) => { 7106 if (error != null) { 7107 console.error(`callback getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`); 7108 } else { 7109 console.log(`getPluralStringByName, result: ${value}`); 7110 // 打印输出结果: getPluralStringByName, result: 1 apple 7111 } 7112 }); 7113 ``` 7114 7115### getPluralStringByName<sup>(deprecated)</sup> 7116 7117getPluralStringByName(resName: string, num: number): Promise<string> 7118 7119获取指定资源名称,指定资源数量的单复数字符串,使用Promise异步回调。 7120 7121> **说明** 7122> 7123> 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 7124> 7125> 从API version 9开始支持,从API version 18开始废弃,建议使用[getIntPluralStringByNameSync](#getintpluralstringbynamesync18)替代。 7126 7127**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 7128 7129**系统能力:** SystemCapability.Global.ResourceManager 7130 7131**参数:** 7132 7133| 参数名 | 类型 | 必填 | 说明 | 7134| ------- | ------ | ---- | ------------------------------------------------------------ | 7135| resName | string | 是 | 资源名称。 | 7136| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 7137 7138**返回值:** 7139 7140| 类型 | 说明 | 7141| --------------------- | ------------------------------------------------ | 7142| Promise<string> | 根据传入的数量值,获取资源名称对应的字符串资源。 | 7143 7144**错误码:** 7145 7146以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 7147 7148| 错误码ID | 错误信息 | 7149| -------- | ------------------------------------------------------------ | 7150| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 7151| 9001003 | Invalid resource name. | 7152| 9001004 | No matching resource is found based on the resource name. | 7153| 9001006 | The resource is referenced cyclically. | 7154 7155**示例:** 7156 ```json 7157 // 资源文件路径: src/main/resources/base/element/plural.json 7158 { 7159 "plural": [ 7160 { 7161 "name": "test", 7162 "value": [ 7163 { 7164 "quantity": "one", 7165 "value": "%d apple" 7166 }, 7167 { 7168 "quantity": "other", 7169 "value": "%d apples" 7170 } 7171 ] 7172 } 7173 ] 7174 } 7175 ``` 7176 ```ts 7177 import { BusinessError } from '@kit.BasicServicesKit'; 7178 7179 // 根据语言单复数规则,参数num取值为1,英文环境下对应单复数类别为one 7180 // 在资源文件中用quantity字段表示单复数类别,因此会获取quantity为one的字符串 7181 this.context.resourceManager.getPluralStringByName("test", 1) 7182 .then((value: string) => { 7183 console.log(`getPluralStringByName, result: ${value}`); 7184 // 打印输出结果: getPluralStringByName, result: 1 apple 7185 }) 7186 .catch((error: BusinessError) => { 7187 console.error(`promise getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`); 7188 }); 7189 ``` 7190 7191### getPluralString<sup>(deprecated)</sup> 7192 7193getPluralString(resId: number, num: number): Promise<string> 7194 7195获取指定资源ID,指定资源数量的单复数字符串,使用Promise异步回调。 7196 7197> **说明** 7198> 7199> 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 7200> 7201> 从API version 6开始支持,从API version 9开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18)替代。 7202 7203**系统能力:** SystemCapability.Global.ResourceManager 7204 7205**参数:** 7206 7207| 参数名 | 类型 | 必填 | 说明 | 7208| ----- | ------ | ---- | ----- | 7209| resId | number | 是 | 资源ID值。 | 7210| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 7211 7212**返回值:** 7213 7214| 类型 | 说明 | 7215| --------------------- | ------------------------- | 7216| Promise<string> | Promise对象,返回资源ID值对应的指定数量的单复数字符串。 | 7217 7218**示例:** 7219 7220 ```ts 7221 import { BusinessError } from '@kit.BasicServicesKit'; 7222 7223 resourceManager.getResourceManager((error, mgr) => { 7224 mgr.getPluralString($r("app.plural.test").id, 1).then((value: string) => { 7225 let str = value; 7226 }).catch((error: BusinessError) => { 7227 console.error("getPluralString promise error is " + error); 7228 }); 7229 }); 7230 ``` 7231 7232 7233### getPluralString<sup>(deprecated)</sup> 7234 7235getPluralString(resId: number, num: number, callback: AsyncCallback<string>): void 7236 7237获取指定资源ID,指定资源数量的单复数字符串,使用callback异步回调。 7238 7239> **说明** 7240> 7241> 中文环境下,字符串不区分单复数;其他语言环境下,字符串区分单复数,具体规则参考[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 7242> 7243> 从API version 6开始支持,从API version 9开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18)替代。 7244 7245**系统能力:** SystemCapability.Global.ResourceManager 7246 7247**参数:** 7248 7249| 参数名 | 类型 | 必填 | 说明 | 7250| -------- | --------------------------- | ---- | ------------------------------- | 7251| resId | number | 是 | 资源ID值。 | 7252| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 7253| callback | [AsyncCallback](#asynccallbackdeprecated)<string> | 是 | 回调函数,返回资源ID值对应的指定数量的单复数字符串。 | 7254 7255**示例:** 7256 7257 ```ts 7258 import { resourceManager } from '@kit.LocalizationKit'; 7259 7260 resourceManager.getResourceManager((error, mgr) => { 7261 mgr.getPluralString($r("app.plural.test").id, 1, (error: Error, value: string) => { 7262 if (error != null) { 7263 console.error("error is " + error); 7264 } else { 7265 let str = value; 7266 } 7267 }); 7268 }); 7269 ``` 7270### getBoolean<sup>(deprecated)</sup> 7271 7272getBoolean(resource: Resource): boolean 7273 7274获取指定resource对象对应的布尔值,使用同步方式返回。 7275 7276> **说明** 7277> 7278> 从API version 9开始支持,从API version 20开始废弃,建议使用[getBooleanByName](#getbooleanbyname9)或[getBoolean](#getboolean9)替代。 7279 7280**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 7281 7282**系统能力:** SystemCapability.Global.ResourceManager 7283 7284**模型约束:** 此接口仅可在Stage模型下使用。 7285 7286**参数:** 7287 7288| 参数名 | 类型 | 必填 | 说明 | 7289| -------- | ---------------------- | ---- | ---- | 7290| resource | [Resource](#resource9) | 是 | 资源信息。 | 7291 7292**返回值:** 7293 7294| 类型 | 说明 | 7295| ------- | ----------------- | 7296| boolean | resource对象对应的布尔值。 | 7297 7298**错误码:** 7299 7300以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 7301 7302| 错误码ID | 错误信息 | 7303| -------- | ---------------------------------------- | 7304| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 7305| 9001001 | Invalid resource ID. | 7306| 9001002 | No matching resource is found based on the resource ID. | 7307| 9001006 | The resource is referenced cyclically. | 7308 7309**示例:** 7310 ```json 7311 // 资源文件路径: src/main/resources/base/element/boolean.json 7312 { 7313 "boolean": [ 7314 { 7315 "name": "boolean_test", 7316 "value": true 7317 } 7318 ] 7319 } 7320 ``` 7321 ```ts 7322 import { resourceManager } from '@kit.LocalizationKit'; 7323 import { BusinessError } from '@kit.BasicServicesKit'; 7324 7325 let resource: resourceManager.Resource = { 7326 bundleName: "com.example.myapplication", 7327 moduleName: "entry", 7328 id: $r('app.boolean.boolean_test').id 7329 }; 7330 try { 7331 let boolTest = this.context.resourceManager.getBoolean(resource); 7332 console.log(`getBoolean, result: ${boolTest}`); 7333 // 打印输出结果: getBoolean, result: true 7334 } catch (error) { 7335 let code = (error as BusinessError).code; 7336 let message = (error as BusinessError).message; 7337 console.error(`getBoolean failed, error code: ${code}, message: ${message}.`); 7338 } 7339 ``` 7340### getNumber<sup>(deprecated)</sup> 7341 7342getNumber(resource: Resource): number 7343 7344获取指定resource对象对应的integer数值或者float数值,使用同步方式返回。 7345 7346> **说明** 7347> 7348> 从API version 9开始支持,从API version 20开始废弃,建议使用[getNumberByName](#getnumberbyname9)或[getNumber](#getnumber9)替代。 7349 7350**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 7351 7352**系统能力:** SystemCapability.Global.ResourceManager 7353 7354**模型约束:** 此接口仅可在Stage模型下使用。 7355 7356**参数:** 7357 7358| 参数名 | 类型 | 必填 | 说明 | 7359| -------- | ---------------------- | ---- | ---- | 7360| resource | [Resource](#resource9) | 是 | 资源信息。 | 7361 7362**返回值:** 7363 7364| 类型 | 说明 | 7365| ------ | --------------- | 7366| number | resource对象对应的数值。<br>integer对应的是原数值,float不带单位时对应的是原数值,带"vp","fp"单位时对应的是px值。 | 7367 7368**错误码:** 7369 7370以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 7371 7372| 错误码ID | 错误信息 | 7373| -------- | ---------------------------------------- | 7374| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 7375| 9001001 | Invalid resource ID. | 7376| 9001002 | No matching resource is found based on the resource ID. | 7377| 9001006 | The resource is referenced cyclically. | 7378 7379**示例:** 7380 ```json 7381 // 资源文件路径: src/main/resources/base/element/integer.json 7382 { 7383 "integer": [ 7384 { 7385 "name": "integer_test", 7386 "value": 100 7387 } 7388 ] 7389 } 7390 ``` 7391 7392 ```ts 7393 import { resourceManager } from '@kit.LocalizationKit'; 7394 import { BusinessError } from '@kit.BasicServicesKit'; 7395 7396 let resource: resourceManager.Resource = { 7397 bundleName: "com.example.myapplication", 7398 moduleName: "entry", 7399 id: $r('app.integer.integer_test').id 7400 }; 7401 7402 try { 7403 let intValue = this.context.resourceManager.getNumber(resource); 7404 console.log(`getNumber, int value: ${intValue}`); 7405 // 打印输出结果: getNumber, int value: 100 7406 } catch (error) { 7407 let code = (error as BusinessError).code; 7408 let message = (error as BusinessError).message; 7409 console.error(`getNumber failed, error code: ${code}, message: ${message}.`); 7410 } 7411 ``` 7412### getColorSync<sup>(deprecated)</sup> 7413 7414getColorSync(resource: Resource): number 7415 7416获取指定resource对象对应的颜色值,使用同步方式返回。 7417 7418> **说明** 7419> 7420> 从API version 10开始支持,从API version 20开始废弃,建议使用[getColorByNameSync](#getcolorbynamesync10)或[getColorSync](#getcolorsync10)替代。 7421 7422**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 7423 7424**系统能力:** SystemCapability.Global.ResourceManager 7425 7426**模型约束:** 此接口仅可在Stage模型下使用。 7427 7428**参数:** 7429 7430| 参数名 | 类型 | 必填 | 说明 | 7431| -------- | ---------------------- | ---- | ---- | 7432| resource | [Resource](#resource9) | 是 | 资源信息。 | 7433 7434**返回值:** 7435 7436| 类型 | 说明 | 7437| ------ | ---------------- | 7438| number | resource对象对应的颜色值(十进制)。 | 7439 7440**错误码:** 7441 7442以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 7443 7444| 错误码ID | 错误信息 | 7445| -------- | ---------------------------------------- | 7446| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 7447| 9001001 | Invalid resource ID. | 7448| 9001002 | No matching resource is found based on the resource ID. | 7449| 9001006 | The resource is referenced cyclically. | 7450 7451**示例:** 7452 ```json 7453 // 资源文件路径: src/main/resources/base/element/color.json 7454 { 7455 "color": [ 7456 { 7457 "name": "test", 7458 "value": "#FFFFFF" 7459 } 7460 ] 7461 } 7462 ``` 7463 ```ts 7464 import { resourceManager } from '@kit.LocalizationKit'; 7465 import { BusinessError } from '@kit.BasicServicesKit'; 7466 7467 let resource: resourceManager.Resource = { 7468 bundleName: "com.example.myapplication", 7469 moduleName: "entry", 7470 id: $r('app.color.test').id 7471 }; 7472 try { 7473 let colorValue = this.context.resourceManager.getColorSync(resource); 7474 console.log(`getColorSync, result: ${colorValue}`); 7475 // 打印输出结果: getColorSync, result: 4294967295 7476 } catch (error) { 7477 let code = (error as BusinessError).code; 7478 let message = (error as BusinessError).message; 7479 console.error(`getColorSync failed, error code: ${code}, message: ${message}.`); 7480 } 7481 ``` 7482### getColor<sup>(deprecated)</sup> 7483 7484getColor(resource: Resource, callback: _AsyncCallback<number>): void 7485 7486获取指定resource对象对应的颜色值,使用callback异步回调。 7487 7488> **说明** 7489> 7490> 从API version 10开始支持,从API version 20开始废弃,建议使用[getColorByName](#getcolorbyname10)或[getColor](#getcolor10)替代。 7491 7492**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 7493 7494**系统能力:** SystemCapability.Global.ResourceManager 7495 7496**模型约束:** 此接口仅可在Stage模型下使用。 7497 7498**参数:** 7499 7500| 参数名 | 类型 | 必填 | 说明 | 7501| -------- | --------------------------- | ---- | --------------- | 7502| resource | [Resource](#resource9) | 是 | 资源信息。 | 7503| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<number> | 是 | 回调函数,返回resource对象对应的颜色值(十进制)。 | 7504 7505**错误码:** 7506 7507以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 7508 7509| 错误码ID | 错误信息 | 7510| -------- | ---------------------------------------- | 7511| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 7512| 9001001 | Invalid resource ID. | 7513| 9001002 | No matching resource is found based on the resource ID. | 7514| 9001006 | The resource is referenced cyclically. | 7515 7516**示例:** 7517 ```json 7518 // 资源文件路径: src/main/resources/base/element/color.json 7519 { 7520 "color": [ 7521 { 7522 "name": "test", 7523 "value": "#FFFFFF" 7524 } 7525 ] 7526 } 7527 ``` 7528 ```ts 7529 import { resourceManager } from '@kit.LocalizationKit'; 7530 import { BusinessError } from '@kit.BasicServicesKit'; 7531 7532 let resource: resourceManager.Resource = { 7533 bundleName: "com.example.myapplication", 7534 moduleName: "entry", 7535 id: $r('app.color.test').id 7536 }; 7537 this.context.resourceManager.getColor(resource, (error: BusinessError, value: number) => { 7538 if (error != null) { 7539 console.error(`callback getColor failed, error code: ${error.code}, message: ${error.message}.`); 7540 } else { 7541 console.log(`getColor, result: ${value}`); 7542 // 打印输出结果: getColor, result: 4294967295 7543 } 7544 }); 7545 ``` 7546 7547### getColor<sup>(deprecated)</sup> 7548 7549getColor(resource: Resource): Promise<number> 7550 7551获取指定resource对象对应的颜色值,使用Promise异步回调。 7552 7553> **说明** 7554> 7555> 从API version 10开始支持,从API version 20开始废弃,建议使用[getColorByName](#getcolorbyname10-1)或[getColor](#getcolor10-1)替代。 7556 7557**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 7558 7559**系统能力:** SystemCapability.Global.ResourceManager 7560 7561**模型约束:** 此接口仅可在Stage模型下使用。 7562 7563**参数:** 7564 7565| 参数名 | 类型 | 必填 | 说明 | 7566| -------- | ---------------------- | ---- | ---- | 7567| resource | [Resource](#resource9) | 是 | 资源信息。 | 7568 7569**返回值:** 7570 7571| 类型 | 说明 | 7572| --------------------- | ---------------- | 7573| Promise<number> | Promise对象,返回resource对象对应的颜色值(十进制)。 | 7574 7575**错误码:** 7576 7577以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 7578 7579| 错误码ID | 错误信息 | 7580| -------- | ---------------------------------------- | 7581| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 7582| 9001001 | Invalid resource ID. | 7583| 9001002 | No matching resource is found based on the resource ID. | 7584| 9001006 | The resource is referenced cyclically. | 7585 7586**示例:** 7587 ```json 7588 // 资源文件路径: src/main/resources/base/element/color.json 7589 { 7590 "color": [ 7591 { 7592 "name": "test", 7593 "value": "#FFFFFF" 7594 } 7595 ] 7596 } 7597 ``` 7598 ```ts 7599 import { resourceManager } from '@kit.LocalizationKit'; 7600 import { BusinessError } from '@kit.BasicServicesKit'; 7601 7602 let resource: resourceManager.Resource = { 7603 bundleName: "com.example.myapplication", 7604 moduleName: "entry", 7605 id: $r('app.color.test').id 7606 }; 7607 this.context.resourceManager.getColor(resource) 7608 .then((value: number) => { 7609 console.log(`getColor, result: ${value}`); 7610 // 打印输出结果: getColor, result: 4294967295 7611 }) 7612 .catch((error: BusinessError) => { 7613 console.error(`promise getColor failed, error code: ${error.code}, message: ${error.message}.`); 7614 }); 7615 ``` 7616### getSymbol<sup>(deprecated)</sup> 7617getSymbol(resource: Resource): number 7618 7619获取指定resource对象对应的[Symbol字符](https://developer.huawei.com/consumer/cn/design/harmonyos-symbol)Unicode码,使用同步方式返回。 7620 7621> **说明** 7622> 7623> 从API version 11开始支持,从API version 20开始废弃,建议使用[getSymbolByName](#getsymbolbyname11)或[getSymbol](#getsymbol11)替代。 7624 7625**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 7626 7627**系统能力:** SystemCapability.Global.ResourceManager 7628 7629**模型约束:** 此接口仅可在Stage模型下使用。 7630 7631**参数:** 7632 7633| 参数名 | 类型 | 必填 | 说明 | 7634| -------- | ---------------------- | ---- | ---- | 7635| resource | [Resource](#resource9) | 是 | 资源信息。 | 7636 7637**返回值:** 7638 7639| 类型 | 说明 | 7640| ------ | ----------- | 7641| number | resource对象对应的Symbol字符Unicode码(十进制)。 | 7642 7643**错误码:** 7644 7645以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 7646 7647| 错误码ID | 错误信息 | 7648| -------- | ---------------------------------------- | 7649| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 7650| 9001001 | Invalid resource ID. | 7651| 9001002 | No matching resource is found based on the resource ID. | 7652| 9001006 | The resource is referenced cyclically. | 7653 7654**示例:** 7655 ```ts 7656 import { resourceManager } from '@kit.LocalizationKit'; 7657 import { BusinessError } from '@kit.BasicServicesKit'; 7658 7659 let resource: resourceManager.Resource = { 7660 bundleName: "com.example.myapplication", 7661 moduleName: "entry", 7662 id: $r('sys.symbol.message').id 7663 }; 7664 try { 7665 let symbolValue = this.context.resourceManager.getSymbol(resource); 7666 console.log(`getSymbol, result: ${symbolValue}`); 7667 // 打印输出结果: getSymbol, result: 983183 7668 } catch (error) { 7669 let code = (error as BusinessError).code; 7670 let message = (error as BusinessError).message; 7671 console.error(`getSymbol failed, error code: ${code}, message: ${message}.`); 7672 } 7673 ``` 7674 7675### getRawFile<sup>(deprecated)</sup> 7676 7677getRawFile(path: string, callback: AsyncCallback<Uint8Array>): void 7678 7679获取resources/rawfile目录下对应的rawfile文件内容,使用callback异步回调。 7680 7681> **说明** 7682> 7683> 从API version 8开始支持,从API version 9开始废弃,建议使用[getRawFileContent](#getrawfilecontent9)替代。 7684 7685**系统能力:** SystemCapability.Global.ResourceManager 7686 7687**参数:** 7688 7689| 参数名 | 类型 | 必填 | 说明 | 7690| -------- | ------------------------------- | ---- | ----------------------- | 7691| path | string | 是 | rawfile文件路径。 | 7692| callback | [AsyncCallback](#asynccallbackdeprecated)<Uint8Array> | 是 | 回调函数,返回rawfile文件内容。 | 7693 7694**示例:** 7695 ```ts 7696 import { resourceManager } from '@kit.LocalizationKit'; 7697 7698 resourceManager.getResourceManager((error, mgr) => { 7699 mgr.getRawFile("test.txt", (error: Error, value: Uint8Array) => { 7700 if (error != null) { 7701 console.error("error is " + error); 7702 } else { 7703 let rawFile = value; 7704 } 7705 }); 7706 }); 7707 ``` 7708 7709 7710### getRawFile<sup>(deprecated)</sup> 7711 7712getRawFile(path: string): Promise<Uint8Array> 7713 7714获取resources/rawfile目录下对应的rawfile文件内容,使用Promise异步回调。 7715 7716> **说明** 7717> 7718> 从API version 8开始支持,从API version 9开始废弃,建议使用[getRawFileContent](#getrawfilecontent9-1)替代。 7719 7720**系统能力:** SystemCapability.Global.ResourceManager 7721 7722**参数:** 7723 7724| 参数名 | 类型 | 必填 | 说明 | 7725| ---- | ------ | ---- | ----------- | 7726| path | string | 是 | rawfile文件路径。 | 7727 7728**返回值:** 7729 7730| 类型 | 说明 | 7731| ------------------------- | ----------- | 7732| Promise<Uint8Array> | Promise对象,返回rawfile文件内容。 | 7733 7734**示例:** 7735 ```ts 7736 import { BusinessError } from '@kit.BasicServicesKit'; 7737 7738 resourceManager.getResourceManager((error, mgr) => { 7739 mgr.getRawFile("test.txt").then((value: Uint8Array) => { 7740 let rawFile = value; 7741 }).catch((error: BusinessError) => { 7742 console.error("getRawFile promise error is " + error); 7743 }); 7744 }); 7745 ``` 7746 7747 7748### getRawFileDescriptor<sup>(deprecated)</sup> 7749 7750getRawFileDescriptor(path: string, callback: AsyncCallback<RawFileDescriptor>): void 7751 7752获取resources/rawfile目录下对应rawfile文件的文件描述符(fd),使用callback异步回调。 7753 7754> **说明** 7755> 7756> 从API version 8开始支持,从API version 9开始废弃,建议使用[getRawFd](#getrawfd9)替代。 7757 7758**系统能力:** SystemCapability.Global.ResourceManager 7759 7760**参数:** 7761 7762| 参数名 | 类型 | 必填 | 说明 | 7763| -------- | ---------------------------------------- | ---- | -------------------------------- | 7764| path | string | 是 | rawfile文件路径。 | 7765| callback | [AsyncCallback](#asynccallbackdeprecated)<[RawFileDescriptor](#rawfiledescriptor9)> | 是 | 回调函数,返回rawfile文件的文件描述符(fd)。 | 7766 7767**示例:** 7768 ```ts 7769 import { resourceManager } from '@kit.LocalizationKit'; 7770 7771 resourceManager.getResourceManager((error, mgr) => { 7772 mgr.getRawFileDescriptor("test.txt", (error: Error, value: resourceManager.RawFileDescriptor) => { 7773 if (error != null) { 7774 console.error("error is " + error); 7775 } else { 7776 let fd = value.fd; 7777 let offset = value.offset; 7778 let length = value.length; 7779 } 7780 }); 7781 }); 7782 ``` 7783 7784### getRawFileDescriptor<sup>(deprecated)</sup> 7785 7786getRawFileDescriptor(path: string): Promise<RawFileDescriptor> 7787 7788获取resources/rawfile目录下对应rawfile文件的文件描述符(fd),使用Promise异步回调。 7789 7790> **说明** 7791> 7792> 从API version 8开始支持,从API version 9开始废弃,建议使用[getRawFd](#getrawfd9-1)替代。 7793 7794**系统能力:** SystemCapability.Global.ResourceManager 7795 7796**参数:** 7797 7798| 参数名 | 类型 | 必填 | 说明 | 7799| ---- | ------ | ---- | ----------- | 7800| path | string | 是 | rawfile文件路径。 | 7801 7802**返回值:** 7803 7804| 类型 | 说明 | 7805| ---------------------------------------- | ------------------- | 7806| Promise<[RawFileDescriptor](#rawfiledescriptor9)> | Promise对象,返回rawfile文件的文件描述符(fd)。 | 7807 7808**示例:** 7809 ```ts 7810 import { BusinessError } from '@kit.BasicServicesKit'; 7811 7812 resourceManager.getResourceManager((error, mgr) => { 7813 mgr.getRawFileDescriptor("test.txt").then((value: resourceManager.RawFileDescriptor) => { 7814 let fd = value.fd; 7815 let offset = value.offset; 7816 let length = value.length; 7817 }).catch((error: BusinessError) => { 7818 console.error("getRawFileDescriptor promise error is " + error); 7819 }); 7820 }); 7821 ``` 7822 7823### closeRawFileDescriptor<sup>(deprecated)</sup> 7824 7825closeRawFileDescriptor(path: string, callback: AsyncCallback<void>): void 7826 7827关闭resources/rawfile目录下rawfile文件的文件描述符(fd),使用callback异步回调。 7828 7829> **说明** 7830> 7831> 从API version 8开始支持,从API version 9开始废弃,建议使用[closeRawFd](#closerawfd9)替代。 7832 7833**系统能力:** SystemCapability.Global.ResourceManager 7834 7835**参数:** 7836 7837 7838 7839| 参数名 | 类型 | 必填 | 说明 | 7840| -------- | ------------------------- | ---- | ----------- | 7841| path | string | 是 | rawfile文件路径。 | 7842| callback | [AsyncCallback](#asynccallbackdeprecated)<void> | 是 | 回调函数。当关闭rawfile文件的文件描述符(fd)成功,err为undefined,否则为错误对象。| 7843 7844**示例:** 7845 ```ts 7846 import { resourceManager } from '@kit.LocalizationKit'; 7847 7848 resourceManager.getResourceManager((error, mgr) => { 7849 mgr.closeRawFileDescriptor("test.txt", (error: Error) => { 7850 if (error != null) { 7851 console.error("error is " + error); 7852 } 7853 }); 7854 }); 7855 ``` 7856 7857### closeRawFileDescriptor<sup>(deprecated)</sup> 7858 7859closeRawFileDescriptor(path: string): Promise<void> 7860 7861关闭resources/rawfile目录下rawfile文件的文件描述符(fd),使用Promise异步回调。 7862 7863> **说明** 7864> 7865> 从API version 8开始支持,从API version 9开始废弃,建议使用[closeRawFd](#closerawfd9-1)替代。 7866 7867**系统能力:** SystemCapability.Global.ResourceManager 7868 7869**参数:** 7870 7871| 参数名 | 类型 | 必填 | 说明 | 7872| ---- | ------ | ---- | ----------- | 7873| path | string | 是 | rawfile文件路径。 | 7874 7875**返回值:** 7876 7877| 类型 | 说明 | 7878| ------------------- | ---- | 7879| Promise<void> | Promise对象。无返回结果的Promise对象。 | 7880 7881**示例:** 7882 ```ts 7883 import { resourceManager } from '@kit.LocalizationKit'; 7884 7885 resourceManager.getResourceManager((error, mgr) => { 7886 mgr.closeRawFileDescriptor("test.txt"); 7887 }); 7888 ``` 7889 7890## resourceManager.getSystemResourceManager<sup>(deprecated)</sup> 7891 7892getSystemResourceManager(): ResourceManager 7893 7894获取系统资源管理ResourceManager对象。 7895 7896> **说明** 7897> 7898> 当前接口获取到的系统资源管理ResourceManager对象中的Configuration为默认值。默认值如下: 7899> {"locale": "", "direction": -1, "deviceType": -1, "screenDensity": 0, "colorMode": 1, "mcc": 0, "mnc": 0}。 7900> 7901> 从API version 10开始支持,从API version 20开始废弃,建议使用[resourceManager.getSysResourceManager](#resourcemanagergetsysresourcemanager20)替代。 7902 7903**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 7904 7905**系统能力:** SystemCapability.Global.ResourceManager 7906 7907**返回值:** 7908 7909| 类型 | 说明 | 7910| ---------------------------------------- | ------------------ | 7911| [ResourceManager](#resourcemanager) | 系统资源管理对象。 | 7912 7913**错误码:** 7914 7915以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 7916 7917| 错误码ID | 错误信息 | 7918| -------- | ---------------------------------------- | 7919| 9001009 | Failed to access the system resource. which is not mapped to application sandbox, This error code will be thrown. | 7920 7921**示例:** 7922 ```js 7923import { resourceManager } from '@kit.LocalizationKit'; 7924import { BusinessError } from '@kit.BasicServicesKit'; 7925 7926 try { 7927 let systemResourceManager = resourceManager.getSystemResourceManager(); 7928 systemResourceManager.getStringValue($r('sys.string.ohos_lab_vibrate').id).then((value: string) => { 7929 let str = value; 7930 }).catch((error: BusinessError) => { 7931 console.error("systemResourceManager getStringValue promise error is " + error); 7932 }); 7933 } catch (error) { 7934 let code = (error as BusinessError).code; 7935 let message = (error as BusinessError).message; 7936 console.error(`getSystemResourceManager failed, error code: ${code}, message: ${message}.`); 7937 } 7938 ``` 7939 7940## AsyncCallback<sup>(deprecated)</sup> 7941 7942> **说明** 7943> 7944> 从API version 6开始支持,从API version 9开始废弃,建议使用[AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)替代。 7945 7946### (err: Error, data: T)<sup>(deprecated)</sup> 7947 7948(err: Error, data: T): void; 7949 7950异步回调函数,携带错误参数和异步返回值。 7951 7952> **说明** 7953> 7954> 从API version 6开始支持,从API version 9开始废弃,建议使用[AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)替代。 7955 7956**系统能力:** SystemCapability.Global.ResourceManager 7957 7958**参数:** 7959 7960| 参数名 | 类型 | 必填 | 说明 | 7961| ---- | ------------------------------------------------------------ | ---- | ---------------------------- | 7962| err | Error | 是 | 接口调用失败的错误信息。 | 7963| data | T | 是 | 接口调用时的回调信息。| 7964 7965## 附录 7966 7967- 示例代码中用到的'app.string.test'文件内容如下: 7968 7969 ```json 7970 // 资源文件路径: src/main/resources/base/element/string.json 7971 { 7972 "string": [ 7973 { 7974 "name": "test", 7975 "value": "I'm a test string resource." 7976 } 7977 ] 7978 } 7979 ``` 7980 7981 ```json 7982 // 资源文件路径: src/main/resources/base/element/string.json 7983 { 7984 "string": [ 7985 { 7986 "name": "test", 7987 "value": "I'm a %1$s, format int: %2$d, format float: %3$f." 7988 } 7989 ] 7990 } 7991 ``` 7992 7993- 示例代码中用到的'app.strarray.test'文件内容如下: 7994 7995 ```json 7996 // 资源文件路径: src/main/resources/base/element/strarray.json 7997 { 7998 "strarray": [ 7999 { 8000 "name": "test", 8001 "value": [ 8002 { 8003 "value": "I'm one of the array's values." 8004 } 8005 ] 8006 } 8007 ] 8008 } 8009 ``` 8010 8011- 示例代码中用到的'app.plural.test'文件内容如下: 8012 ```json 8013 // 资源文件路径: src/main/resources/base/element/plural.json 8014 { 8015 "plural": [ 8016 { 8017 "name": "test", 8018 "value": [ 8019 { 8020 "quantity": "one", 8021 "value": "%d apple" 8022 }, 8023 { 8024 "quantity": "other", 8025 "value": "%d apples" 8026 } 8027 ] 8028 } 8029 ] 8030 } 8031 ``` 8032 8033- 示例代码中用到的'app.plural.format_test'文件内容如下: 8034 8035 ```json 8036 // 资源文件路径: src/main/resources/base/element/plural.json 8037 { 8038 "plural": [ 8039 { 8040 "name": "format_test", 8041 "value": [ 8042 { 8043 "quantity": "one", 8044 "value": "There is %d apple in the %s, the total amount is %f kg." 8045 }, 8046 { 8047 "quantity": "other", 8048 "value": "There are %d apples in the %s, the total amount is %f kg." 8049 } 8050 ] 8051 } 8052 ] 8053 } 8054 ``` 8055 8056- 示例代码中用到的'app.boolean.boolean_test'文件内容如下: 8057 ```json 8058 // 资源文件路径: src/main/resources/base/element/boolean.json 8059 { 8060 "boolean": [ 8061 { 8062 "name": "boolean_test", 8063 "value": true 8064 } 8065 ] 8066 } 8067 ``` 8068 8069- 示例代码中用到的"integer_test"和"float_test"文件内容如下: 8070 ```json 8071 // 资源文件路径: src/main/resources/base/element/integer.json 8072 { 8073 "integer": [ 8074 { 8075 "name": "integer_test", 8076 "value": 100 8077 } 8078 ] 8079 } 8080 ``` 8081 8082 ```json 8083 // 资源文件路径: src/main/resources/base/element/float.json 8084 { 8085 "float": [ 8086 { 8087 "name": "float_test", 8088 "value": "30.6vp" 8089 } 8090 ] 8091 } 8092 ``` 8093- 示例代码中用到的'app.color.test'文件内容如下: 8094 ```json 8095 // 资源文件路径: src/main/resources/base/element/color.json 8096 { 8097 "color": [ 8098 { 8099 "name": "test", 8100 "value": "#FFFFFF" 8101 } 8102 ] 8103 } 8104 ```