1# @ohos.resourceManager (资源管理) 2 3资源管理模块,根据当前configuration:语言、区域、横竖屏、Mcc(移动国家码)和Mnc(移动网络码)、Device capability(设备类型)、Density(分辨率)提供获取应用资源信息读取接口。 4 5> **说明:** 6> 7> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10 11```js 12import { resourceManager } from '@kit.LocalizationKit' 13``` 14 15## 使用说明 16 17从API Version9开始,Stage模型通过context获取resourceManager对象的方式后,可直接调用其内部获取资源的接口,无需再导入包。 18FA模型仍需要先导入包,再调用[getResourceManager](#resourcemanagergetresourcemanager)接口获取资源对象。 19Stage模型下Context的引用方法请参考[Stage模型的Context详细介绍](../../application-models/application-context-stage.md)。 20 21```ts 22import { UIAbility } from '@kit.AbilityKit'; 23import { window } from '@kit.ArkUI'; 24 25export default class EntryAbility extends UIAbility { 26 onWindowStageCreate(windowStage: window.WindowStage) { 27 let context = this.context; 28 let resourceManager = context.resourceManager; 29 } 30} 31``` 32 33## resourceManager.getResourceManager 34 35getResourceManager(callback: AsyncCallback<ResourceManager>): void 36 37获取当前应用的资源管理对象,使用callback异步回调。 38 39**系统能力**:SystemCapability.Global.ResourceManager 40 41**模型约束**:此接口仅可在FA模型下使用。 42 43**参数:** 44 45| 参数名 | 类型 | 必填 | 说明 | 46| -------- | ---------------------------------------- | ---- | ----------------------------- | 47| callback | [AsyncCallback](#asynccallbackdeprecated)<[ResourceManager](#resourcemanager)> | 是 |返回资源管理ResourceManager对象。 | 48 49**示例:** 50 <!--code_no_check_fa--> 51 ```js 52 resourceManager.getResourceManager((error, mgr) => { 53 if (error != null) { 54 console.error("error is " + error); 55 return; 56 } 57 mgr.getStringValue($r('app.string.test').id, (error: BusinessError, value: string) => { 58 if (error != null) { 59 console.error("error is " + error); 60 } else { 61 let str = value; 62 } 63 }); 64 }); 65 ``` 66 67## resourceManager.getResourceManager 68 69getResourceManager(bundleName: string, callback: AsyncCallback<ResourceManager>): void 70 71获取指定应用的资源管理对象,使用callback异步回调。 72 73**系统能力**:SystemCapability.Global.ResourceManager 74 75**模型约束**:此接口仅可在FA模型下使用。 76 77**参数:** 78 79| 参数名 | 类型 | 必填 | 说明 | 80| ---------- | ---------------------------------------- | ---- | ----------------------------- | 81| bundleName | string | 是 | 应用的Bundle名称。 | 82| callback | [AsyncCallback](#asynccallbackdeprecated)<[ResourceManager](#resourcemanager)> | 是 | 返回资源管理ResourceManager对象。 | 83 84**示例:** 85 <!--code_no_check_fa--> 86 ```js 87 resourceManager.getResourceManager("com.example.myapplication", (error, mgr) => { 88 }); 89 ``` 90 91## resourceManager.getResourceManager 92 93getResourceManager(): Promise<ResourceManager> 94 95获取当前应用的资源管理对象,使用Promise异步回调。 96 97**系统能力**:SystemCapability.Global.ResourceManager 98 99**模型约束**:此接口仅可在FA模型下使用。 100 101**返回值:** 102 103| 类型 | 说明 | 104| ---------------------------------------- | ----------------- | 105| Promise<[ResourceManager](#resourcemanager)> | 返回资源管理ResourceManager对象。 | 106 107**示例:** 108 <!--code_no_check_fa--> 109 ```js 110 import { resourceManager } from '@kit.LocalizationKit' 111 import { BusinessError } from '@kit.BasicServicesKit'; 112 113 resourceManager.getResourceManager().then((mgr: resourceManager.ResourceManager) => { 114 mgr.getStringValue($r('app.string.test').id, (error: BusinessError, value: string) => { 115 if (error != null) { 116 console.error("error is " + error); 117 } else { 118 let str = value; 119 } 120 }); 121 }).catch((error: BusinessError) => { 122 console.error("error is " + error); 123 }); 124 ``` 125 126## resourceManager.getResourceManager 127 128getResourceManager(bundleName: string): Promise<ResourceManager> 129 130获取指定应用的资源管理对象,使用Promise异步回调。 131 132**系统能力**:SystemCapability.Global.ResourceManager 133 134**模型约束**:此接口仅可在FA模型下使用。 135 136**参数:** 137 138| 参数名 | 类型 | 必填 | 说明 | 139| ---------- | ------ | ---- | ------------- | 140| bundleName | string | 是 | 应用的Bundle名称。 | 141 142**返回值:** 143 144| 类型 | 说明 | 145| ---------------------------------------- | ------------------ | 146| Promise<[ResourceManager](#resourcemanager)> | 返回资源管理ResourceManager对象。 | 147 148**示例:** 149 <!--code_no_check_fa--> 150 ```js 151 import { resourceManager } from '@kit.LocalizationKit' 152 import { BusinessError } from '@kit.BasicServicesKit'; 153 154 resourceManager.getResourceManager("com.example.myapplication").then((mgr: resourceManager.ResourceManager) => { 155 }).catch((error: BusinessError) => { 156 }); 157 ``` 158 159## resourceManager.getSystemResourceManager<sup>10+</sup> 160 161getSystemResourceManager(): ResourceManager 162 163获取系统资源管理对象,返回系统资源的ResourceManager对象。 164 165**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 166 167**系统能力**:SystemCapability.Global.ResourceManager 168 169**返回值:** 170 171| 类型 | 说明 | 172| ---------------------------------------- | ------------------ | 173| [Resourcemanager](#resourcemanager) | 返回系统资源的管理对象。 | 174 175**错误码:** 176 177以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 178 179| 错误码ID | 错误信息 | 180| -------- | ---------------------------------------- | 181| 9001009 | Failed to access the system resource.which is not mapped to application sandbox, This error code will be thrown. | 182 183**示例:** 184 ```js 185import { resourceManager } from '@kit.LocalizationKit' 186import { BusinessError } from '@kit.BasicServicesKit'; 187 188 try { 189 let systemResourceManager = resourceManager.getSystemResourceManager(); 190 systemResourceManager.getStringValue($r('sys.string.ohos_lab_vibrate').id).then((value: string) => { 191 let str = value; 192 }).catch((error: BusinessError) => { 193 console.error("systemResourceManager getStringValue promise error is " + error); 194 }); 195 } catch (error) { 196 let code = (error as BusinessError).code; 197 let message = (error as BusinessError).message; 198 console.error(`systemResourceManager getStringValue failed, error code: ${code}, message: ${message}.`); 199 } 200 ``` 201 202## Direction 203 204用于表示设备屏幕方向。 205 206**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 207 208**系统能力**:SystemCapability.Global.ResourceManager 209 210| 名称 | 值 | 说明 | 211| -------------------- | ---- | ---- | 212| DIRECTION_VERTICAL | 0 | 竖屏。 | 213| DIRECTION_HORIZONTAL | 1 | 横屏。 | 214 215 216## DeviceType 217 218用于表示当前设备类型。 219 220**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 221 222**系统能力**:SystemCapability.Global.ResourceManager 223<!--RP1--> 224| 名称 | 值 | 说明 | 225| -------------------- | ---- | ---- | 226| DEVICE_TYPE_PHONE | 0x00 | 手机。 | 227| DEVICE_TYPE_TABLET | 0x01 | 平板。 | 228| DEVICE_TYPE_CAR | 0x02 | 汽车。 | 229| DEVICE_TYPE_TV | 0x04 | 电视。 | 230| DEVICE_TYPE_WEARABLE | 0x06 | 穿戴。 | 231| DEVICE_TYPE_2IN1<sup>11+</sup> | 0x07 | 2in1。 | 232<!--RP1End--> 233 234## ScreenDensity 235 236用于表示当前设备屏幕密度。 237 238**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 239 240**系统能力**:SystemCapability.Global.ResourceManager 241 242| 名称 | 值 | 说明 | 243| -------------- | ---- | ---------- | 244| SCREEN_SDPI | 120 | 小规模的屏幕密度。 | 245| SCREEN_MDPI | 160 | 中规模的屏幕密度。 | 246| SCREEN_LDPI | 240 | 大规模的屏幕密度。 | 247| SCREEN_XLDPI | 320 | 特大规模的屏幕密度。 | 248| SCREEN_XXLDPI | 480 | 超大规模的屏幕密度。 | 249| SCREEN_XXXLDPI | 640 | 超特大规模的屏幕密度。 | 250 251 252## ColorMode<sup>12+</sup> 253 254用于表示当前设备颜色模式。 255 256**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 257 258**系统能力**:SystemCapability.Global.ResourceManager 259 260| 名称 | 值 | 说明 | 261| ----- | ---- | ---------- | 262| DARK | 0 | 深色模式。 | 263| LIGHT | 1 | 浅色模式。 | 264 265 266## Configuration 267 268表示当前设备的状态。 269 270**系统能力**:SystemCapability.Global.ResourceManager 271 272| 名称 | 类型 | 可读 | 可写 | 说明 | 273| --------------------------- | ------------------------------- | ---- | ---- | ------------------ | 274| direction | [Direction](#direction) | 是 | 是 | 屏幕方向。<br>**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 | 275| locale | string | 是 | 是 | 语言文字国家地区。<br>**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 | 276| deviceType<sup>12+</sup> | [DeviceType](#devicetype) | 是 | 是 | 设备类型。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 | 277| screenDensity<sup>12+</sup> | [ScreenDensity](#screendensity) | 是 | 是 | 屏幕密度。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 | 278| colorMode<sup>12+</sup> | [ColorMode](#colormode12) | 是 | 是 | 颜色模式。 <br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 | 279| mcc<sup>12+</sup> | number | 是 | 是 | 移动国家码。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 | 280| mnc<sup>12+</sup> | number | 是 | 是 | 移动网络码。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 | 281 282 283 284## DeviceCapability 285 286表示设备支持的能力。 287 288**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 289 290**系统能力**:SystemCapability.Global.ResourceManager 291 292| 名称 | 类型 | 可读 | 可写 | 说明 | 293| ------------- | ------------------------------- | ---- | ---- | -------- | 294| screenDensity | [ScreenDensity](#screendensity) | 是 | 否 | 当前设备屏幕密度。 | 295| deviceType | [DeviceType](#devicetype) | 是 | 否 | 当前设备类型。 | 296 297 298## RawFileDescriptor<sup>9+</sup> 299 300type RawFileDescriptor = _RawFileDescriptor 301 302**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 303 304**系统能力:** SystemCapability.Global.ResourceManager 305 306| 类型 | 说明 | 307| ------ | ---- | 308|[_RawFileDescriptor](js-apis-rawFileDescriptor.md#rawfiledescriptor-1)|表示rawfile文件所在hap的descriptor信息。| 309 310## Resource<sup>9+</sup> 311 312type Resource = _Resource 313 314**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 315 316**系统能力:** SystemCapability.Global.ResourceManager 317 318| 类型 | 说明 | 319| ------ | ---- | 320|[_Resource](js-apis-resource.md#resource-1)|表示资源信息。| 321 322## ResourceManager 323 324提供访问应用资源的能力。 325 326> **说明:** 327> 328> - ResourceManager涉及到的方法,仅限基于TS扩展的声明式开发范式使用。 329> 330> - 资源文件在工程的resources目录中定义,通过resId、resName、resource对象等可以获取对应的字符串、字符串数组等,resId可通过`$r(资源地址).id`的方式获取,例如`$r('app.string.test').id`。 331> 332> - resource对象适用于多工程应用内的跨包访问,因resource对象需创建对应module的context获取资源,故相比于入参为resId、resName的接口耗时长。 333> 334> - 单HAP包和跨HAP/HSP包资源的访问方式具体请参考[资源访问](../../quick-start/resource-categories-and-access.md#资源访问)。 335> 336> - 示例代码中test文件的具体内容请参考[附录](#附录)。 337 338### getStringSync<sup>9+</sup> 339 340getStringSync(resId: number): string 341 342用户获取指定资源ID对应的字符串,使用同步方式返回。 343 344**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 345 346**系统能力**:SystemCapability.Global.ResourceManager 347 348**参数:** 349 350| 参数名 | 类型 | 必填 | 说明 | 351| ----- | ------ | ---- | ----- | 352| resId | number | 是 | 资源ID值。 | 353 354**返回值:** 355 356| 类型 | 说明 | 357| ------ | ----------- | 358| string | 资源ID值对应的字符串。 | 359 360**错误码:** 361 362以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 363 364| 错误码ID | 错误信息 | 365| -------- | ---------------------------------------- | 366| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 367| 9001001 | Invalid resource ID. | 368| 9001002 | No matching resource is found based on the resource ID. | 369| 9001006 | The resource is referenced cyclically. | 370 371**示例:** 372 ```ts 373 import { BusinessError } from '@kit.BasicServicesKit'; 374 375 try { 376 this.context.resourceManager.getStringSync($r('app.string.test').id); 377 } catch (error) { 378 let code = (error as BusinessError).code; 379 let message = (error as BusinessError).message; 380 console.error(`getStringSync failed, error code: ${code}, message: ${message}.`); 381 } 382 ``` 383 384### getStringSync<sup>10+</sup> 385 386getStringSync(resId: number, ...args: Array<string | number>): string 387 388用户获取指定资源ID对应的字符串,根据args参数进行格式化,使用同步方式返回。 389 390**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 391 392**系统能力**:SystemCapability.Global.ResourceManager 393 394**参数:** 395 396| 参数名 | 类型 | 必填 | 说明 | 397| ----- | ------ | ---- | ----- | 398| resId | number | 是 | 资源ID值。 | 399| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:%d、%f、%s、%%、%数字`$d`、%数字`$f`、%数字`$s`。<br>说明:%%转义为%; %数字`$d`表示使用第几个参数。<br>举例:%%d格式化后为%d字符串; %1`$d`表示使用第一个参数。| 400 401**返回值:** 402 403| 类型 | 说明 | 404| ------ | ---------------------------- | 405| string | 资源ID值对应的格式化字符串。| 406 407**错误码:** 408以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 409 410| 错误码ID | 错误信息 | 411| -------- | ---------------------------------------- | 412| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 413| 9001001 | Invalid resource ID. | 414| 9001002 | No matching resource is found based on the resource ID. | 415| 9001006 | The resource is referenced cyclically. | 416| 9001007 | Failed to format the resource obtained based on the resource ID. | 417 418**示例:** 419 ```ts 420 import { BusinessError } from '@kit.BasicServicesKit'; 421 422 try { 423 this.context.resourceManager.getStringSync($r('app.string.test').id, "format string", 10, 98.78); 424 } catch (error) { 425 let code = (error as BusinessError).code; 426 let message = (error as BusinessError).message; 427 console.error(`getStringSync failed, error code: ${code}, message: ${message}.`); 428 } 429 ``` 430 431### getStringSync<sup>9+</sup> 432 433getStringSync(resource: Resource): string 434 435用户获取指定resource对象对应的字符串,使用同步方式返回字符串。 436 437**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 438 439**系统能力**:SystemCapability.Global.ResourceManager 440 441**模型约束**:此接口仅可在Stage模型下使用。 442 443**参数:** 444 445| 参数名 | 类型 | 必填 | 说明 | 446| -------- | ---------------------- | ---- | ---- | 447| resource | [Resource](#resource9) | 是 | 资源信息。 | 448 449**返回值:** 450 451| 类型 | 说明 | 452| ------ | ---------------- | 453| string | resource对象对应的字符串。 | 454 455**错误码:** 456 457以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 458 459| 错误码ID | 错误信息 | 460| -------- | ---------------------------------------- | 461| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 462| 9001001 | Invalid resource ID. | 463| 9001002 | No matching resource is found based on the resource ID. | 464| 9001006 | The resource is referenced cyclically. | 465 466**示例:** 467 ```ts 468 import { resourceManager } from '@kit.LocalizationKit' 469 import { BusinessError } from '@kit.BasicServicesKit'; 470 471 let resource: resourceManager.Resource = { 472 bundleName: "com.example.myapplication", 473 moduleName: "entry", 474 id: $r('app.string.test').id 475 }; 476 try { 477 this.context.resourceManager.getStringSync(resource); 478 } catch (error) { 479 let code = (error as BusinessError).code; 480 let message = (error as BusinessError).message; 481 console.error(`getStringSync failed, error code: ${code}, message: ${message}.`); 482 } 483 ``` 484 485### getStringSync<sup>10+</sup> 486 487getStringSync(resource: Resource, ...args: Array<string | number>): string 488 489用户获取指定resource对象对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。 490 491**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 492 493**系统能力**:SystemCapability.Global.ResourceManager 494 495**模型约束**:此接口仅可在Stage模型下使用。 496 497**参数:** 498 499| 参数名 | 类型 | 必填 | 说明 | 500| -------- | ---------------------- | ---- | ---- | 501| resource | [Resource](#resource9) | 是 | 资源信息。 | 502| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:%d、%f、%s、%%、%数字`$d`、%数字`$f`、%数字`$s`。<br>说明:%%转义为%; %数字`$d`表示使用第几个参数。<br>举例:%%d格式化后为%d字符串; %1`$d`表示使用第一个参数。| 503 504**返回值:** 505 506| 类型 | 说明 | 507| ------ | ---------------------------- | 508| string | resource对象对应的格式化字符串。| 509 510**错误码:** 511以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 512 513| 错误码ID | 错误信息 | 514| -------- | ---------------------------------------- | 515| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 516| 9001001 | Invalid resource ID. | 517| 9001002 | No matching resource is found based on the resource ID. | 518| 9001006 | The resource is referenced cyclically. | 519| 9001007 | Failed to format the resource obtained based on the resource ID. | 520 521**示例:** 522 ```ts 523 import { resourceManager } from '@kit.LocalizationKit' 524 import { BusinessError } from '@kit.BasicServicesKit'; 525 526 let resource: resourceManager.Resource = { 527 bundleName: "com.example.myapplication", 528 moduleName: "entry", 529 id: $r('app.string.test').id 530 }; 531 try { 532 this.context.resourceManager.getStringSync(resource, "format string", 10, 98.78); 533 } catch (error) { 534 let code = (error as BusinessError).code; 535 let message = (error as BusinessError).message; 536 console.error(`getStringSync failed, error code: ${code}, message: ${message}.`); 537 } 538 ``` 539 540### getStringByNameSync<sup>9+</sup> 541 542getStringByNameSync(resName: string): string 543 544用户获取指定资源名称对应的字符串,使用同步方式返回字符串。 545 546**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 547 548**系统能力**:SystemCapability.Global.ResourceManager 549 550**参数:** 551 552| 参数名 | 类型 | 必填 | 说明 | 553| ------- | ------ | ---- | ---- | 554| resName | string | 是 | 资源名称。 | 555 556**返回值:** 557 558| 类型 | 说明 | 559| ------ | ---------- | 560| string | 资源名称对应的字符串。 | 561 562**错误码:** 563 564以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 565 566| 错误码ID | 错误信息 | 567| -------- | ---------------------------------------- | 568| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 569| 9001003 | Invalid resource name. | 570| 9001004 | No matching resource is found based on the resource name. | 571| 9001006 | The resource is referenced cyclically. | 572 573**示例:** 574 ```ts 575 import { BusinessError } from '@kit.BasicServicesKit'; 576 577 try { 578 this.context.resourceManager.getStringByNameSync("test"); 579 } catch (error) { 580 let code = (error as BusinessError).code; 581 let message = (error as BusinessError).message; 582 console.error(`getStringByNameSync failed, error code: ${code}, message: ${message}.`); 583 } 584 ``` 585 586### getStringByNameSync<sup>10+</sup> 587 588getStringByNameSync(resName: string, ...args: Array<string | number>): string 589 590用户获取指定资源名称对应的字符串,根据args参数进行格式化,使用同步方式返回相应字符串。 591 592**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 593 594**系统能力**:SystemCapability.Global.ResourceManager 595 596**参数:** 597 598| 参数名 | 类型 | 必填 | 说明 | 599| ------- | ------ | ---- | ---- | 600| resName | string | 是 | 资源名称。 | 601| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:%d、%f、%s、%%、%数字`$d`、%数字`$f`、%数字`$s`。<br>说明:%%转义为%; %数字`$d`表示使用第几个参数。<br>举例:%%d格式化后为%d字符串; %1`$d`表示使用第一个参数。| 602 603**返回值:** 604 605| 类型 | 说明 | 606| ------ | ---------------------------- | 607| string | 资源名称对应的格式化字符串。| 608 609**错误码:** 610 611以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 612 613| 错误码ID | 错误信息 | 614| -------- | ---------------------------------------- | 615| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 616| 9001003 | Invalid resource name. | 617| 9001004 | No matching resource is found based on the resource name. | 618| 9001006 | The resource is referenced cyclically. | 619| 9001008 | Failed to format the resource obtained based on the resource Name. | 620 621**示例:** 622 ```ts 623 import { BusinessError } from '@kit.BasicServicesKit'; 624 625 try { 626 this.context.resourceManager.getStringByNameSync("test", "format string", 10, 98.78); 627 } catch (error) { 628 let code = (error as BusinessError).code; 629 let message = (error as BusinessError).message; 630 console.error(`getStringByNameSync failed, error code: ${code}, message: ${message}.`); 631 } 632 ``` 633 634### getStringValue<sup>9+</sup> 635 636getStringValue(resId: number, callback: _AsyncCallback<string>): void 637 638用户获取指定资源ID对应的字符串,使用callback异步回调。 639 640**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 641 642**系统能力**:SystemCapability.Global.ResourceManager 643 644**参数:** 645 646| 参数名 | 类型 | 必填 | 说明 | 647| -------- | --------------------------- | ---- | --------------- | 648| resId | number | 是 | 资源ID值。 | 649| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 返回获取的字符串。 | 650 651**错误码:** 652 653以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 654 655| 错误码ID | 错误信息 | 656| -------- | ---------------------------------------- | 657| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 658| 9001001 | If the module resId invalid. | 659| 9001002 | No matching resource is found based on the resource ID. | 660| 9001006 | The resource is referenced cyclically. | 661 662**示例Stage:** 663 ```ts 664 import { BusinessError } from '@kit.BasicServicesKit'; 665 666 try { 667 this.context.resourceManager.getStringValue($r('app.string.test').id, (error: BusinessError, value: string) => { 668 if (error != null) { 669 console.error("error is " + error); 670 } else { 671 let str = value; 672 } 673 }); 674 } catch (error) { 675 let code = (error as BusinessError).code; 676 let message = (error as BusinessError).message; 677 console.error(`callback getStringValue failed, error code: ${code}, message: ${message}.`); 678 } 679 ``` 680 681### getStringValue<sup>9+</sup> 682 683getStringValue(resId: number): Promise<string> 684 685用户获取指定资源ID对应的字符串,使用Promise异步回调。 686 687**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 688 689**系统能力**:SystemCapability.Global.ResourceManager 690 691**参数:** 692 693| 参数名 | 类型 | 必填 | 说明 | 694| ----- | ------ | ---- | ----- | 695| resId | number | 是 | 资源ID值。 | 696 697**返回值:** 698 699| 类型 | 说明 | 700| --------------------- | ----------- | 701| Promise<string> | 资源ID值对应的字符串。 | 702 703**错误码:** 704 705以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 706 707| 错误码ID | 错误信息 | 708| -------- | ---------------------------------------- | 709| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 710| 9001001 | Invalid resource ID. | 711| 9001002 | No matching resource is found based on the resource ID. | 712| 9001006 | The resource is referenced cyclically. | 713 714**示例:** 715 ```ts 716 import { BusinessError } from '@kit.BasicServicesKit'; 717 718 try { 719 this.context.resourceManager.getStringValue($r('app.string.test').id).then((value: string) => { 720 let str = value; 721 }).catch((error: BusinessError) => { 722 console.error("getStringValue promise error is " + error); 723 }); 724 } catch (error) { 725 let code = (error as BusinessError).code; 726 let message = (error as BusinessError).message; 727 console.error(`promise getStringValue failed, error code: ${code}, message: ${message}.`); 728 } 729 ``` 730 731### getStringValue<sup>9+</sup> 732 733getStringValue(resource: Resource, callback: _AsyncCallback<string>): void 734 735用户获取指定resource对象对应的字符串,使用callback异步回调。 736 737**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 738 739**系统能力**:SystemCapability.Global.ResourceManager 740 741**模型约束**:此接口仅可在Stage模型下使用。 742 743**参数:** 744 745| 参数名 | 类型 | 必填 | 说明 | 746| -------- | --------------------------- | ---- | --------------- | 747| resource | [Resource](#resource9) | 是 | 资源信息。 | 748| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 返回获取的字符串。 | 749 750**错误码:** 751 752以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 753 754| 错误码ID | 错误信息 | 755| -------- | ---------------------------------------- | 756| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 757| 9001001 | Invalid resource ID. | 758| 9001002 | No matching resource is found based on the resource ID. | 759| 9001006 | The resource is referenced cyclically. | 760 761**示例:** 762 ```ts 763 import { resourceManager } from '@kit.LocalizationKit' 764 import { BusinessError } from '@kit.BasicServicesKit'; 765 766 let resource: resourceManager.Resource = { 767 bundleName: "com.example.myapplication", 768 moduleName: "entry", 769 id: $r('app.string.test').id 770 }; 771 try { 772 this.context.resourceManager.getStringValue(resource, (error: BusinessError, value: string) => { 773 if (error != null) { 774 console.error("error is " + error); 775 } else { 776 let str = value; 777 } 778 }); 779 } catch (error) { 780 let code = (error as BusinessError).code; 781 let message = (error as BusinessError).message; 782 console.error(`callback getStringValue failed, error code: ${code}, message: ${message}.`); 783 } 784 ``` 785 786### getStringValue<sup>9+</sup> 787 788getStringValue(resource: Resource): Promise<string> 789 790用户获取指定resource对象对应的字符串,使用Promise异步回调。 791 792**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 793 794**系统能力**:SystemCapability.Global.ResourceManager 795 796**模型约束**:此接口仅可在Stage模型下使用。 797 798**参数:** 799 800| 参数名 | 类型 | 必填 | 说明 | 801| -------- | ---------------------- | ---- | ---- | 802| resource | [Resource](#resource9) | 是 | 资源信息。 | 803 804**返回值:** 805 806| 类型 | 说明 | 807| --------------------- | ---------------- | 808| Promise<string> | resource对象对应的字符串。 | 809 810**错误码:** 811 812以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 813 814| 错误码ID | 错误信息 | 815| -------- | ---------------------------------------- | 816| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 817| 9001001 | Invalid resource ID. | 818| 9001002 | No matching resource is found based on the resource ID. | 819| 9001006 | The resource is referenced cyclically. | 820 821**示例:** 822 ```ts 823 import { resourceManager } from '@kit.LocalizationKit' 824 import { BusinessError } from '@kit.BasicServicesKit'; 825 826 let resource: resourceManager.Resource = { 827 bundleName: "com.example.myapplication", 828 moduleName: "entry", 829 id: $r('app.string.test').id 830 }; 831 try { 832 this.context.resourceManager.getStringValue(resource).then((value: string) => { 833 let str = value; 834 }).catch((error: BusinessError) => { 835 console.error("getStringValue promise error is " + error); 836 }); 837 } catch (error) { 838 let code = (error as BusinessError).code; 839 let message = (error as BusinessError).message; 840 console.error(`promise getStringValue failed, error code: ${code}, message: ${message}.`); 841 } 842 ``` 843 844### getStringByName<sup>9+</sup> 845 846getStringByName(resName: string, callback: _AsyncCallback<string>): void 847 848用户获取指定资源名称对应的字符串,使用callback异步回调。 849 850**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 851 852**系统能力**:SystemCapability.Global.ResourceManager 853 854**参数:** 855 856| 参数名 | 类型 | 必填 | 说明 | 857| -------- | --------------------------- | ---- | --------------- | 858| resName | string | 是 | 资源名称。 | 859| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 |返回获取的字符串。 | 860 861**错误码:** 862以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 863 864| 错误码ID | 错误信息 | 865| -------- | ---------------------------------------- | 866| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 867| 9001003 | Invalid resource name. | 868| 9001004 | No matching resource is found based on the resource name. | 869| 9001006 | The resource is referenced cyclically. | 870 871**示例:** 872 ```ts 873 import { BusinessError } from '@kit.BasicServicesKit'; 874 875 try { 876 this.context.resourceManager.getStringByName("test", (error: BusinessError, value: string) => { 877 if (error != null) { 878 console.error("error is " + error); 879 } else { 880 let str = value; 881 } 882 }); 883 } catch (error) { 884 let code = (error as BusinessError).code; 885 let message = (error as BusinessError).message; 886 console.error(`callback getStringByName failed, error code: ${code}, message: ${message}.`); 887 } 888 ``` 889 890### getStringByName<sup>9+</sup> 891 892getStringByName(resName: string): Promise<string> 893 894用户获取指定资源名称对应的字符串,使用Promise异步回调。 895 896**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 897 898**系统能力**:SystemCapability.Global.ResourceManager 899 900**参数:** 901 902| 参数名 | 类型 | 必填 | 说明 | 903| ------- | ------ | ---- | ---- | 904| resName | string | 是 | 资源名称。 | 905 906**返回值:** 907 908| 类型 | 说明 | 909| --------------------- | ---------- | 910| Promise<string> | 资源名称对应的字符串。 | 911 912**错误码:** 913 914以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 915 916| 错误码ID | 错误信息 | 917| -------- | ---------------------------------------- | 918| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 919| 9001003 | Invalid resource name. | 920| 9001004 | No matching resource is found based on the resource name. | 921| 9001006 | The resource is referenced cyclically. | 922 923**示例:** 924 ```ts 925 import { BusinessError } from '@kit.BasicServicesKit'; 926 927 try { 928 this.context.resourceManager.getStringByName("test").then((value: string) => { 929 let str = value; 930 }).catch((error: BusinessError) => { 931 console.error("getStringByName promise error is " + error); 932 }); 933 } catch (error) { 934 let code = (error as BusinessError).code; 935 let message = (error as BusinessError).message; 936 console.error(`promise getStringByName failed, error code: ${code}, message: ${message}.`); 937 } 938 ``` 939 940### getStringArrayValueSync<sup>10+</sup> 941 942getStringArrayValueSync(resId: number): Array<string> 943 944用户获取指定资源ID对应的字符串数组,使用同步方式返回。 945 946**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 947 948**系统能力**:SystemCapability.Global.ResourceManager 949 950**参数:** 951 952| 参数名 | 类型 | 必填 | 说明 | 953| ----- | ------ | ---- | ----- | 954| resId | number | 是 | 资源ID值。 | 955 956**返回值:** 957 958| 类型 | 说明 | 959| --------------------- | ----------- | 960| Array<string> | 资源ID值对应的字符串数组。 | 961 962**错误码:** 963 964以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 965 966| 错误码ID | 错误信息 | 967| -------- | ---------------------------------------- | 968| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 969| 9001001 | Invalid resource ID. | 970| 9001002 | No matching resource is found based on the resource ID. | 971| 9001006 | The resource is referenced cyclically. | 972 973**示例:** 974 ```ts 975 import { BusinessError } from '@kit.BasicServicesKit'; 976 977 try { 978 this.context.resourceManager.getStringArrayValueSync($r('app.strarray.test').id); 979 } catch (error) { 980 let code = (error as BusinessError).code; 981 let message = (error as BusinessError).message; 982 console.error(`getStringArrayValueSync failed, error code: ${code}, message: ${message}.`); 983 } 984 ``` 985 986### getStringArrayValueSync<sup>10+</sup> 987 988getStringArrayValueSync(resource: Resource): Array<string> 989 990用户获取指定resource对象对应的字符串数组,使用同步方式返回。 991 992**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 993 994**系统能力**:SystemCapability.Global.ResourceManager 995 996**模型约束**:此接口仅可在Stage模型下使用。 997 998**参数:** 999 1000| 参数名 | 类型 | 必填 | 说明 | 1001| ----- | ------ | ---- | ----- | 1002| resource | [Resource](#resource9) | 是 | 资源信息。 | 1003 1004**返回值:** 1005 1006| 类型 | 说明 | 1007| --------------------- | ----------- | 1008| Array<string> | resource对象对应的字符串数组。 | 1009 1010**错误码:** 1011 1012以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 1013 1014| 错误码ID | 错误信息 | 1015| -------- | ---------------------------------------- | 1016| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1017| 9001001 | Invalid resource ID. | 1018| 9001002 | No matching resource is found based on the resource ID. | 1019| 9001006 | The resource is referenced cyclically. | 1020 1021**示例:** 1022 ```ts 1023 import { resourceManager } from '@kit.LocalizationKit' 1024 import { BusinessError } from '@kit.BasicServicesKit'; 1025 1026 let resource: resourceManager.Resource = { 1027 bundleName: "com.example.myapplication", 1028 moduleName: "entry", 1029 id: $r('app.strarray.test').id 1030 }; 1031 try { 1032 this.context.resourceManager.getStringArrayValueSync(resource); 1033 } catch (error) { 1034 let code = (error as BusinessError).code; 1035 let message = (error as BusinessError).message; 1036 console.error(`getStringArrayValueSync failed, error code: ${code}, message: ${message}.`); 1037 } 1038 ``` 1039 1040### getStringArrayByNameSync<sup>10+</sup> 1041 1042getStringArrayByNameSync(resName: string): Array<string> 1043 1044用户获取指定资源名称对应的字符串数组,使用同步方式返回。 1045 1046**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 1047 1048**系统能力**:SystemCapability.Global.ResourceManager 1049 1050**参数:** 1051 1052| 参数名 | 类型 | 必填 | 说明 | 1053| ----- | ------ | ---- | ----- | 1054| resName | string | 是 | 资源名称。 | 1055 1056**返回值:** 1057 1058| 类型 | 说明 | 1059| --------------------- | ----------- | 1060| Array<string> | 对应资源名称的字符串数组。 | 1061 1062**错误码:** 1063 1064以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 1065 1066| 错误码ID | 错误信息 | 1067| -------- | ---------------------------------------- | 1068| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1069| 9001003 | Invalid resource name. | 1070| 9001004 | No matching resource is found based on the resource name. | 1071| 9001006 | The resource is referenced cyclically. | 1072 1073**示例:** 1074 ```ts 1075 try { 1076 this.context.resourceManager.getStringArrayByNameSync("test"); 1077 } catch (error) { 1078 let code = (error as BusinessError).code; 1079 let message = (error as BusinessError).message; 1080 console.error(`getStringArrayByNameSync failed, error code: ${code}, message: ${message}.`); 1081 } 1082 ``` 1083 1084### getStringArrayValue<sup>9+</sup> 1085 1086getStringArrayValue(resId: number, callback: _AsyncCallback<Array<string>>): void 1087 1088用户获取指定资源ID对应的字符串数组,使用callback异步回调。 1089 1090**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 1091 1092**系统能力**:SystemCapability.Global.ResourceManager 1093 1094**参数:** 1095 1096| 参数名 | 类型 | 必填 | 说明 | 1097| -------- | ---------------------------------------- | ---- | ----------------- | 1098| resId | number | 是 | 资源ID值。 | 1099| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Array<string>> | 是 | 返回获取的字符串数组。 | 1100 1101**错误码:** 1102以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1103 1104| 错误码ID | 错误信息 | 1105| -------- | ---------------------------------------- | 1106| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1107| 9001001 | Invalid resource ID. | 1108| 9001002 | No matching resource is found based on the resource ID. | 1109| 9001006 | The resource is referenced cyclically. | 1110 1111**示例:** 1112 ```ts 1113 import { BusinessError } from '@kit.BasicServicesKit'; 1114 1115 try { 1116 this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id, (error: BusinessError, value: Array<string>) => { 1117 if (error != null) { 1118 console.error("error is " + error); 1119 } else { 1120 let strArray = value; 1121 } 1122 }); 1123 } catch (error) { 1124 let code = (error as BusinessError).code; 1125 let message = (error as BusinessError).message; 1126 console.error(`callback getStringArrayValue failed, error code: ${code}, message: ${message}.`); 1127 } 1128 ``` 1129 1130### getStringArrayValue<sup>9+</sup> 1131 1132getStringArrayValue(resId: number): Promise<Array<string>> 1133 1134用户获取指定资源ID对应的字符串数组,使用Promise异步回调。 1135 1136**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 1137 1138**系统能力**:SystemCapability.Global.ResourceManager 1139 1140**参数:** 1141 1142| 参数名 | 类型 | 必填 | 说明 | 1143| ----- | ------ | ---- | ----- | 1144| resId | number | 是 | 资源ID值。 | 1145 1146**返回值:** 1147 1148| 类型 | 说明 | 1149| ---------------------------------- | ------------- | 1150| Promise<Array<string>> | 资源ID值对应的字符串数组。 | 1151 1152**错误码:** 1153以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1154 1155| 错误码ID | 错误信息 | 1156| -------- | ---------------------------------------- | 1157| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1158| 9001001 | Invalid resource ID. | 1159| 9001002 | No matching resource is found based on the resource ID. | 1160| 9001006 | The resource is referenced cyclically. | 1161 1162**示例:** 1163 ```ts 1164 import { BusinessError } from '@kit.BasicServicesKit'; 1165 1166 try { 1167 this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id).then((value: Array<string>) => { 1168 let strArray = value; 1169 }).catch((error: BusinessError) => { 1170 console.error("getStringArrayValue promise error is " + error); 1171 }); 1172 } catch (error) { 1173 let code = (error as BusinessError).code; 1174 let message = (error as BusinessError).message; 1175 console.error(`promise getStringArrayValue failed, error code: ${code}, message: ${message}.`); 1176 } 1177 ``` 1178 1179### getStringArrayValue<sup>9+</sup> 1180 1181getStringArrayValue(resource: Resource, callback: _AsyncCallback<Array<string>>): void 1182 1183用户获取指定resource对象对应的字符串数组,使用callback异步回调。 1184 1185**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 1186 1187**系统能力**:SystemCapability.Global.ResourceManager 1188 1189**模型约束**:此接口仅可在Stage模型下使用。 1190 1191**参数:** 1192 1193| 参数名 | 类型 | 必填 | 说明 | 1194| -------- | ---------------------------------------- | ---- | ----------------- | 1195| resource | [Resource](#resource9) | 是 | 资源信息。 | 1196| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Array<string>> | 是 | 返回获取的字符串数组。| 1197 1198**错误码:** 1199 1200以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 1201 1202| 错误码ID | 错误信息 | 1203| -------- | ---------------------------------------- | 1204| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1205| 9001001 | Invalid resource ID. | 1206| 9001002 | No matching resource is found based on the resource ID. | 1207| 9001006 | The resource is referenced cyclically. | 1208 1209**示例:** 1210 ```ts 1211 import { resourceManager } from '@kit.LocalizationKit' 1212 import { BusinessError } from '@kit.BasicServicesKit'; 1213 1214 let resource: resourceManager.Resource = { 1215 bundleName: "com.example.myapplication", 1216 moduleName: "entry", 1217 id: $r('app.strarray.test').id 1218 }; 1219 try { 1220 this.context.resourceManager.getStringArrayValue(resource, (error: BusinessError, value: Array<string>) => { 1221 if (error != null) { 1222 console.error("error is " + error); 1223 } else { 1224 let strArray = value; 1225 } 1226 }); 1227 } catch (error) { 1228 let code = (error as BusinessError).code; 1229 let message = (error as BusinessError).message; 1230 console.error(`callback getStringArrayValue failed, error code: ${code}, message: ${message}.`); 1231 } 1232 ``` 1233 1234### getStringArrayValue<sup>9+</sup> 1235 1236getStringArrayValue(resource: Resource): Promise<Array<string>> 1237 1238用户获取指定resource对象对应的字符串数组,使用Promise异步回调。 1239 1240**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 1241 1242**系统能力**:SystemCapability.Global.ResourceManager 1243 1244**模型约束**:此接口仅可在Stage模型下使用。 1245 1246**参数:** 1247 1248| 参数名 | 类型 | 必填 | 说明 | 1249| -------- | ---------------------- | ---- | ---- | 1250| resource | [Resource](#resource9) | 是 | 资源信息。 | 1251 1252**返回值:** 1253 1254| 类型 | 说明 | 1255| ---------------------------------- | ------------------ | 1256| Promise<Array<string>> | resource对象对应的字符串数组。 | 1257 1258**错误码:** 1259 1260以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 1261 1262| 错误码ID | 错误信息 | 1263| -------- | ---------------------------------------- | 1264| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1265| 9001001 | Invalid resource ID. | 1266| 9001002 | No matching resource is found based on the resource ID. | 1267| 9001006 | The resource is referenced cyclically. | 1268 1269**示例:** 1270 ```ts 1271 import { resourceManager } from '@kit.LocalizationKit' 1272 import { BusinessError } from '@kit.BasicServicesKit'; 1273 1274 let resource: resourceManager.Resource = { 1275 bundleName: "com.example.myapplication", 1276 moduleName: "entry", 1277 id: $r('app.strarray.test').id 1278 }; 1279 try { 1280 this.context.resourceManager.getStringArrayValue(resource).then((value: Array<string>) => { 1281 let strArray = value; 1282 }).catch((error: BusinessError) => { 1283 console.error("getStringArray promise error is " + error); 1284 }); 1285 } catch (error) { 1286 let code = (error as BusinessError).code; 1287 let message = (error as BusinessError).message; 1288 console.error(`promise getStringArrayValue failed, error code: ${code}, message: ${message}.`); 1289 } 1290 ``` 1291 1292### getStringArrayByName<sup>9+</sup> 1293 1294getStringArrayByName(resName: string, callback: _AsyncCallback<Array<string>>): void 1295 1296用户获取指定资源名称对应的字符串数组,使用callback异步回调。 1297 1298**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 1299 1300**系统能力**:SystemCapability.Global.ResourceManager 1301 1302**参数:** 1303 1304| 参数名 | 类型 | 必填 | 说明 | 1305| -------- | ---------------------------------------- | ---- | ----------------- | 1306| resName | string | 是 | 资源名称。 | 1307| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Array<string>> | 是 | 返回获取的字符串数组。 | 1308 1309**错误码:** 1310 1311以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1312 1313| 错误码ID | 错误信息 | 1314| -------- | ---------------------------------------- | 1315| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1316| 9001003 | Invalid resource name. | 1317| 9001004 | No matching resource is found based on the resource name. | 1318| 9001006 | The resource is referenced cyclically. | 1319 1320**示例:** 1321 ```ts 1322 import { BusinessError } from '@kit.BasicServicesKit'; 1323 1324 try { 1325 this.context.resourceManager.getStringArrayByName("test", (error: BusinessError, value: Array<string>) => { 1326 if (error != null) { 1327 console.error("error is " + error); 1328 } else { 1329 let strArray = value; 1330 } 1331 }); 1332 } catch (error) { 1333 let code = (error as BusinessError).code; 1334 let message = (error as BusinessError).message; 1335 console.error(`callback getStringArrayByName failed, error code: ${code}, message: ${message}.`); 1336 } 1337 ``` 1338 1339### getStringArrayByName<sup>9+</sup> 1340 1341getStringArrayByName(resName: string): Promise<Array<string>> 1342 1343用户获取指定资源名称对应的字符串数组,使用Promise异步回调。 1344 1345**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 1346 1347**系统能力**:SystemCapability.Global.ResourceManager 1348 1349**参数:** 1350 1351| 参数名 | 类型 | 必填 | 说明 | 1352| ------- | ------ | ---- | ---- | 1353| resName | string | 是 | 资源名称。 | 1354 1355**返回值:** 1356 1357| 类型 | 说明 | 1358| ---------------------------------- | ------------ | 1359| Promise<Array<string>> | 资源名称对应的字符串数组。 | 1360 1361**错误码:** 1362 1363以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1364 1365| 错误码ID | 错误信息 | 1366| -------- | ---------------------------------------- | 1367| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1368| 9001003 | Invalid resource name. | 1369| 9001004 | No matching resource is found based on the resource name. | 1370| 9001006 | The resource is referenced cyclically. | 1371 1372**示例:** 1373 ```ts 1374 import { BusinessError } from '@kit.BasicServicesKit'; 1375 1376 try { 1377 this.context.resourceManager.getStringArrayByName("test").then((value: Array<string>) => { 1378 let strArray = value; 1379 }).catch((error: BusinessError) => { 1380 console.error("getStringArrayByName promise error is " + error); 1381 }); 1382 } catch (error) { 1383 let code = (error as BusinessError).code; 1384 let message = (error as BusinessError).message; 1385 console.error(`promise getStringArrayByName failed, error code: ${code}, message: ${message}.`); 1386 } 1387 ``` 1388 1389### getIntPluralStringValueSync<sup>18+</sup> 1390 1391getIntPluralStringValueSync(resId: number, num: number, ...args: Array<string | number>): string; 1392 1393获取指定资源ID的单复数字符串。 1394 1395> **说明** 1396> 1397> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 1398 1399**原子化服务API**:从API version 18开始,该接口支持在原子化服务中使用。 1400 1401**系统能力**:SystemCapability.Global.ResourceManager 1402 1403**参数:** 1404 1405| 参数名 | 类型 | 必填 | 说明 | 1406| ------- | ----------------------- | ---- | ------------------------------------------------------------ | 1407| resId | number | 是 | 资源ID值。 | 1408| num | number | 是 | 数量值(整数)。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 1409| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:%d、%f、%s、%%、%数字`$d`、%数字`$f`、%数字`$s`。<br>说明:%%转义为%; %数字`$d`表示使用第几个参数。<br>举例:%%d格式化后为%d字符串; %1`$d`表示使用第一个参数。 | 1410 1411**返回值:** 1412 1413| 类型 | 说明 | 1414| ------ | ---------------------- | 1415| string | 指定ID的单复数字符串。 | 1416 1417**错误码:** 1418 1419以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1420 1421| 错误码ID | 错误信息 | 1422| -------- | ------------------------------------------------------------ | 1423| 9001001 | Invalid resource ID. | 1424| 9001002 | No matching resource is found based on the resource ID. | 1425| 9001006 | The resource is referenced cyclically. | 1426| 9001007 | Failed to format the resource obtained based on the resource ID. | 1427 1428**示例:** 1429 1430 ```ts 1431import { BusinessError } from '@kit.BasicServicesKit'; 1432 1433try { 1434 // 参数num取值为1,根据语言单复数规则对应的quantity为one, 因此在资源$r('app.plural.format_test')中取quantity为one的字符串 1435 this.context.resourceManager.getIntPluralStringValueSync($r('app.plural.format_test').id, 1, 1, "aaa", 1.1); 1436} catch (error) { 1437 let code = (error as BusinessError).code; 1438 let message = (error as BusinessError).message; 1439 console.error(`getIntPluralStringValueSync failed, error code: ${code}, message: ${message}.`); 1440} 1441 ``` 1442 1443### getIntPluralStringValueSync<sup>18+</sup> 1444 1445getIntPluralStringValueSync(resource: Resource, num: number, ...args: Array<string | number>): string; 1446 1447获取指定资源信息的单复数字符串。 1448 1449> **说明** 1450> 1451> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 1452 1453**原子化服务API**:从API version 18开始,该接口支持在原子化服务中使用。 1454 1455**系统能力**:SystemCapability.Global.ResourceManager 1456 1457**模型约束**:此接口仅可在Stage模型下使用。 1458 1459**参数:** 1460 1461| 参数名 | 类型 | 必填 | 说明 | 1462| -------- | ----------------------- | ---- | ------------------------------------------------------------ | 1463| resource | [Resource](#resource9) | 是 | 资源信息。 | 1464| num | number | 是 | 数量值(整数)。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 1465| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:%d、%f、%s、%%、%数字`$d`、%数字`$f`、%数字`$s`。<br>说明:%%转义为%; %数字`$d`表示使用第几个参数。<br>举例:%%d格式化后为%d字符串; %1`$d`表示使用第一个参数。 | 1466 1467**返回值:** 1468 1469| 类型 | 说明 | 1470| ------ | ------------------------------------ | 1471| string | 指定resource对象表示的单复数字符串。 | 1472 1473**错误码:** 1474 1475以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1476 1477| 错误码ID | 错误信息 | 1478| -------- | ------------------------------------------------------------ | 1479| 9001001 | Invalid resource ID. | 1480| 9001002 | No matching resource is found based on the resource ID. | 1481| 9001006 | The resource is referenced cyclically. | 1482| 9001007 | Failed to format the resource obtained based on the resource ID. | 1483 1484**示例:** 1485 1486 ```ts 1487import { resourceManager } from '@kit.LocalizationKit' 1488import { BusinessError } from '@kit.BasicServicesKit'; 1489 1490let resource: resourceManager.Resource = { 1491 bundleName: "com.example.myapplication", 1492 moduleName: "entry", 1493 id: $r('app.plural.format_test').id 1494}; 1495try { 1496 // 参数num取值为1,根据语言单复数规则对应的quantity为one, 因此在资源$r('app.plural.format_test')中取quantity为one的字符串 1497 this.context.resourceManager.getIntPluralStringValueSync(resource, 1, 1, "aaa", 1.1); 1498} catch (error) { 1499 let code = (error as BusinessError).code; 1500 let message = (error as BusinessError).message; 1501 console.error(`getIntPluralStringValueSync failed, error code: ${code}, message: ${message}.`); 1502} 1503 ``` 1504 1505### getIntPluralStringByNameSync<sup>18+</sup> 1506 1507getIntPluralStringByNameSync(resName: string, num: number, ...args: Array<string | number>): string; 1508 1509获取指定资源名称的单复数字符串。 1510 1511> **说明** 1512> 1513> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 1514 1515**原子化服务API**:从API version 18开始,该接口支持在原子化服务中使用。 1516 1517**系统能力**:SystemCapability.Global.ResourceManager 1518 1519**参数:** 1520 1521| 参数名 | 类型 | 必填 | 说明 | 1522| ------- | ----------------------- | ---- | ------------------------------------------------------------ | 1523| resName | string | 是 | 资源名称。 | 1524| num | number | 是 | 数量值(整数)。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 1525| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:%d、%f、%s、%%、%数字`$d`、%数字`$f`、%数字`$s`。<br>说明:%%转义为%; %数字`$d`表示使用第几个参数。<br>举例:%%d格式化后为%d字符串; %1`$d`表示使用第一个参数。 | 1526 1527**返回值:** 1528 1529| 类型 | 说明 | 1530| ------ | ------------------------------------ | 1531| string | 获取指定资源名称表示的单复数字符串。 | 1532 1533**错误码:** 1534 1535以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1536 1537| 错误码ID | 错误信息 | 1538| -------- | ------------------------------------------------------------ | 1539| 9001003 | Invalid resource name. | 1540| 9001004 | No matching resource is found based on the resource name. | 1541| 9001006 | The resource is referenced cyclically. | 1542| 9001008 | Failed to format the resource obtained based on the resource name. | 1543 1544**示例:** 1545 1546 ```ts 1547import { BusinessError } from '@kit.BasicServicesKit'; 1548 1549try { 1550 // 参数num取值为1,根据语言单复数规则对应的quantity为one, 因此在资源$r('app.plural.format_test')中取quantity为one的字符串 1551 this.context.resourceManager.getIntPluralStringByNameSync("format_test", 1, 1, "aaa", 1.1); 1552} catch (error) { 1553 let code = (error as BusinessError).code; 1554 let message = (error as BusinessError).message; 1555 console.error(`getIntPluralStringByNameSync failed, error code: ${code}, message: ${message}.`); 1556} 1557 ``` 1558 1559### getDoublePluralStringValueSync<sup>18+</sup> 1560 1561getDoublePluralStringValueSync(resId: number, num: number, ...args: Array<string | number>): string; 1562 1563获取指定资源ID的单复数字符串。 1564 1565> **说明** 1566> 1567> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 1568 1569**原子化服务API**:从API version 18开始,该接口支持在原子化服务中使用。 1570 1571**系统能力**:SystemCapability.Global.ResourceManager 1572 1573**参数:** 1574 1575| 参数名 | 类型 | 必填 | 说明 | 1576| ------- | ----------------------- | ---- | ------------------------------------------------------------ | 1577| resId | number | 是 | 资源ID值。 | 1578| num | number | 是 | 数量值(浮点数)。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 1579| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:%d、%f、%s、%%、%数字`$d`、%数字`$f`、%数字`$s`。<br>说明:%%转义为%; %数字`$d`表示使用第几个参数。<br>举例:%%d格式化后为%d字符串; %1`$d`表示使用第一个参数。 | 1580 1581**返回值:** 1582 1583| 类型 | 说明 | 1584| ------ | -------------------------------- | 1585| string | 指定ID字符串表示的单复数字符串。 | 1586 1587**错误码:** 1588 1589以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1590 1591| 错误码ID | 错误信息 | 1592| -------- | ------------------------------------------------------------ | 1593| 9001001 | Invalid resource ID. | 1594| 9001002 | No matching resource is found based on the resource ID. | 1595| 9001006 | The resource is referenced cyclically. | 1596| 9001007 | Failed to format the resource obtained based on the resource ID. | 1597 1598**示例:** 1599 1600 ```ts 1601import { BusinessError } from '@kit.BasicServicesKit'; 1602 1603try { 1604 // 参数num取值为2.1,根据语言单复数规则对应的quantity为other, 因此在资源$r('app.plural.format_test')中取quantity为other的字符串 1605 this.context.resourceManager.getDoublePluralStringValueSync($r('app.plural.format_test').id, 2.1, 2, "aaa", 1.1); 1606} catch (error) { 1607 let code = (error as BusinessError).code; 1608 let message = (error as BusinessError).message; 1609 console.error(`getDoublePluralStringValueSync failed, error code: ${code}, message: ${message}.`); 1610} 1611 ``` 1612 1613### getDoublePluralStringValueSync<sup>18+</sup> 1614 1615getDoublePluralStringValueSync(resource: Resource, num: number, ...args: Array<string | number>): string; 1616 1617获取指定资源信息的单复数字符串。 1618 1619> **说明** 1620> 1621> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 1622 1623**原子化服务API**:从API version 18开始,该接口支持在原子化服务中使用。 1624 1625**系统能力**:SystemCapability.Global.ResourceManager 1626 1627**模型约束**:此接口仅可在Stage模型下使用。 1628 1629**参数:** 1630 1631| 参数名 | 类型 | 必填 | 说明 | 1632| -------- | ----------------------- | ---- | ------------------------------------------------------------ | 1633| resource | [Resource](#resource9) | 是 | 资源信息。 | 1634| num | number | 是 | 数量值(浮点数)。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 1635| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:%d、%f、%s、%%、%数字`$d`、%数字`$f`、%数字`$s`。<br>说明:%%转义为%; %数字`$d`表示使用第几个参数。<br>举例:%%d格式化后为%d字符串; %1`$d`表示使用第一个参数。 | 1636 1637**返回值:** 1638 1639| 类型 | 说明 | 1640| ------ | ---------------------------------------- | 1641| string | 获取指定resource对象表示的单复数字符串。 | 1642 1643**错误码:** 1644 1645以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1646 1647| 错误码ID | 错误信息 | 1648| -------- | ------------------------------------------------------------ | 1649| 9001001 | Invalid resource ID. | 1650| 9001002 | No matching resource is found based on the resource ID. | 1651| 9001006 | The resource is referenced cyclically. | 1652| 9001007 | Failed to format the resource obtained based on the resource ID. | 1653 1654**示例:** 1655 1656 ```ts 1657import { resourceManager } from '@kit.LocalizationKit' 1658import { BusinessError } from '@kit.BasicServicesKit'; 1659 1660let resource: resourceManager.Resource = { 1661 bundleName: "com.example.myapplication", 1662 moduleName: "entry", 1663 id: $r('app.plural.format_test').id 1664}; 1665try { 1666 // 参数num取值为2.1,根据语言单复数规则对应的quantity为other, 因此在资源$r('app.plural.format_test')中取quantity为other的字符串 1667 this.context.resourceManager.getDoublePluralStringValueSync(resource, 2.1, 2, "aaa", 1.1); 1668} catch (error) { 1669 let code = (error as BusinessError).code; 1670 let message = (error as BusinessError).message; 1671 console.error(`getDoublePluralStringValueSync failed, error code: ${code}, message: ${message}.`); 1672} 1673 ``` 1674 1675### getDoublePluralStringByNameSync<sup>18+</sup> 1676 1677getDoublePluralStringByNameSync(resName: string, num: number, ...args: Array<string | number>): string; 1678 1679获取指定资源名称的单复数字符串。 1680 1681> **说明** 1682> 1683> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 1684 1685**原子化服务API**:从API version 18开始,该接口支持在原子化服务中使用。 1686 1687**系统能力**:SystemCapability.Global.ResourceManager 1688 1689**参数:** 1690 1691| 参数名 | 类型 | 必填 | 说明 | 1692| ------- | ----------------------- | ---- | ------------------------------------------------------------ | 1693| resName | string | 是 | 资源名称。 | 1694| num | number | 是 | 数量值(浮点数)。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 1695| ...args | Array<string \| number> | 否 | 格式化字符串资源参数。<br>支持参数类型:%d、%f、%s、%%、%数字`$d`、%数字`$f`、%数字`$s`。<br>说明:%%转义为%; %数字`$d`表示使用第几个参数。<br>举例:%%d格式化后为%d字符串; %1`$d`表示使用第一个参数。 | 1696 1697**返回值:** 1698 1699| 类型 | 说明 | 1700| ------ | -------------------------------- | 1701| string | 指定资源名称表示的单复数字符串。 | 1702 1703**错误码:** 1704 1705以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1706 1707| 错误码ID | 错误信息 | 1708| -------- | ------------------------------------------------------------ | 1709| 9001003 | Invalid resource name. | 1710| 9001004 | No matching resource is found based on the resource name. | 1711| 9001006 | The resource is referenced cyclically. | 1712| 9001008 | Failed to format the resource obtained based on the resource name. | 1713 1714**示例:** 1715 1716 ```ts 1717import { BusinessError } from '@kit.BasicServicesKit'; 1718 1719try { 1720 // 参数num取值为2.1,根据语言单复数规则对应的quantity为other, 因此在资源$r('app.plural.format_test')中取quantity为other的字符串 1721 this.context.resourceManager.getDoublePluralStringByNameSync("format_test", 2.1, 2, "aaa", 1.1); 1722} catch (error) { 1723 let code = (error as BusinessError).code; 1724 let message = (error as BusinessError).message; 1725 console.error(`getDoublePluralStringByNameSync failed, error code: ${code}, message: ${message}.`); 1726} 1727 ``` 1728 1729### getMediaContentSync<sup>10+</sup> 1730 1731getMediaContentSync(resId: number, density?: number): Uint8Array 1732 1733用户获取指定资源ID对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回。 1734 1735**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 1736 1737**系统能力**:SystemCapability.Global.ResourceManager 1738 1739**参数:** 1740 1741| 参数名 | 类型 | 必填 | 说明 | 1742| ----- | ------ | ---- | ----- | 1743| resId | number | 是 | 资源ID值。 | 1744| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 1745 1746**返回值:** 1747 1748| 类型 | 说明 | 1749| -------- | ----------- | 1750| Uint8Array | 资源ID对应的媒体文件内容。 | 1751 1752**错误码:** 1753 1754以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 1755 1756| 错误码ID | 错误信息 | 1757| -------- | ---------------------------------------- | 1758| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 1759| 9001001 | Invalid resource ID. | 1760| 9001002 | No matching resource is found based on the resource ID. | 1761 1762**示例:** 1763 ```ts 1764 import { BusinessError } from '@kit.BasicServicesKit'; 1765 1766 try { 1767 this.context.resourceManager.getMediaContentSync($r('app.media.test').id); // 默认屏幕密度 1768 } catch (error) { 1769 let code = (error as BusinessError).code; 1770 let message = (error as BusinessError).message; 1771 console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`); 1772 } 1773 1774 try { 1775 this.context.resourceManager.getMediaContentSync($r('app.media.test').id, 120); // 指定屏幕密度 1776 } catch (error) { 1777 let code = (error as BusinessError).code; 1778 let message = (error as BusinessError).message; 1779 console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`); 1780 } 1781 ``` 1782 1783### getMediaContentSync<sup>10+</sup> 1784 1785getMediaContentSync(resource: Resource, density?: number): Uint8Array 1786 1787用户获取指定resource对象对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回。 1788 1789**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 1790 1791**系统能力**:SystemCapability.Global.ResourceManager 1792 1793**模型约束**:此接口仅可在Stage模型下使用。 1794 1795**参数:** 1796 1797| 参数名 | 类型 | 必填 | 说明 | 1798| ----- | ------ | ---- | ----- | 1799| resource | [Resource](#resource9) | 是 | 资源信息。 | 1800| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 1801 1802**返回值:** 1803 1804| 类型 | 说明 | 1805| --------------------- | ----------- | 1806| Uint8Array | resource对象对应的媒体文件内容。 | 1807 1808**错误码:** 1809 1810以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 1811 1812| 错误码ID | 错误信息 | 1813| -------- | ---------------------------------------- | 1814| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 1815| 9001001 | Invalid resource ID. | 1816| 9001002 | No matching resource is found based on the resource ID. | 1817 1818**示例:** 1819 ```ts 1820 import { resourceManager } from '@kit.LocalizationKit' 1821 import { BusinessError } from '@kit.BasicServicesKit'; 1822 1823 let resource: resourceManager.Resource = { 1824 bundleName: "com.example.myapplication", 1825 moduleName: "entry", 1826 id: $r('app.media.test').id 1827 }; 1828 try { 1829 this.context.resourceManager.getMediaContentSync(resource); // 默认屏幕密度 1830 } catch (error) { 1831 let code = (error as BusinessError).code; 1832 let message = (error as BusinessError).message; 1833 console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`); 1834 } 1835 1836 try { 1837 this.context.resourceManager.getMediaContentSync(resource, 120); // 指定屏幕密度 1838 } catch (error) { 1839 let code = (error as BusinessError).code; 1840 let message = (error as BusinessError).message; 1841 console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`); 1842 } 1843 ``` 1844 1845### getMediaByNameSync<sup>10+</sup> 1846 1847getMediaByNameSync(resName: string, density?: number): Uint8Array 1848 1849用户获取指定资源名称对应的默认或指定的屏幕密度媒体文件内容,使用同步方式返回。 1850 1851**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 1852 1853**系统能力**:SystemCapability.Global.ResourceManager 1854 1855**参数:** 1856 1857| 参数名 | 类型 | 必填 | 说明 | 1858| ----- | ------ | ---- | ----- | 1859| resName | string | 是 | 资源名称。 | 1860| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 1861 1862**返回值:** 1863 1864| 类型 | 说明 | 1865| --------------------- | ----------- | 1866| Uint8Array | 对应资源名称的媒体文件内容。 | 1867 1868**错误码:** 1869 1870以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 1871 1872| 错误码ID | 错误信息 | 1873| -------- | ---------------------------------------- | 1874| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 1875| 9001003 | Invalid resource name. | 1876| 9001004 | No matching resource is found based on the resource name. | 1877 1878**示例:** 1879 ```ts 1880 import { BusinessError } from '@kit.BasicServicesKit'; 1881 1882 try { 1883 this.context.resourceManager.getMediaByNameSync("test"); // 默认屏幕密度 1884 } catch (error) { 1885 let code = (error as BusinessError).code; 1886 let message = (error as BusinessError).message; 1887 console.error(`getMediaByNameSync failed, error code: ${code}, message: ${message}.`); 1888 } 1889 1890 try { 1891 this.context.resourceManager.getMediaByNameSync("test", 120); // 指定屏幕密度 1892 } catch (error) { 1893 let code = (error as BusinessError).code; 1894 let message = (error as BusinessError).message; 1895 console.error(`getMediaByNameSync failed, error code: ${code}, message: ${message}.`); 1896 } 1897 ``` 1898 1899### getMediaContent<sup>9+</sup> 1900 1901getMediaContent(resId: number, callback: _AsyncCallback<Uint8Array>): void 1902 1903用户获取指定资源ID对应的媒体文件内容,使用callback异步回调。 1904 1905**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 1906 1907**系统能力**:SystemCapability.Global.ResourceManager 1908 1909**参数:** 1910 1911| 参数名 | 类型 | 必填 | 说明 | 1912| -------- | ------------------------------- | ---- | ------------------ | 1913| resId | number | 是 | 资源ID值。 | 1914| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Uint8Array> | 是 | 返回获取的媒体文件内容。 | 1915 1916**错误码:** 1917 1918以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1919 1920| 错误码ID | 错误信息 | 1921| -------- | ---------------------------------------- | 1922| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 1923| 9001001 | Invalid resource ID. | 1924| 9001002 | No matching resource is found based on the resource ID. | 1925 1926**示例:** 1927 ```ts 1928 import { BusinessError } from '@kit.BasicServicesKit'; 1929 1930 try { 1931 this.context.resourceManager.getMediaContent($r('app.media.test').id, (error: BusinessError, value: Uint8Array) => { 1932 if (error != null) { 1933 console.error("error is " + error); 1934 } else { 1935 let media = value; 1936 } 1937 }); 1938 } catch (error) { 1939 let code = (error as BusinessError).code; 1940 let message = (error as BusinessError).message; 1941 console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`); 1942 } 1943 ``` 1944 1945### getMediaContent<sup>10+</sup> 1946 1947getMediaContent(resId: number, density: number, callback: _AsyncCallback<Uint8Array>): void 1948 1949用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用callback异步回调。 1950 1951**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 1952 1953**系统能力**:SystemCapability.Global.ResourceManager 1954 1955**参数:** 1956 1957| 参数名 | 类型 | 必填 | 说明 | 1958| -------- | ------------------------------- | ---- | ------------------ | 1959| resId | number | 是 | 资源ID值。 | 1960| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 1961| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Uint8Array> | 是 | 返回获取的媒体文件内容。 | 1962 1963**错误码:** 1964 1965以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 1966 1967| 错误码ID | 错误信息 | 1968| -------- | ---------------------------------------- | 1969| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 1970| 9001001 | Invalid resource ID. | 1971| 9001002 | No matching resource is found based on the resource ID. | 1972 1973**示例:** 1974 ```ts 1975 import { BusinessError } from '@kit.BasicServicesKit'; 1976 1977 try { 1978 this.context.resourceManager.getMediaContent($r('app.media.test').id, 120, (error: BusinessError, value: Uint8Array) => { 1979 if (error != null) { 1980 console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); 1981 } else { 1982 let media = value; 1983 } 1984 }); 1985 } catch (error) { 1986 let code = (error as BusinessError).code; 1987 let message = (error as BusinessError).message; 1988 console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`); 1989 } 1990 ``` 1991 1992### getMediaContent<sup>9+</sup> 1993 1994getMediaContent(resId: number): Promise<Uint8Array> 1995 1996用户获取指定资源ID对应的媒体文件内容,使用Promise异步回调。 1997 1998**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 1999 2000**系统能力**:SystemCapability.Global.ResourceManager 2001 2002**参数:** 2003 2004| 参数名 | 类型 | 必填 | 说明 | 2005| ----- | ------ | ---- | ----- | 2006| resId | number | 是 | 资源ID值。 | 2007 2008**返回值:** 2009 2010| 类型 | 说明 | 2011| ------------------------- | -------------- | 2012| Promise<Uint8Array> | 资源ID值对应的媒体文件内容。 | 2013 2014**错误码:** 2015 2016以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2017 2018| 错误码ID | 错误信息 | 2019| -------- | ---------------------------------------- | 2020| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 2021| 9001001 | Invalid resource ID. | 2022| 9001002 | No matching resource is found based on the resource ID. | 2023 2024**示例:** 2025 ```ts 2026 import { BusinessError } from '@kit.BasicServicesKit'; 2027 2028 try { 2029 this.context.resourceManager.getMediaContent($r('app.media.test').id).then((value: Uint8Array) => { 2030 let media = value; 2031 }).catch((error: BusinessError) => { 2032 console.error("getMediaContent promise error is " + error); 2033 }); 2034 } catch (error) { 2035 let code = (error as BusinessError).code; 2036 let message = (error as BusinessError).message; 2037 console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`); 2038 } 2039 ``` 2040 2041### getMediaContent<sup>10+</sup> 2042 2043getMediaContent(resId: number, density: number): Promise<Uint8Array> 2044 2045用户获取指定资源ID对应的指定屏幕密度媒体文件内容,使用Promise异步回调。 2046 2047**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2048 2049**系统能力**:SystemCapability.Global.ResourceManager 2050 2051**参数:** 2052 2053| 参数名 | 类型 | 必填 | 说明 | 2054| ----- | ------ | ---- | ----- | 2055| resId | number | 是 | 资源ID值。 | 2056| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 2057 2058**返回值:** 2059 2060| 类型 | 说明 | 2061| ------------------------- | -------------- | 2062| Promise<Uint8Array> | 资源ID值对应的媒体文件内容。 | 2063 2064**错误码:** 2065 2066以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 2067 2068| 错误码ID | 错误信息 | 2069| -------- | ---------------------------------------- | 2070| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2071| 9001001 | Invalid resource ID. | 2072| 9001002 | No matching resource is found based on the resource ID. | 2073 2074**示例:** 2075 ```ts 2076 import { BusinessError } from '@kit.BasicServicesKit'; 2077 2078 try { 2079 this.context.resourceManager.getMediaContent($r('app.media.test').id, 120).then((value: Uint8Array) => { 2080 let media = value; 2081 }).catch((error: BusinessError) => { 2082 console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); 2083 }); 2084 } catch (error) { 2085 let code = (error as BusinessError).code; 2086 let message = (error as BusinessError).message; 2087 console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`); 2088 } 2089 ``` 2090 2091### getMediaContent<sup>9+</sup> 2092 2093getMediaContent(resource: Resource, callback: _AsyncCallback<Uint8Array>): void 2094 2095用户获取指定resource对象对应的媒体文件内容,使用callback异步回调。 2096 2097**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2098 2099**系统能力**:SystemCapability.Global.ResourceManager 2100 2101**模型约束**:此接口仅可在Stage模型下使用。 2102 2103**参数:** 2104 2105| 参数名 | 类型 | 必填 | 说明 | 2106| -------- | ------------------------------- | ---- | ------------------ | 2107| resource | [Resource](#resource9) | 是 | 资源信息。 | 2108| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Uint8Array> | 是 | 返回获取的媒体文件内容。 | 2109 2110**错误码:** 2111 2112以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 2113 2114| 错误码ID | 错误信息 | 2115| -------- | ---------------------------------------- | 2116| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 2117| 9001001 | Invalid resource ID. | 2118| 9001002 | No matching resource is found based on the resource ID. | 2119 2120**示例:** 2121 ```ts 2122 import { resourceManager } from '@kit.LocalizationKit' 2123 import { BusinessError } from '@kit.BasicServicesKit'; 2124 2125 let resource: resourceManager.Resource = { 2126 bundleName: "com.example.myapplication", 2127 moduleName: "entry", 2128 id: $r('app.media.test').id 2129 }; 2130 try { 2131 this.context.resourceManager.getMediaContent(resource, (error: BusinessError, value: Uint8Array) => { 2132 if (error != null) { 2133 console.error("error is " + error); 2134 } else { 2135 let media = value; 2136 } 2137 }); 2138 } catch (error) { 2139 let code = (error as BusinessError).code; 2140 let message = (error as BusinessError).message; 2141 console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`); 2142 } 2143 ``` 2144 2145### getMediaContent<sup>10+</sup> 2146 2147getMediaContent(resource: Resource, density: number, callback: _AsyncCallback<Uint8Array>): void 2148 2149用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用callback异步回调。 2150 2151**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2152 2153**系统能力**:SystemCapability.Global.ResourceManager 2154 2155**模型约束**:此接口仅可在Stage模型下使用。 2156 2157**参数:** 2158 2159| 参数名 | 类型 | 必填 | 说明 | 2160| -------- | ------------------------------- | ---- | ------------------ | 2161| resource | [Resource](#resource9) | 是 | 资源信息。 | 2162| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 2163| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Uint8Array> | 是 | 返回获取的媒体文件内容。 | 2164 2165**错误码:** 2166 2167以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 2168 2169| 错误码ID | 错误信息 | 2170| -------- | ---------------------------------------- | 2171| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2172| 9001001 | Invalid resource ID. | 2173| 9001002 | No matching resource is found based on the resource ID. | 2174 2175**示例:** 2176 ```ts 2177 import { resourceManager } from '@kit.LocalizationKit' 2178 import { BusinessError } from '@kit.BasicServicesKit'; 2179 2180 let resource: resourceManager.Resource = { 2181 bundleName: "com.example.myapplication", 2182 moduleName: "entry", 2183 id: $r('app.media.test').id 2184 }; 2185 try { 2186 this.context.resourceManager.getMediaContent(resource, 120, (error: BusinessError, value: Uint8Array) => { 2187 if (error != null) { 2188 console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); 2189 } else { 2190 let media = value; 2191 } 2192 }); 2193 } catch (error) { 2194 let code = (error as BusinessError).code; 2195 let message = (error as BusinessError).message; 2196 console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`); 2197 } 2198 ``` 2199 2200### getMediaContent<sup>9+</sup> 2201 2202getMediaContent(resource: Resource): Promise<Uint8Array> 2203 2204用户获取指定resource对象对应的媒体文件内容,使用Promise异步回调。 2205 2206**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2207 2208**系统能力**:SystemCapability.Global.ResourceManager 2209 2210**模型约束**:此接口仅可在Stage模型下使用。 2211 2212**参数:** 2213 2214| 参数名 | 类型 | 必填 | 说明 | 2215| -------- | ---------------------- | ---- | ---- | 2216| resource | [Resource](#resource9) | 是 | 资源信息。 | 2217 2218**返回值:** 2219 2220| 类型 | 说明 | 2221| ------------------------- | ------------------- | 2222| Promise<Uint8Array> | resource对象对应的媒体文件内容。 | 2223 2224**错误码:** 2225 2226以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 2227 2228| 错误码ID | 错误信息 | 2229| -------- | ---------------------------------------- | 2230| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 2231| 9001001 | Invalid resource ID. | 2232| 9001002 | No matching resource is found based on the resource ID. | 2233 2234**示例:** 2235 ```ts 2236 import { resourceManager } from '@kit.LocalizationKit' 2237 import { BusinessError } from '@kit.BasicServicesKit'; 2238 2239 let resource: resourceManager.Resource = { 2240 bundleName: "com.example.myapplication", 2241 moduleName: "entry", 2242 id: $r('app.media.test').id 2243 }; 2244 try { 2245 this.context.resourceManager.getMediaContent(resource).then((value: Uint8Array) => { 2246 let media = value; 2247 }).catch((error: BusinessError) => { 2248 console.error("getMediaContent promise error is " + error); 2249 }); 2250 } catch (error) { 2251 let code = (error as BusinessError).code; 2252 let message = (error as BusinessError).message; 2253 console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`); 2254 } 2255 ``` 2256 2257### getMediaContent<sup>10+</sup> 2258 2259getMediaContent(resource: Resource, density: number): Promise<Uint8Array> 2260 2261用户获取指定resource对象对应的指定屏幕密度媒体文件内容,使用Promise异步回调。 2262 2263**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2264 2265**系统能力**:SystemCapability.Global.ResourceManager 2266 2267**模型约束**:此接口仅可在Stage模型下使用。 2268 2269**参数:** 2270 2271| 参数名 | 类型 | 必填 | 说明 | 2272| -------- | ---------------------- | ---- | ---- | 2273| resource | [Resource](#resource9) | 是 | 资源信息。 | 2274| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 2275 2276**返回值:** 2277 2278| 类型 | 说明 | 2279| ------------------------- | ------------------- | 2280| Promise<Uint8Array> | resource对象对应的媒体文件内容。 | 2281 2282**错误码:** 2283 2284以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 2285 2286| 错误码ID | 错误信息 | 2287| -------- | ---------------------------------------- | 2288| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2289| 9001001 | Invalid resource ID. | 2290| 9001002 | No matching resource is found based on the resource ID. | 2291 2292**示例:** 2293 ```ts 2294 import { resourceManager } from '@kit.LocalizationKit' 2295 import { BusinessError } from '@kit.BasicServicesKit'; 2296 2297 let resource: resourceManager.Resource = { 2298 bundleName: "com.example.myapplication", 2299 moduleName: "entry", 2300 id: $r('app.media.test').id 2301 }; 2302 try { 2303 this.context.resourceManager.getMediaContent(resource, 120).then((value: Uint8Array) => { 2304 let media = value; 2305 }).catch((error: BusinessError) => { 2306 console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); 2307 }); 2308 } catch (error) { 2309 let code = (error as BusinessError).code; 2310 let message = (error as BusinessError).message; 2311 console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`); 2312 } 2313 ``` 2314 2315### getMediaByName<sup>9+</sup> 2316 2317getMediaByName(resName: string, callback: _AsyncCallback<Uint8Array>): void 2318 2319用户获取指定资源名称对应的媒体文件内容,使用callback异步回调。 2320 2321**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2322 2323**系统能力**:SystemCapability.Global.ResourceManager 2324 2325**参数:** 2326 2327| 参数名 | 类型 | 必填 | 说明 | 2328| -------- | ------------------------------- | ---- | ------------------ | 2329| resName | string | 是 | 资源名称。 | 2330| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Uint8Array> | 是 | 返回获取的媒体文件内容。 | 2331 2332**错误码:** 2333以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2334 2335| 错误码ID | 错误信息 | 2336| -------- | ---------------------------------------- | 2337| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 2338| 9001003 | Invalid resource name. | 2339| 9001004 | No matching resource is found based on the resource name. | 2340 2341**示例:** 2342 ```ts 2343 import { BusinessError } from '@kit.BasicServicesKit'; 2344 2345 try { 2346 this.context.resourceManager.getMediaByName("test", (error: BusinessError, value: Uint8Array) => { 2347 if (error != null) { 2348 console.error("error is " + error); 2349 } else { 2350 let media = value; 2351 } 2352 }); 2353 } catch (error) { 2354 let code = (error as BusinessError).code; 2355 let message = (error as BusinessError).message; 2356 console.error(`callback getMediaByName failed, error code: ${code}, message: ${message}.`); 2357 } 2358 ``` 2359 2360### getMediaByName<sup>10+</sup> 2361 2362getMediaByName(resName: string, density: number, callback: _AsyncCallback<Uint8Array>): void 2363 2364用户获取指定资源名称对应的指定屏幕密度媒体文件内容,使用callback异步回调。 2365 2366**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2367 2368**系统能力**:SystemCapability.Global.ResourceManager 2369 2370**参数:** 2371 2372| 参数名 | 类型 | 必填 | 说明 | 2373| -------- | ------------------------------- | ---- | ------------------ | 2374| resName | string | 是 | 资源名称。 | 2375| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 2376| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Uint8Array> | 是 | 返回获取的媒体文件内容。 | 2377 2378**错误码:** 2379 2380以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 2381 2382| 错误码ID | 错误信息 | 2383| -------- | ---------------------------------------- | 2384| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2385| 9001003 | Invalid resource name. | 2386| 9001004 | No matching resource is found based on the resource name. | 2387 2388**示例:** 2389 ```ts 2390 import { BusinessError } from '@kit.BasicServicesKit'; 2391 2392 try { 2393 this.context.resourceManager.getMediaByName("test", 120, (error: BusinessError, value: Uint8Array) => { 2394 if (error != null) { 2395 console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); 2396 } else { 2397 let media = value; 2398 } 2399 }); 2400 } catch (error) { 2401 let code = (error as BusinessError).code; 2402 let message = (error as BusinessError).message; 2403 console.error(`callback getMediaByName failed, error code: ${code}, message: ${message}.`); 2404 } 2405 ``` 2406 2407### getMediaByName<sup>9+</sup> 2408 2409getMediaByName(resName: string): Promise<Uint8Array> 2410 2411用户获取指定资源名称对应的媒体文件内容,使用Promise异步回调。 2412 2413**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2414 2415**系统能力**:SystemCapability.Global.ResourceManager 2416 2417**参数:** 2418 2419| 参数名 | 类型 | 必填 | 说明 | 2420| ------- | ------ | ---- | ---- | 2421| resName | string | 是 | 资源名称。 | 2422 2423**返回值:** 2424 2425| 类型 | 说明 | 2426| ------------------------- | ------------- | 2427| Promise<Uint8Array> | 资源名称对应的媒体文件内容。 | 2428 2429**错误码:** 2430 2431以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2432 2433| 错误码ID | 错误信息 | 2434| -------- | ---------------------------------------- | 2435| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 2436| 9001003 | Invalid resource name. | 2437| 9001004 | No matching resource is found based on the resource name. | 2438 2439**示例:** 2440 ```ts 2441 import { BusinessError } from '@kit.BasicServicesKit'; 2442 2443 try { 2444 this.context.resourceManager.getMediaByName("test").then((value: Uint8Array) => { 2445 let media = value; 2446 }).catch((error: BusinessError) => { 2447 console.error("getMediaByName promise error is " + error); 2448 }); 2449 } catch (error) { 2450 let code = (error as BusinessError).code; 2451 let message = (error as BusinessError).message; 2452 console.error(`promise getMediaByName failed, error code: ${code}, message: ${message}.`); 2453 } 2454 ``` 2455 2456### getMediaByName<sup>10+</sup> 2457 2458getMediaByName(resName: string, density: number): Promise<Uint8Array> 2459 2460用户获取指定资源名称对应的指定屏幕密度媒体文件内容,使用Promise异步回调。 2461 2462**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2463 2464**系统能力**:SystemCapability.Global.ResourceManager 2465 2466**参数:** 2467 2468| 参数名 | 类型 | 必填 | 说明 | 2469| ------- | ------ | ---- | ---- | 2470| resName | string | 是 | 资源名称。 | 2471| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 2472 2473**返回值:** 2474 2475| 类型 | 说明 | 2476| ------------------------- | ------------- | 2477| Promise<Uint8Array> | 资源名称对应的媒体文件内容。 | 2478 2479**错误码:** 2480 2481以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 2482 2483| 错误码ID | 错误信息 | 2484| -------- | ---------------------------------------- | 2485| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2486| 9001003 | Invalid resource name. | 2487| 9001004 | No matching resource is found based on the resource name. | 2488 2489**示例:** 2490 ```ts 2491 import { BusinessError } from '@kit.BasicServicesKit'; 2492 2493 try { 2494 this.context.resourceManager.getMediaByName("test", 120).then((value: Uint8Array) => { 2495 let media = value; 2496 }).catch((error: BusinessError) => { 2497 console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); 2498 }); 2499 } catch (error) { 2500 let code = (error as BusinessError).code; 2501 let message = (error as BusinessError).message; 2502 console.error(`promise getMediaByName failed, error code: ${code}, message: ${message}.`); 2503 } 2504 ``` 2505 2506### getMediaContentBase64Sync<sup>10+</sup> 2507 2508getMediaContentBase64Sync(resId: number, density?: number): string 2509 2510用户获取指定资源ID对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回。 2511 2512**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2513 2514**系统能力**:SystemCapability.Global.ResourceManager 2515 2516**参数:** 2517 2518| 参数名 | 类型 | 必填 | 说明 | 2519| ----- | ------ | ---- | ----- | 2520| resId | number | 是 | 资源ID值。 | 2521| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 2522 2523**返回值:** 2524 2525| 类型 | 说明 | 2526| -------- | ----------- | 2527| string | 资源ID对应的图片资源Base64编码。 | 2528 2529**错误码:** 2530 2531以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 2532 2533| 错误码ID | 错误信息 | 2534| -------- | ---------------------------------------- | 2535| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2536| 9001001 | Invalid resource ID. | 2537| 9001002 | No matching resource is found based on the resource ID. | 2538 2539**示例:** 2540 ```ts 2541 import { BusinessError } from '@kit.BasicServicesKit'; 2542 2543 try { 2544 this.context.resourceManager.getMediaContentBase64Sync($r('app.media.test').id); // 默认屏幕密度 2545 } catch (error) { 2546 let code = (error as BusinessError).code; 2547 let message = (error as BusinessError).message; 2548 console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`); 2549 } 2550 2551 try { 2552 this.context.resourceManager.getMediaContentBase64Sync($r('app.media.test').id, 120); // 指定屏幕密度 2553 } catch (error) { 2554 let code = (error as BusinessError).code; 2555 let message = (error as BusinessError).message; 2556 console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`); 2557 } 2558 ``` 2559 2560### getMediaContentBase64Sync<sup>10+</sup> 2561 2562getMediaContentBase64Sync(resource: Resource, density?: number): string 2563 2564用户获取指定resource对象对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回。 2565 2566**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2567 2568**系统能力**:SystemCapability.Global.ResourceManager 2569 2570**模型约束**:此接口仅可在Stage模型下使用。 2571 2572**参数:** 2573 2574| 参数名 | 类型 | 必填 | 说明 | 2575| ----- | ------ | ---- | ----- | 2576| resource | [Resource](#resource9) | 是 | 资源信息。 | 2577| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 2578 2579**返回值:** 2580 2581| 类型 | 说明 | 2582| --------------------- | ----------- | 2583| string | resource对象对应的图片资源Base64编码。 | 2584 2585**错误码:** 2586 2587以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 2588 2589| 错误码ID | 错误信息 | 2590| -------- | ---------------------------------------- | 2591| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2592| 9001001 | Invalid resource ID. | 2593| 9001002 | No matching resource is found based on the resource ID. | 2594 2595**示例:** 2596 ```ts 2597 import { resourceManager } from '@kit.LocalizationKit' 2598 import { BusinessError } from '@kit.BasicServicesKit'; 2599 2600 let resource: resourceManager.Resource = { 2601 bundleName: "com.example.myapplication", 2602 moduleName: "entry", 2603 id: $r('app.media.test').id 2604 }; 2605 try { 2606 this.context.resourceManager.getMediaContentBase64Sync(resource); // 默认屏幕密度 2607 } catch (error) { 2608 let code = (error as BusinessError).code; 2609 let message = (error as BusinessError).message; 2610 console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`); 2611 } 2612 2613 try { 2614 this.context.resourceManager.getMediaContentBase64Sync(resource, 120); // 指定屏幕密度 2615 } catch (error) { 2616 let code = (error as BusinessError).code; 2617 let message = (error as BusinessError).message; 2618 console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`); 2619 } 2620 ``` 2621 2622### getMediaBase64ByNameSync<sup>10+</sup> 2623 2624getMediaBase64ByNameSync(resName: string, density?: number): string 2625 2626用户获取指定资源名称对应的默认或指定的屏幕密度图片资源Base64编码,使用同步方式返回。 2627 2628**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2629 2630**系统能力**:SystemCapability.Global.ResourceManager 2631 2632**参数:** 2633 2634| 参数名 | 类型 | 必填 | 说明 | 2635| ----- | ------ | ---- | ----- | 2636| resName | string | 是 | 资源名称。 | 2637| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 2638 2639**返回值:** 2640 2641| 类型 | 说明 | 2642| --------------------- | ----------- | 2643| string | 资源名称对应的图片资源Base64编码。 | 2644 2645**错误码:** 2646 2647以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 2648 2649| 错误码ID | 错误信息 | 2650| -------- | ---------------------------------------- | 2651| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2652| 9001003 | Invalid resource name. | 2653| 9001004 | No matching resource is found based on the resource name. | 2654 2655**示例:** 2656 ```ts 2657 import { BusinessError } from '@kit.BasicServicesKit'; 2658 2659 try { 2660 this.context.resourceManager.getMediaBase64ByNameSync("test"); // 默认屏幕密度 2661 } catch (error) { 2662 let code = (error as BusinessError).code; 2663 let message = (error as BusinessError).message; 2664 console.error(`getMediaBase64ByNameSync failed, error code: ${code}, message: ${message}.`); 2665 } 2666 2667 try { 2668 this.context.resourceManager.getMediaBase64ByNameSync("test", 120); // 指定屏幕密度 2669 } catch (error) { 2670 let code = (error as BusinessError).code; 2671 let message = (error as BusinessError).message; 2672 console.error(`getMediaBase64ByNameSync failed, error code: ${code}, message: ${message}.`); 2673 } 2674 ``` 2675 2676### getMediaContentBase64<sup>9+</sup> 2677 2678getMediaContentBase64(resId: number, callback: _AsyncCallback<string>): void 2679 2680用户获取指定资源ID对应的图片资源Base64编码,使用callback异步回调。 2681 2682**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2683 2684**系统能力**:SystemCapability.Global.ResourceManager 2685 2686**参数:** Content 2687 2688| 参数名 | 类型 | 必填 | 说明 | 2689| -------- | --------------------------- | ---- | ------------------------ | 2690| resId | number | 是 | 资源ID值。 | 2691| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 返回获取的图片资源Base64编码。 | 2692 2693**错误码:** 2694 2695以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2696 2697| 错误码ID | 错误信息 | 2698| -------- | ---------------------------------------- | 2699| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 2700| 9001001 | Invalid resource ID. | 2701| 9001002 | No matching resource is found based on the resource ID. | 2702 2703**示例:** 2704 ```ts 2705 import { BusinessError } from '@kit.BasicServicesKit'; 2706 2707 try { 2708 this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, (error: BusinessError, value: string) => { 2709 if (error != null) { 2710 console.error("error is " + error); 2711 } else { 2712 let media = value; 2713 } 2714 }); 2715 } catch (error) { 2716 let code = (error as BusinessError).code; 2717 let message = (error as BusinessError).message; 2718 console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2719 } 2720 ``` 2721 2722### getMediaContentBase64<sup>10+</sup> 2723 2724getMediaContentBase64(resId: number, density: number, callback: _AsyncCallback<string>): void 2725 2726用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用callback异步回调。 2727 2728**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2729 2730**系统能力**:SystemCapability.Global.ResourceManager 2731 2732**参数:** 2733 2734| 参数名 | 类型 | 必填 | 说明 | 2735| -------- | --------------------------- | ---- | ------------------------ | 2736| resId | number | 是 | 资源ID值。 | 2737| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 2738| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 获取的图片资源Base64编码。 | 2739 2740**错误码:** 2741 2742以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 2743 2744| 错误码ID | 错误信息 | 2745| -------- | ---------------------------------------- | 2746| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2747| 9001001 | Invalid resource ID. | 2748| 9001002 | No matching resource is found based on the resource ID. | 2749 2750**示例:** 2751 ```ts 2752 import { BusinessError } from '@kit.BasicServicesKit'; 2753 2754 try { 2755 this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120, (error: BusinessError, value: string) => { 2756 if (error != null) { 2757 console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); 2758 } else { 2759 let media = value; 2760 } 2761 }); 2762 } catch (error) { 2763 let code = (error as BusinessError).code; 2764 let message = (error as BusinessError).message; 2765 console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2766 } 2767 ``` 2768 2769### getMediaContentBase64<sup>9+</sup> 2770 2771getMediaContentBase64(resId: number): Promise<string> 2772 2773用户获取指定资源ID对应的图片资源Base64编码,使用Promise异步回调。 2774 2775**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2776 2777**系统能力**:SystemCapability.Global.ResourceManager 2778 2779**参数:** 2780 2781| 参数名 | 类型 | 必填 | 说明 | 2782| ----- | ------ | ---- | ----- | 2783| resId | number | 是 | 资源ID值。 | 2784 2785**返回值:** 2786 2787| 类型 | 说明 | 2788| --------------------- | -------------------- | 2789| Promise<string> | 资源ID值对应的图片资源Base64编码。 | 2790 2791**错误码:** 2792 2793以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 2794 2795| 错误码ID | 错误信息 | 2796| -------- | ---------------------------------------- | 2797| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 2798| 9001001 | Invalid resource ID. | 2799| 9001002 | No matching resource is found based on the resource ID. | 2800 2801**示例:** 2802 ```ts 2803 import { BusinessError } from '@kit.BasicServicesKit'; 2804 2805 try { 2806 this.context.resourceManager.getMediaContentBase64($r('app.media.test').id).then((value: string) => { 2807 let media = value; 2808 }).catch((error: BusinessError) => { 2809 console.error("getMediaContentBase64 promise error is " + error); 2810 }); 2811 } catch (error) { 2812 let code = (error as BusinessError).code; 2813 let message = (error as BusinessError).message; 2814 console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2815 } 2816 ``` 2817 2818### getMediaContentBase64<sup>10+</sup> 2819 2820getMediaContentBase64(resId: number, density: number): Promise<string> 2821 2822用户获取指定资源ID对应的指定屏幕密度图片资源Base64编码,使用Promise异步回调。 2823 2824**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2825 2826**系统能力**:SystemCapability.Global.ResourceManager 2827 2828**参数:** 2829 2830| 参数名 | 类型 | 必填 | 说明 | 2831| ----- | ------ | ---- | ----- | 2832| resId | number | 是 | 资源ID值。 | 2833| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 2834 2835**返回值:** 2836 2837| 类型 | 说明 | 2838| --------------------- | -------------------- | 2839| Promise<string> | 资源ID值对应的图片资源Base64编码。 | 2840 2841**错误码:** 2842 2843以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 2844 2845| 错误码ID | 错误信息 | 2846| -------- | ---------------------------------------- | 2847| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2848| 9001001 | Invalid resource ID. | 2849| 9001002 | No matching resource is found based on the resource ID. | 2850 2851**示例:** 2852 ```ts 2853 import { BusinessError } from '@kit.BasicServicesKit'; 2854 2855 try { 2856 this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120).then((value: string) => { 2857 let media = value; 2858 }).catch((error: BusinessError) => { 2859 console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); 2860 }); 2861 } catch (error) { 2862 let code = (error as BusinessError).code; 2863 let message = (error as BusinessError).message; 2864 console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2865 } 2866 ``` 2867 2868### getMediaContentBase64<sup>9+</sup> 2869 2870getMediaContentBase64(resource: Resource, callback: _AsyncCallback<string>): void 2871 2872用户获取指定resource对象对应的图片资源Base64编码,使用callback异步回调。 2873 2874**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2875 2876**系统能力**:SystemCapability.Global.ResourceManager 2877 2878**模型约束**:此接口仅可在Stage模型下使用。 2879 2880**参数:** 2881 2882| 参数名 | 类型 | 必填 | 说明 | 2883| -------- | --------------------------- | ---- | ------------------------ | 2884| resource | [Resource](#resource9) | 是 | 资源信息。 | 2885| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 返回获取的图片资源Base64编码。 | 2886 2887**错误码:** 2888 2889以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 2890 2891| 错误码ID | 错误信息 | 2892| -------- | ---------------------------------------- | 2893| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 2894| 9001001 | Invalid resource ID. | 2895| 9001002 | No matching resource is found based on the resource ID. | 2896 2897**示例:** 2898 ```ts 2899 import { resourceManager } from '@kit.LocalizationKit' 2900 import { BusinessError } from '@kit.BasicServicesKit'; 2901 2902 let resource: resourceManager.Resource = { 2903 bundleName: "com.example.myapplication", 2904 moduleName: "entry", 2905 id: $r('app.media.test').id 2906 }; 2907 try { 2908 this.context.resourceManager.getMediaContentBase64(resource, (error: BusinessError, value: string) => { 2909 if (error != null) { 2910 console.error("error is " + error); 2911 } else { 2912 let media = value; 2913 } 2914 }); 2915 } catch (error) { 2916 let code = (error as BusinessError).code; 2917 let message = (error as BusinessError).message; 2918 console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2919 } 2920 ``` 2921 2922### getMediaContentBase64<sup>10+</sup> 2923 2924getMediaContentBase64(resource: Resource, density: number, callback: _AsyncCallback<string>): void 2925 2926用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用callback异步回调。 2927 2928**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2929 2930**系统能力**:SystemCapability.Global.ResourceManager 2931 2932**模型约束**:此接口仅可在Stage模型下使用。 2933 2934**参数:** 2935 2936| 参数名 | 类型 | 必填 | 说明 | 2937| -------- | --------------------------- | ---- | ------------------------ | 2938| resource | [Resource](#resource9) | 是 | 资源信息。 | 2939| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 2940| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 获取的图片资源Base64编码。 | 2941 2942**错误码:** 2943 2944以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 2945 2946| 错误码ID | 错误信息 | 2947| -------- | ---------------------------------------- | 2948| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 2949| 9001001 | Invalid resource ID. | 2950| 9001002 | No matching resource is found based on the resource ID. | 2951 2952**示例:** 2953 ```ts 2954 import { resourceManager } from '@kit.LocalizationKit' 2955 import { BusinessError } from '@kit.BasicServicesKit'; 2956 2957 let resource: resourceManager.Resource = { 2958 bundleName: "com.example.myapplication", 2959 moduleName: "entry", 2960 id: $r('app.media.test').id 2961 }; 2962 try { 2963 this.context.resourceManager.getMediaContentBase64(resource, 120, (error: BusinessError, value: string) => { 2964 if (error != null) { 2965 console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); 2966 } else { 2967 let media = value; 2968 } 2969 }); 2970 } catch (error) { 2971 let code = (error as BusinessError).code; 2972 let message = (error as BusinessError).message; 2973 console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2974 } 2975 ``` 2976 2977### getMediaContentBase64<sup>9+</sup> 2978 2979getMediaContentBase64(resource: Resource): Promise<string> 2980 2981用户获取指定resource对象对应的图片资源Base64编码,使用Promise异步回调。 2982 2983**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 2984 2985**系统能力**:SystemCapability.Global.ResourceManager 2986 2987**模型约束**:此接口仅可在Stage模型下使用。 2988 2989**参数:** 2990 2991| 参数名 | 类型 | 必填 | 说明 | 2992| -------- | ---------------------- | ---- | ---- | 2993| resource | [Resource](#resource9) | 是 | 资源信息。 | 2994 2995**返回值:** 2996 2997| 类型 | 说明 | 2998| --------------------- | ------------------------- | 2999| Promise<string> | resource对象对应的图片资源Base64编码。 | 3000 3001**错误码:** 3002 3003以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 3004 3005| 错误码ID | 错误信息 | 3006| -------- | ---------------------------------------- | 3007| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3008| 9001001 | Invalid resource ID. | 3009| 9001002 | No matching resource is found based on the resource ID. | 3010 3011**示例:** 3012 ```ts 3013 import { resourceManager } from '@kit.LocalizationKit' 3014 import { BusinessError } from '@kit.BasicServicesKit'; 3015 3016 let resource: resourceManager.Resource = { 3017 bundleName: "com.example.myapplication", 3018 moduleName: "entry", 3019 id: $r('app.media.test').id 3020 }; 3021 try { 3022 this.context.resourceManager.getMediaContentBase64(resource).then((value: string) => { 3023 let media = value; 3024 }).catch((error: BusinessError) => { 3025 console.error("getMediaContentBase64 promise error is " + error); 3026 }); 3027 } catch (error) { 3028 let code = (error as BusinessError).code; 3029 let message = (error as BusinessError).message; 3030 console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 3031 } 3032 ``` 3033 3034### getMediaContentBase64<sup>10+</sup> 3035 3036getMediaContentBase64(resource: Resource, density: number): Promise<string> 3037 3038用户获取指定resource对象对应的指定屏幕密度图片资源Base64编码,使用Promise异步回调。 3039 3040**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3041 3042**系统能力**:SystemCapability.Global.ResourceManager 3043 3044**模型约束**:此接口仅可在Stage模型下使用。 3045 3046**参数:** 3047 3048| 参数名 | 类型 | 必填 | 说明 | 3049| -------- | ---------------------- | ---- | ---- | 3050| resource | [Resource](#resource9) | 是 | 资源信息。 | 3051| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 3052 3053**返回值:** 3054 3055| 类型 | 说明 | 3056| --------------------- | ------------------------- | 3057| Promise<string> | resource对象对应的图片资源Base64编码。 | 3058 3059**错误码:** 3060 3061以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 3062 3063| 错误码ID | 错误信息 | 3064| -------- | ---------------------------------------- | 3065| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 3066| 9001001 | Invalid resource ID. | 3067| 9001002 | No matching resource is found based on the resource ID. | 3068 3069**示例:** 3070 ```ts 3071 import { resourceManager } from '@kit.LocalizationKit' 3072 import { BusinessError } from '@kit.BasicServicesKit'; 3073 3074 let resource: resourceManager.Resource = { 3075 bundleName: "com.example.myapplication", 3076 moduleName: "entry", 3077 id: $r('app.media.test').id 3078 }; 3079 try { 3080 this.context.resourceManager.getMediaContentBase64(resource, 120).then((value: string) => { 3081 let media = value; 3082 }).catch((error: BusinessError) => { 3083 console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); 3084 }); 3085 } catch (error) { 3086 let code = (error as BusinessError).code; 3087 let message = (error as BusinessError).message; 3088 console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 3089 } 3090 ``` 3091 3092### getMediaBase64ByName<sup>9+</sup> 3093 3094getMediaBase64ByName(resName: string, callback: _AsyncCallback<string>): void 3095 3096用户获取指定资源名称对应的图片资源Base64编码,使用callback异步回调。 3097 3098**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3099 3100**系统能力**:SystemCapability.Global.ResourceManager 3101 3102**参数:** 3103 3104| 参数名 | 类型 | 必填 | 说明 | 3105| -------- | --------------------------- | ---- | ------------------------ | 3106| resName | string | 是 | 资源名称。 | 3107| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 返回获取的图片资源Base64编码。 | 3108 3109**错误码:** 3110 3111以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3112 3113| 错误码ID | 错误信息 | 3114| -------- | ---------------------------------------- | 3115| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3116| 9001003 | Invalid resource name. | 3117| 9001004 | No matching resource is found based on the resource name. | 3118 3119**示例:** 3120 ```ts 3121 import { BusinessError } from '@kit.BasicServicesKit'; 3122 3123 try { 3124 this.context.resourceManager.getMediaBase64ByName("test", (error: BusinessError, value: string) => { 3125 if (error != null) { 3126 console.error("error is " + error); 3127 } else { 3128 let media = value; 3129 } 3130 }); 3131 } catch (error) { 3132 let code = (error as BusinessError).code; 3133 let message = (error as BusinessError).message; 3134 console.error(`callback getMediaBase64ByName failed, error code: ${code}, message: ${message}.`); 3135 } 3136 ``` 3137 3138### getMediaBase64ByName<sup>10+</sup> 3139 3140getMediaBase64ByName(resName: string, density: number, callback: _AsyncCallback<string>): void 3141 3142用户获取指定资源名称对应的指定屏幕密度图片资源Base64编码,使用callback异步回调。 3143 3144**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3145 3146**系统能力**:SystemCapability.Global.ResourceManager 3147 3148**参数:** 3149 3150| 参数名 | 类型 | 必填 | 说明 | 3151| -------- | --------------------------- | ---- | ------------------------ | 3152| resName | string | 是 | 资源名称。 | 3153| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 3154| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 返回获取的图片资源Base64编码。 | 3155 3156**错误码:** 3157 3158以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 3159 3160| 错误码ID | 错误信息 | 3161| -------- | ---------------------------------------- | 3162| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 3163| 9001003 | Invalid resource name. | 3164| 9001004 | No matching resource is found based on the resource name. | 3165 3166**示例:** 3167 ```ts 3168 import { BusinessError } from '@kit.BasicServicesKit'; 3169 3170 try { 3171 this.context.resourceManager.getMediaBase64ByName("test", 120, (error: BusinessError, value: string) => { 3172 if (error != null) { 3173 console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); 3174 } else { 3175 let media = value; 3176 } 3177 }); 3178 } catch (error) { 3179 let code = (error as BusinessError).code; 3180 let message = (error as BusinessError).message; 3181 console.error(`callback getMediaBase64ByName failed, error code: ${code}, message: ${message}.`); 3182 } 3183 ``` 3184 3185### getMediaBase64ByName<sup>9+</sup> 3186 3187getMediaBase64ByName(resName: string): Promise<string> 3188 3189用户获取指定资源名称对应的图片资源Base64编码,使用Promise异步回调。 3190 3191**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3192 3193**系统能力**:SystemCapability.Global.ResourceManager 3194 3195**参数:** 3196 3197| 参数名 | 类型 | 必填 | 说明 | 3198| ------- | ------ | ---- | ---- | 3199| resName | string | 是 | 资源名称。 | 3200 3201**返回值:** 3202 3203| 类型 | 说明 | 3204| --------------------- | ------------------- | 3205| Promise<string> | 资源名称对应的图片资源Base64编码。 | 3206 3207**错误码:** 3208 3209以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3210 3211| 错误码ID | 错误信息 | 3212| -------- | ---------------------------------------- | 3213| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3214| 9001003 | Invalid resource name. | 3215| 9001004 | No matching resource is found based on the resource name. | 3216 3217**示例:** 3218 ```ts 3219 import { BusinessError } from '@kit.BasicServicesKit'; 3220 3221 try { 3222 this.context.resourceManager.getMediaBase64ByName("test").then((value: string) => { 3223 let media = value; 3224 }).catch((error: BusinessError) => { 3225 console.error("getMediaBase64ByName promise error is " + error); 3226 }); 3227 } catch (error) { 3228 let code = (error as BusinessError).code; 3229 let message = (error as BusinessError).message; 3230 console.error(`promise getMediaBase64ByName failed, error code: ${code}, message: ${message}.`); 3231 } 3232 ``` 3233 3234### getMediaBase64ByName<sup>10+</sup> 3235 3236getMediaBase64ByName(resName: string, density: number): Promise<string> 3237 3238用户获取指定资源名称对应的指定屏幕密度图片资源Base64编码,使用Promise异步回调。 3239 3240**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3241 3242**系统能力**:SystemCapability.Global.ResourceManager 3243 3244**参数:** 3245 3246| 参数名 | 类型 | 必填 | 说明 | 3247| ------- | ------ | ---- | ---- | 3248| resName | string | 是 | 资源名称。 | 3249| [density](#screendensity) | number | 是 | 资源获取需要的屏幕密度,0表示默认屏幕密度。 | 3250 3251**返回值:** 3252 3253| 类型 | 说明 | 3254| --------------------- | ------------------- | 3255| Promise<string> | 资源名称对应的图片资源Base64编码 。| 3256 3257**错误码:** 3258 3259以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 3260 3261| 错误码ID | 错误信息 | 3262| -------- | ---------------------------------------- | 3263| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 3264| 9001003 | Invalid resource name. | 3265| 9001004 | No matching resource is found based on the resource name. | 3266 3267**示例:** 3268 ```ts 3269 import { BusinessError } from '@kit.BasicServicesKit'; 3270 3271 try { 3272 this.context.resourceManager.getMediaBase64ByName("test", 120).then((value: string) => { 3273 let media = value; 3274 }).catch((error: BusinessError) => { 3275 console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); 3276 }); 3277 } catch (error) { 3278 let code = (error as BusinessError).code; 3279 let message = (error as BusinessError).message; 3280 console.error(`promise getMediaBase64ByName failed, error code: ${code}, message: ${message}.`); 3281 } 3282 ``` 3283 3284### getDrawableDescriptor<sup>10+</sup> 3285 3286getDrawableDescriptor(resId: number, density?: number, type?: number): DrawableDescriptor 3287 3288用户获取指定资源ID对应的DrawableDescriptor对象,用于图标的显示,使用同步方式返回。 3289 3290**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3291 3292**系统能力**:SystemCapability.Global.ResourceManager 3293 3294**参数:** 3295 3296| 参数名 | 类型 | 必填 | 说明 | 3297| ----- | ------ | ---- | ----- | 3298| resId | number | 是 | 资源ID值。 | 3299| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 3300| type<sup>11+</sup> | number | 否 | 1表示用于取主题资源包中应用的分层图标资源,0或缺省表示取应用自身图标资源。 | 3301 3302**返回值:** 3303 3304| 类型 | 说明 | 3305| ------ | ---------- | 3306| [DrawableDescriptor](../apis-arkui/js-apis-arkui-drawableDescriptor.md#drawabledescriptor) | 资源ID值对应的DrawableDescriptor对象。| 3307 3308**错误码:** 3309 3310以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3311 3312| 错误码ID | 错误信息 | 3313| -------- | ---------------------------------------- | 3314| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 3315| 9001001 | Invalid resource ID. | 3316| 9001002 | No matching resource is found based on the resource ID. | 3317 3318**示例:** 3319 ```ts 3320 import { BusinessError } from '@kit.BasicServicesKit'; 3321 import { DrawableDescriptor } from '@kit.ArkUI'; 3322 3323 try { 3324 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id); 3325 } catch (error) { 3326 let code = (error as BusinessError).code; 3327 let message = (error as BusinessError).message; 3328 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 3329 } 3330 try { 3331 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id, 120); 3332 } catch (error) { 3333 let code = (error as BusinessError).code; 3334 let message = (error as BusinessError).message; 3335 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 3336 } 3337 try { 3338 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id, 0, 1); 3339 } catch (error) { 3340 let code = (error as BusinessError).code; 3341 let message = (error as BusinessError).message; 3342 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 3343 } 3344 ``` 3345 3346### getDrawableDescriptor<sup>10+</sup> 3347 3348getDrawableDescriptor(resource: Resource, density?: number, type?: number): DrawableDescriptor 3349 3350用户获取指定resource对应的DrawableDescriptor对象,用于图标的显示,使用同步方式返回。 3351 3352**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3353 3354**系统能力**:SystemCapability.Global.ResourceManager 3355 3356**模型约束**:此接口仅可在Stage模型下使用。 3357 3358**参数:** 3359 3360| 参数名 | 类型 | 必填 | 说明 | 3361| -------- | ---------------------- | ---- | ---- | 3362| resource | [Resource](#resource9) | 是 | 资源信息。 | 3363| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 3364| type<sup>11+</sup> | number | 否 | 1表示用于取主题资源包中应用的分层图标资源,0或缺省表示取应用自身图标资源。 | 3365 3366**返回值:** 3367 3368| 类型 | 说明 | 3369| ------- | ----------------- | 3370| [DrawableDescriptor](../apis-arkui/js-apis-arkui-drawableDescriptor.md#drawabledescriptor) | 资源ID值对应的DrawableDescriptor对象。 | 3371 3372**错误码:** 3373 3374以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3375 3376| 错误码ID | 错误信息 | 3377| -------- | ---------------------------------------- | 3378| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 3379| 9001001 | Invalid resource ID. | 3380| 9001002 | No matching resource is found based on the resource ID. | 3381 3382**示例:** 3383 ```ts 3384 import { resourceManager } from '@kit.LocalizationKit' 3385 import { BusinessError } from '@kit.BasicServicesKit'; 3386 import { DrawableDescriptor } from '@kit.ArkUI'; 3387 3388 let resource: resourceManager.Resource = { 3389 bundleName: "com.example.myapplication", 3390 moduleName: "entry", 3391 id: $r('app.media.icon').id 3392 }; 3393 try { 3394 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor(resource); 3395 } catch (error) { 3396 let code = (error as BusinessError).code; 3397 let message = (error as BusinessError).message; 3398 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 3399 } 3400 try { 3401 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor(resource, 120); 3402 } catch (error) { 3403 let code = (error as BusinessError).code; 3404 let message = (error as BusinessError).message; 3405 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 3406 } 3407 try { 3408 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptor(resource, 0, 1); 3409 } catch (error) { 3410 let code = (error as BusinessError).code; 3411 let message = (error as BusinessError).message; 3412 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 3413 } 3414 ``` 3415 3416### getDrawableDescriptorByName<sup>10+</sup> 3417 3418getDrawableDescriptorByName(resName: string, density?: number, type?: number): DrawableDescriptor 3419 3420用户获取指定资源名称对应的DrawableDescriptor对象,用于图标的显示,使用同步方式返回。 3421 3422**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3423 3424**系统能力**:SystemCapability.Global.ResourceManager 3425 3426**参数:** 3427 3428| 参数名 | 类型 | 必填 | 说明 | 3429| ------- | ------ | ---- | ---- | 3430| resName | string | 是 | 资源名称。 | 3431| [density](#screendensity) | number | 否 | 资源获取需要的屏幕密度,0或缺省表示默认屏幕密度。 | 3432| type<sup>11+</sup> | number | 否 | 1表示用于取主题资源包中应用的分层图标资源,0或缺省表示取应用自身图标资源。 | 3433 3434**返回值:** 3435 3436| 类型 | 说明 | 3437| ------ | --------- | 3438| [DrawableDescriptor](../apis-arkui/js-apis-arkui-drawableDescriptor.md#drawabledescriptor) | 资源ID值对应的DrawableDescriptor对象。 | 3439 3440**错误码:** 3441 3442以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3443 3444| 错误码ID | 错误信息 | 3445| -------- | ---------------------------------------- | 3446| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 3447| 9001003 | Invalid resource name. | 3448| 9001004 | No matching resource is found based on the resource name. | 3449 3450**示例:** 3451 ```ts 3452 import { BusinessError } from '@kit.BasicServicesKit'; 3453 import { DrawableDescriptor } from '@kit.ArkUI'; 3454 3455 try { 3456 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptorByName('icon'); 3457 } catch (error) { 3458 let code = (error as BusinessError).code; 3459 let message = (error as BusinessError).message; 3460 console.error(`getDrawableDescriptorByName failed, error code: ${code}, message: ${message}.`); 3461 } 3462 try { 3463 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptorByName('icon', 120); 3464 } catch (error) { 3465 let code = (error as BusinessError).code; 3466 let message = (error as BusinessError).message; 3467 console.error(`getDrawableDescriptorByName failed, error code: ${code}, message: ${message}.`); 3468 } 3469 try { 3470 let drawableDescriptor:DrawableDescriptor = this.context.resourceManager.getDrawableDescriptorByName('icon', 0, 1); 3471 } catch (error) { 3472 let code = (error as BusinessError).code; 3473 let message = (error as BusinessError).message; 3474 console.error(`getDrawableDescriptorByName failed, error code: ${code}, message: ${message}.`); 3475 } 3476 ``` 3477 3478### getBoolean<sup>9+</sup> 3479 3480getBoolean(resId: number): boolean 3481 3482使用同步方式,返回获取指定资源ID对应的布尔结果。 3483 3484**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3485 3486**系统能力**:SystemCapability.Global.ResourceManager 3487 3488**参数:** 3489 3490| 参数名 | 类型 | 必填 | 说明 | 3491| ----- | ------ | ---- | ----- | 3492| resId | number | 是 | 资源ID值。 | 3493 3494**返回值:** 3495 3496| 类型 | 说明 | 3497| ------- | ------------ | 3498| boolean | 资源ID值对应的布尔结果。 | 3499 3500**错误码:** 3501 3502以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3503 3504| 错误码ID | 错误信息 | 3505| -------- | ---------------------------------------- | 3506| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3507| 9001001 | Invalid resource ID. | 3508| 9001002 | No matching resource is found based on the resource ID. | 3509| 9001006 | The resource is referenced cyclically. | 3510 3511**示例:** 3512 ```ts 3513 import { BusinessError } from '@kit.BasicServicesKit'; 3514 3515 try { 3516 this.context.resourceManager.getBoolean($r('app.boolean.boolean_test').id); 3517 } catch (error) { 3518 let code = (error as BusinessError).code; 3519 let message = (error as BusinessError).message; 3520 console.error(`getBoolean failed, error code: ${code}, message: ${message}.`); 3521 } 3522 ``` 3523### getBoolean<sup>9+</sup> 3524 3525getBoolean(resource: Resource): boolean 3526 3527使用同步方式,返回获取指定resource对象对应的布尔结果。 3528 3529**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3530 3531**系统能力**:SystemCapability.Global.ResourceManager 3532 3533**模型约束**:此接口仅可在Stage模型下使用。 3534 3535**参数:** 3536 3537| 参数名 | 类型 | 必填 | 说明 | 3538| -------- | ---------------------- | ---- | ---- | 3539| resource | [Resource](#resource9) | 是 | 资源信息。 | 3540 3541**返回值:** 3542 3543| 类型 | 说明 | 3544| ------- | ----------------- | 3545| boolean | resource对象对应的布尔结果。 | 3546 3547**错误码:** 3548 3549以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3550 3551| 错误码ID | 错误信息 | 3552| -------- | ---------------------------------------- | 3553| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3554| 9001001 | Invalid resource ID. | 3555| 9001002 | No matching resource is found based on the resource ID. | 3556| 9001006 | The resource is referenced cyclically. | 3557 3558**示例:** 3559 ```ts 3560 import { resourceManager } from '@kit.LocalizationKit' 3561 import { BusinessError } from '@kit.BasicServicesKit'; 3562 3563 let resource: resourceManager.Resource = { 3564 bundleName: "com.example.myapplication", 3565 moduleName: "entry", 3566 id: $r('app.boolean.boolean_test').id 3567 }; 3568 try { 3569 this.context.resourceManager.getBoolean(resource); 3570 } catch (error) { 3571 let code = (error as BusinessError).code; 3572 let message = (error as BusinessError).message; 3573 console.error(`getBoolean failed, error code: ${code}, message: ${message}.`); 3574 } 3575 ``` 3576 3577### getBooleanByName<sup>9+</sup> 3578 3579getBooleanByName(resName: string): boolean 3580 3581使用同步方式,返回获取指定资源名称对应的布尔结果。 3582 3583**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3584 3585**系统能力**:SystemCapability.Global.ResourceManager 3586 3587**参数:** 3588 3589| 参数名 | 类型 | 必填 | 说明 | 3590| ------- | ------ | ---- | ---- | 3591| resName | string | 是 | 资源名称。 | 3592 3593**返回值:** 3594 3595| 类型 | 说明 | 3596| ------- | ----------- | 3597| boolean | 资源名称对应的布尔结果。 | 3598 3599**错误码:** 3600 3601以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3602 3603| 错误码ID | 错误信息 | 3604| -------- | ---------------------------------------- | 3605| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3606| 9001003 | Invalid resource name. | 3607| 9001004 | No matching resource is found based on the resource name. | 3608| 9001006 | The resource is referenced cyclically. | 3609 3610**示例:** 3611 ```ts 3612 import { BusinessError } from '@kit.BasicServicesKit'; 3613 3614 try { 3615 this.context.resourceManager.getBooleanByName("boolean_test"); 3616 } catch (error) { 3617 let code = (error as BusinessError).code; 3618 let message = (error as BusinessError).message; 3619 console.error(`getBooleanByName failed, error code: ${code}, message: ${message}.`); 3620 } 3621 ``` 3622 3623### getNumber<sup>9+</sup> 3624 3625getNumber(resId: number): number 3626 3627用户获取指定资源ID对应的integer数值或者float数值,使用同步方式返回。 3628 3629**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3630 3631**系统能力**:SystemCapability.Global.ResourceManager 3632 3633**参数:** 3634 3635| 参数名 | 类型 | 必填 | 说明 | 3636| ----- | ------ | ---- | ----- | 3637| resId | number | 是 | 资源ID值。 | 3638 3639**返回值:** 3640 3641| 类型 | 说明 | 3642| ------ | ---------- | 3643| number | 资源ID值对应的数值。integer对应的是原数值,float对应的是真实像素点值,具体参考示例代码。 | 3644 3645**错误码:** 3646 3647以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3648 3649| 错误码ID | 错误信息 | 3650| -------- | ---------------------------------------- | 3651| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3652| 9001001 | Invalid resource ID. | 3653| 9001002 | No matching resource is found based on the resource ID. | 3654| 9001006 | The resource is referenced cyclically. | 3655 3656**示例:** 3657 ```ts 3658 import { BusinessError } from '@kit.BasicServicesKit'; 3659 3660 try { 3661 this.context.resourceManager.getNumber($r('app.integer.integer_test').id); // integer对应返回的是原数值 3662 } catch (error) { 3663 let code = (error as BusinessError).code; 3664 let message = (error as BusinessError).message; 3665 console.error(`getNumber failed, error code: ${code}, message: ${message}.`); 3666 } 3667 3668 try { 3669 this.context.resourceManager.getNumber($r('app.float.float_test').id); // float对应返回的是真实像素点值 3670 } catch (error) { 3671 let code = (error as BusinessError).code; 3672 let message = (error as BusinessError).message; 3673 console.error(`getNumber failed, error code: ${code}, message: ${message}.`); 3674 } 3675 ``` 3676 3677### getNumber<sup>9+</sup> 3678 3679getNumber(resource: Resource): number 3680 3681用户获取指定resource对象对应的integer数值或者float数值,使用同步方式返回。 3682 3683> **说明** 3684> 3685> 使用接口获取单位为"vp"的float值时,通过resId和resource对象获取到的值不一致,resId获取的值是准确的。该问题正在优化改进。 3686 3687**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3688 3689**系统能力**:SystemCapability.Global.ResourceManager 3690 3691**模型约束**:此接口仅可在Stage模型下使用。 3692 3693**参数:** 3694 3695| 参数名 | 类型 | 必填 | 说明 | 3696| -------- | ---------------------- | ---- | ---- | 3697| resource | [Resource](#resource9) | 是 | 资源信息。 | 3698 3699**返回值:** 3700 3701| 类型 | 说明 | 3702| ------ | --------------- | 3703| number | 资源名称对应的数值。<br>integer对应的是原数值,float不带单位时对应的是原数值,带"vp","fp"单位时对应的是px值。 | 3704 3705**错误码:** 3706 3707以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3708 3709| 错误码ID | 错误信息 | 3710| -------- | ---------------------------------------- | 3711| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3712| 9001001 | Invalid resource ID. | 3713| 9001002 | No matching resource is found based on the resource ID. | 3714| 9001006 | The resource is referenced cyclically. | 3715 3716**示例:** 3717 ```ts 3718 import { resourceManager } from '@kit.LocalizationKit' 3719 import { BusinessError } from '@kit.BasicServicesKit'; 3720 3721 let resource: resourceManager.Resource = { 3722 bundleName: "com.example.myapplication", 3723 moduleName: "entry", 3724 id: $r('app.integer.integer_test').id 3725 }; 3726 try { 3727 this.context.resourceManager.getNumber(resource); 3728 } catch (error) { 3729 let code = (error as BusinessError).code; 3730 let message = (error as BusinessError).message; 3731 console.error(`getNumber failed, error code: ${code}, message: ${message}.`); 3732 } 3733 ``` 3734 3735### getNumberByName<sup>9+</sup> 3736 3737getNumberByName(resName: string): number 3738 3739用户获取指定资源名称对应的integer数值或者float数值,使用同步方式返回。 3740 3741**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3742 3743**系统能力**:SystemCapability.Global.ResourceManager 3744 3745**参数:** 3746 3747| 参数名 | 类型 | 必填 | 说明 | 3748| ------- | ------ | ---- | ---- | 3749| resName | string | 是 | 资源名称。 | 3750 3751**返回值:** 3752 3753| 类型 | 说明 | 3754| ------ | --------- | 3755| number | 资源名称对应的数值。<br>integer对应的是原数值,float不带单位时对应的是原数值,带"vp","fp"单位时对应的是px值。 | 3756 3757**错误码:** 3758 3759以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 3760 3761| 错误码ID | 错误信息 | 3762| -------- | ---------------------------------------- | 3763| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3764| 9001003 | Invalid resource name. | 3765| 9001004 | No matching resource is found based on the resource name. | 3766| 9001006 | The resource is referenced cyclically. | 3767 3768**示例:** 3769 ```ts 3770 import { BusinessError } from '@kit.BasicServicesKit'; 3771 3772 try { 3773 this.context.resourceManager.getNumberByName("integer_test"); 3774 } catch (error) { 3775 let code = (error as BusinessError).code; 3776 let message = (error as BusinessError).message; 3777 console.error(`getNumberByName failed, error code: ${code}, message: ${message}.`); 3778 } 3779 3780 try { 3781 this.context.resourceManager.getNumberByName("float_test"); 3782 } catch (error) { 3783 let code = (error as BusinessError).code; 3784 let message = (error as BusinessError).message; 3785 console.error(`getNumberByName failed, error code: ${code}, message: ${message}.`); 3786 } 3787 ``` 3788 3789### getColorSync<sup>10+</sup> 3790 3791getColorSync(resId: number) : number; 3792 3793用户获取指定资源ID对应的颜色值,使用同步方式返回。 3794 3795**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3796 3797**系统能力**:SystemCapability.Global.ResourceManager 3798 3799**参数:** 3800 3801| 参数名 | 类型 | 必填 | 说明 | 3802| ----- | ------ | ---- | ----- | 3803| resId | number | 是 | 资源ID值。 | 3804 3805**返回值:** 3806 3807| 类型 | 说明 | 3808| ------ | ----------- | 3809| number | 资源ID值对应的颜色值(十进制)。 | 3810 3811**错误码:** 3812 3813以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 3814 3815| 错误码ID | 错误信息 | 3816| -------- | ---------------------------------------- | 3817| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3818| 9001001 | Invalid resource ID. | 3819| 9001002 | No matching resource is found based on the resource ID. | 3820| 9001006 | The resource is referenced cyclically. | 3821 3822**示例:** 3823 ```ts 3824 import { BusinessError } from '@kit.BasicServicesKit'; 3825 3826 try { 3827 this.context.resourceManager.getColorSync($r('app.color.test').id); 3828 } catch (error) { 3829 let code = (error as BusinessError).code; 3830 let message = (error as BusinessError).message; 3831 console.error(`getColorSync failed, error code: ${code}, message: ${message}.`); 3832 } 3833 ``` 3834 3835### getColorSync<sup>10+</sup> 3836 3837getColorSync(resource: Resource): number 3838 3839用户获取指定resource对象对应的颜色值,使用同步方式返回。 3840 3841**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3842 3843**系统能力**:SystemCapability.Global.ResourceManager 3844 3845**模型约束**:此接口仅可在Stage模型下使用。 3846 3847**参数:** 3848 3849| 参数名 | 类型 | 必填 | 说明 | 3850| -------- | ---------------------- | ---- | ---- | 3851| resource | [Resource](#resource9) | 是 | 资源信息。 | 3852 3853**返回值:** 3854 3855| 类型 | 说明 | 3856| ------ | ---------------- | 3857| number | resource对象对应的颜色值(十进制)。 | 3858 3859**错误码:** 3860 3861以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 3862 3863| 错误码ID | 错误信息 | 3864| -------- | ---------------------------------------- | 3865| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3866| 9001001 | Invalid resource ID. | 3867| 9001002 | No matching resource is found based on the resource ID. | 3868| 9001006 | The resource is referenced cyclically. | 3869 3870**示例:** 3871 ```ts 3872 import { resourceManager } from '@kit.LocalizationKit' 3873 import { BusinessError } from '@kit.BasicServicesKit'; 3874 3875 let resource: resourceManager.Resource = { 3876 bundleName: "com.example.myapplication", 3877 moduleName: "entry", 3878 id: $r('app.color.test').id 3879 }; 3880 try { 3881 this.context.resourceManager.getColorSync(resource); 3882 } catch (error) { 3883 let code = (error as BusinessError).code; 3884 let message = (error as BusinessError).message; 3885 console.error(`getColorSync failed, error code: ${code}, message: ${message}.`); 3886 } 3887 ``` 3888 3889### getColorByNameSync<sup>10+</sup> 3890 3891getColorByNameSync(resName: string) : number; 3892 3893用户获取指定资源名称对应的颜色值,使用同步方式返回。 3894 3895**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3896 3897**系统能力**:SystemCapability.Global.ResourceManager 3898 3899**参数:** 3900 3901| 参数名 | 类型 | 必填 | 说明 | 3902| ------- | ------ | ---- | ---- | 3903| resName | string | 是 | 资源名称。 | 3904 3905**返回值:** 3906 3907| 类型 | 说明 | 3908| ------ | ---------- | 3909| number | 资源名称对应的颜色值(十进制)。 | 3910 3911**错误码:** 3912 3913以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 3914 3915| 错误码ID | 错误信息 | 3916| -------- | ---------------------------------------- | 3917| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3918| 9001003 | Invalid resource name. | 3919| 9001004 | No matching resource is found based on the resource name. | 3920| 9001006 | The resource is referenced cyclically. | 3921 3922**示例:** 3923 ```ts 3924 import { BusinessError } from '@kit.BasicServicesKit'; 3925 3926 try { 3927 this.context.resourceManager.getColorByNameSync("test"); 3928 } catch (error) { 3929 let code = (error as BusinessError).code; 3930 let message = (error as BusinessError).message; 3931 console.error(`getColorByNameSync failed, error code: ${code}, message: ${message}.`); 3932 } 3933 ``` 3934 3935### getColor<sup>10+</sup> 3936 3937getColor(resId: number, callback: _AsyncCallback<number>): void; 3938 3939用户获取指定资源ID对应的颜色值,使用callback异步回调。 3940 3941**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3942 3943**系统能力:** SystemCapability.Global.ResourceManager 3944 3945**参数:** 3946 3947| 参数名 | 类型 | 必填 | 说明 | 3948| -------- | --------------------------- | ---- | --------------- | 3949| resId | number | 是 | 资源ID值。 | 3950| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<number> | 是 | 返回获取的颜色值(十进制)。 | 3951 3952**错误码:** 3953 3954以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 3955 3956| 错误码ID | 错误信息 | 3957| -------- | ---------------------------------------- | 3958| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 3959| 9001001 | If the module resId invalid. | 3960| 9001002 | No matching resource is found based on the resource ID. | 3961| 9001006 | The resource is referenced cyclically. | 3962 3963**示例Stage:** 3964 ```ts 3965 import { BusinessError } from '@kit.BasicServicesKit'; 3966 3967 try { 3968 this.context.resourceManager.getColor($r('app.color.test').id, (error: BusinessError, value: number) => { 3969 if (error != null) { 3970 console.error("error is " + error); 3971 } else { 3972 let str = value; 3973 } 3974 }); 3975 } catch (error) { 3976 let code = (error as BusinessError).code; 3977 let message = (error as BusinessError).message; 3978 console.error(`callback getColor failed, error code: ${code}, message: ${message}.`); 3979 } 3980 ``` 3981 3982### getColor<sup>10+</sup> 3983 3984getColor(resId: number): Promise<number> 3985 3986用户获取指定资源ID对应的颜色值,使用Promise异步回调。 3987 3988**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3989 3990**系统能力**:SystemCapability.Global.ResourceManager 3991 3992**参数:** 3993 3994| 参数名 | 类型 | 必填 | 说明 | 3995| ----- | ------ | ---- | ----- | 3996| resId | number | 是 | 资源ID值。 | 3997 3998**返回值:** 3999 4000| 类型 | 说明 | 4001| --------------------- | ----------- | 4002| Promise<number> | 资源ID值对应的颜色值(十进制)。 | 4003 4004**错误码:** 4005 4006以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 4007 4008| 错误码ID | 错误信息 | 4009| -------- | ---------------------------------------- | 4010| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4011| 9001001 | Invalid resource ID. | 4012| 9001002 | No matching resource is found based on the resource ID. | 4013| 9001006 | The resource is referenced cyclically. | 4014 4015**示例:** 4016 ```ts 4017 import { BusinessError } from '@kit.BasicServicesKit'; 4018 4019 try { 4020 this.context.resourceManager.getColor($r('app.color.test').id).then((value: number) => { 4021 let str = value; 4022 }).catch((error: BusinessError) => { 4023 console.error("getColor promise error is " + error); 4024 }); 4025 } catch (error) { 4026 let code = (error as BusinessError).code; 4027 let message = (error as BusinessError).message; 4028 console.error(`promise getColor failed, error code: ${code}, message: ${message}.`); 4029 } 4030 ``` 4031 4032### getColor<sup>10+</sup> 4033 4034getColor(resource: Resource, callback: _AsyncCallback<number>): void; 4035 4036用户获取指定resource对象对应的颜色值,使用callback异步回调。 4037 4038**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4039 4040**系统能力:** SystemCapability.Global.ResourceManager 4041 4042**模型约束**:此接口仅可在Stage模型下使用。 4043 4044**参数:** 4045 4046| 参数名 | 类型 | 必填 | 说明 | 4047| -------- | --------------------------- | ---- | --------------- | 4048| resource | [Resource](#resource9) | 是 | 资源信息。 | 4049| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<number> | 是 | 返回获取的颜色值(十进制)。 | 4050 4051**错误码:** 4052 4053以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 4054 4055| 错误码ID | 错误信息 | 4056| -------- | ---------------------------------------- | 4057| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4058| 9001001 | Invalid resource ID. | 4059| 9001002 | No matching resource is found based on the resource ID. | 4060| 9001006 | The resource is referenced cyclically. | 4061 4062**示例:** 4063 ```ts 4064 import { resourceManager } from '@kit.LocalizationKit' 4065 import { BusinessError } from '@kit.BasicServicesKit'; 4066 4067 let resource: resourceManager.Resource = { 4068 bundleName: "com.example.myapplication", 4069 moduleName: "entry", 4070 id: $r('app.color.test').id 4071 }; 4072 try { 4073 this.context.resourceManager.getColor(resource, (error: BusinessError, value: number) => { 4074 if (error != null) { 4075 console.error("error is " + error); 4076 } else { 4077 let str = value; 4078 } 4079 }); 4080 } catch (error) { 4081 let code = (error as BusinessError).code; 4082 let message = (error as BusinessError).message; 4083 console.error(`callback getColor failed, error code: ${code}, message: ${message}.`); 4084 } 4085 ``` 4086 4087### getColor<sup>10+</sup> 4088 4089getColor(resource: Resource): Promise<number>; 4090 4091用户获取指定resource对象对应的颜色值,使用Promise异步回调。 4092 4093**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4094 4095**系统能力**:SystemCapability.Global.ResourceManager 4096 4097**模型约束**:此接口仅可在Stage模型下使用。 4098 4099**参数:** 4100 4101| 参数名 | 类型 | 必填 | 说明 | 4102| -------- | ---------------------- | ---- | ---- | 4103| resource | [Resource](#resource9) | 是 | 资源信息。 | 4104 4105**返回值:** 4106 4107| 类型 | 说明 | 4108| --------------------- | ---------------- | 4109| Promise<number> | resource对象对应的颜色值(十进制)。 | 4110 4111**错误码:** 4112 4113以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 4114 4115| 错误码ID | 错误信息 | 4116| -------- | ---------------------------------------- | 4117| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4118| 9001001 | Invalid resource ID. | 4119| 9001002 | No matching resource is found based on the resource ID. | 4120| 9001006 | The resource is referenced cyclically. | 4121 4122**示例:** 4123 ```ts 4124 import { resourceManager } from '@kit.LocalizationKit' 4125 import { BusinessError } from '@kit.BasicServicesKit'; 4126 4127 let resource: resourceManager.Resource = { 4128 bundleName: "com.example.myapplication", 4129 moduleName: "entry", 4130 id: $r('app.color.test').id 4131 }; 4132 try { 4133 this.context.resourceManager.getColor(resource).then((value: number) => { 4134 let str = value; 4135 }).catch((error: BusinessError) => { 4136 console.error("getColor promise error is " + error); 4137 }); 4138 } catch (error) { 4139 let code = (error as BusinessError).code; 4140 let message = (error as BusinessError).message; 4141 console.error(`promise getColor failed, error code: ${code}, message: ${message}.`); 4142 } 4143 ``` 4144 4145### getColorByName<sup>10+</sup> 4146 4147getColorByName(resName: string, callback: _AsyncCallback<number>): void 4148 4149用户获取指定资源名称对应的颜色值,使用callback异步回调。 4150 4151**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4152 4153**系统能力**:SystemCapability.Global.ResourceManager 4154 4155**参数:** 4156 4157| 参数名 | 类型 | 必填 | 说明 | 4158| -------- | --------------------------- | ---- | --------------- | 4159| resName | string | 是 | 资源名称。 | 4160| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<number> | 是 | 异步回调,用于返回获取的颜色值(十进制)。 | 4161 4162**错误码:** 4163 4164以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 4165 4166| 错误码ID | 错误信息 | 4167| -------- | ---------------------------------------- | 4168| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4169| 9001003 | Invalid resource name. | 4170| 9001004 | No matching resource is found based on the resource name. | 4171| 9001006 | The resource is referenced cyclically. | 4172 4173**示例:** 4174 ```ts 4175 import { BusinessError } from '@kit.BasicServicesKit'; 4176 4177 try { 4178 this.context.resourceManager.getColorByName("test", (error: BusinessError, value: number) => { 4179 if (error != null) { 4180 console.error("error is " + error); 4181 } else { 4182 let string = value; 4183 } 4184 }); 4185 } catch (error) { 4186 let code = (error as BusinessError).code; 4187 let message = (error as BusinessError).message; 4188 console.error(`callback getColorByName failed, error code: ${code}, message: ${message}.`); 4189 } 4190 ``` 4191 4192### getColorByName<sup>10+</sup> 4193 4194getColorByName(resName: string): Promise<number> 4195 4196用户获取指定资源名称对应的颜色值,使用Promise异步回调。 4197 4198**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4199 4200**系统能力**:SystemCapability.Global.ResourceManager 4201 4202**参数:** 4203 4204| 参数名 | 类型 | 必填 | 说明 | 4205| ------- | ------ | ---- | ---- | 4206| resName | string | 是 | 资源名称。 | 4207 4208**返回值:** 4209 4210| 类型 | 说明 | 4211| --------------------- | ---------- | 4212| Promise<number> | 资源名称对应的颜色值(十进制)。 | 4213 4214**错误码:** 4215 4216以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 4217 4218| 错误码ID | 错误信息 | 4219| -------- | ---------------------------------------- | 4220| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4221| 9001003 | Invalid resource name. | 4222| 9001004 | No matching resource is found based on the resource name. | 4223| 9001006 | The resource is referenced cyclically. | 4224 4225**示例:** 4226 ```ts 4227 import { BusinessError } from '@kit.BasicServicesKit'; 4228 4229 try { 4230 this.context.resourceManager.getColorByName("test").then((value: number) => { 4231 let string = value; 4232 }).catch((error: BusinessError) => { 4233 console.error("getColorByName promise error is " + error); 4234 }); 4235 } catch (error) { 4236 let code = (error as BusinessError).code; 4237 let message = (error as BusinessError).message; 4238 console.error(`promise getColorByName failed, error code: ${code}, message: ${message}.`); 4239 } 4240 ``` 4241 4242### getRawFileContentSync<sup>10+</sup> 4243 4244getRawFileContentSync(path: string): Uint8Array 4245 4246用户获取resources/rawfile目录下对应的rawfile文件内容,使用同步形式返回。 4247 4248**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4249 4250**系统能力:** SystemCapability.Global.ResourceManager 4251 4252**参数:** 4253 4254| 参数名 | 类型 | 必填 | 说明 | 4255| -------- | ------------------------------- | ---- | ----------------------- | 4256| path | string | 是 | rawfile文件路径。 | 4257 4258**返回值:** 4259 4260| 类型 | 说明 | 4261| --------------------- | ---------- | 4262| Uint8Array | 返回获取的rawfile文件内容。 | 4263 4264**错误码:** 4265 4266以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 4267 4268| 错误码ID | 错误信息 | 4269| -------- | ---------------------------------------- | 4270| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4271| 9001005 | Invalid relative path. | 4272 4273**示例:** 4274 ```ts 4275 import { BusinessError } from '@kit.BasicServicesKit'; 4276 4277 try { 4278 this.context.resourceManager.getRawFileContentSync("test.txt"); 4279 } catch (error) { 4280 let code = (error as BusinessError).code; 4281 let message = (error as BusinessError).message; 4282 console.error(`getRawFileContentSync failed, error code: ${code}, message: ${message}.`); 4283 } 4284 ``` 4285 4286### getRawFileContent<sup>9+</sup> 4287 4288getRawFileContent(path: string, callback: _AsyncCallback<Uint8Array>): void 4289 4290用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback异步回调。 4291 4292**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4293 4294**系统能力**:SystemCapability.Global.ResourceManager 4295 4296**参数:** 4297 4298| 参数名 | 类型 | 必填 | 说明 | 4299| -------- | ------------------------------- | ---- | ----------------------- | 4300| path | string | 是 | rawfile文件路径。 | 4301| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Uint8Array> | 是 | 返回获取的rawfile文件内容。 | 4302 4303**错误码:** 4304 4305以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 4306 4307| 错误码ID | 错误信息 | 4308| -------- | ---------------------------------------- | 4309| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4310 4311**示例:** 4312 ```ts 4313 import { BusinessError } from '@kit.BasicServicesKit'; 4314 4315 try { 4316 this.context.resourceManager.getRawFileContent("test.txt", (error: BusinessError, value: Uint8Array) => { 4317 if (error != null) { 4318 console.error("error is " + error); 4319 } else { 4320 let rawFile = value; 4321 } 4322 }); 4323 } catch (error) { 4324 let code = (error as BusinessError).code; 4325 let message = (error as BusinessError).message; 4326 console.error(`callback getRawFileContent failed, error code: ${code}, message: ${message}.`); 4327 } 4328 ``` 4329 4330### getRawFileContent<sup>9+</sup> 4331 4332getRawFileContent(path: string): Promise<Uint8Array> 4333 4334用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise异步回调。 4335 4336**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4337 4338**系统能力**:SystemCapability.Global.ResourceManager 4339 4340**参数:** 4341 4342| 参数名 | 类型 | 必填 | 说明 | 4343| ---- | ------ | ---- | ----------- | 4344| path | string | 是 | rawfile文件路径。 | 4345 4346**返回值:** 4347 4348| 类型 | 说明 | 4349| ------------------------- | ----------- | 4350| Promise<Uint8Array> | rawfile文件内容。 | 4351 4352**错误码:** 4353 4354以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 4355 4356| 错误码ID | 错误信息 | 4357| -------- | ---------------------------------------- | 4358| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4359| 9001005 | Invalid relative path. | 4360 4361**示例:** 4362 ```ts 4363 import { BusinessError } from '@kit.BasicServicesKit'; 4364 4365 try { 4366 this.context.resourceManager.getRawFileContent("test.txt").then((value: Uint8Array) => { 4367 let rawFile = value; 4368 }).catch((error: BusinessError) => { 4369 console.error("getRawFileContent promise error is " + error); 4370 }); 4371 } catch (error) { 4372 let code = (error as BusinessError).code; 4373 let message = (error as BusinessError).message; 4374 console.error(`promise getRawFileContent failed, error code: ${code}, message: ${message}.`); 4375 } 4376 ``` 4377 4378### getRawFileListSync<sup>10+</sup> 4379 4380getRawFileListSync(path: string): Array\<string> 4381 4382用户获取resources/rawfile目录下文件夹及文件列表,使用同步形式返回。 4383 4384>**说明** 4385> 4386> 若文件夹中无文件,则不返回;若文件夹中有文件,则返回文件夹及文件列表。 4387 4388**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4389 4390**系统能力:** SystemCapability.Global.ResourceManager 4391 4392**参数:** 4393 4394| 参数名 | 类型 | 必填 | 说明 | 4395| -------- | ------------------------------- | ---- | ----------------------- | 4396| path | string | 是 | rawfile文件夹路径。 | 4397 4398**返回值:** 4399 4400| 类型 | 说明 | 4401| ------------------------- | ----------- | 4402| Array\<string> | rawfile文件目录下的文件夹及文件列表。 | 4403 4404**错误码:** 4405 4406以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 4407 4408| 错误码ID | 错误信息 | 4409| -------- | ---------------------------------------- | 4410| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4411| 9001005 | Invalid relative path. | 4412 4413**示例:** 4414 ```ts 4415 import { BusinessError } from '@kit.BasicServicesKit'; 4416 4417 try { // 传入""表示获取rawfile根目录下的文件列表 4418 this.context.resourceManager.getRawFileListSync("") 4419 } catch (error) { 4420 let code = (error as BusinessError).code; 4421 let message = (error as BusinessError).message; 4422 console.error(`getRawFileListSync failed, error code: ${code}, message: ${message}.`); 4423 } 4424 ``` 4425 4426### getRawFileList<sup>10+</sup> 4427 4428getRawFileList(path: string, callback: _AsyncCallback<Array\<string\>>): void; 4429 4430用户获取resources/rawfile目录下文件夹及文件列表,使用callback异步回调。 4431 4432>**说明** 4433> 4434> 若文件夹中无文件,则不返回;若文件夹中有文件,则返回文件夹及文件列表。 4435 4436**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4437 4438**系统能力**:SystemCapability.Global.ResourceManager 4439 4440**参数:** 4441 4442| 参数名 | 类型 | 必填 | 说明 | 4443| -------- | ------------------------------- | ---- | ----------------------- | 4444| path | string | 是 | rawfile文件夹路径。 | 4445| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<Array\<string\>> | 是 | rawfile文件目录下的文件夹及文件列表。 | 4446 4447**错误码:** 4448 4449以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 4450 4451| 错误码ID | 错误信息 | 4452| -------- | ---------------------------------------- | 4453| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4454| 9001005 | Invalid relative path. | 4455 4456**示例:** 4457 ```ts 4458 import { BusinessError } from '@kit.BasicServicesKit'; 4459 4460 try { // 传入""表示获取rawfile根目录下的文件列表 4461 this.context.resourceManager.getRawFileList("", (error: BusinessError, value: Array<string>) => { 4462 if (error != null) { 4463 console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); 4464 } else { 4465 let rawFile = value; 4466 } 4467 }); 4468 } catch (error) { 4469 let code = (error as BusinessError).code; 4470 let message = (error as BusinessError).message; 4471 console.error(`callback getRawFileList failed, error code: ${code}, message: ${message}.`); 4472 } 4473 ``` 4474 4475### getRawFileList<sup>10+</sup> 4476 4477getRawFileList(path: string): Promise<Array\<string\>> 4478 4479用户获取resources/rawfile目录下文件夹及文件列表,使用Promise异步回调。 4480 4481>**说明** 4482> 4483> 若文件夹中无文件,则不返回;若文件夹中有文件,则返回文件夹及文件列表。 4484 4485**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4486 4487**系统能力**:SystemCapability.Global.ResourceManager 4488 4489**参数:** 4490 4491| 参数名 | 类型 | 必填 | 说明 | 4492| ---- | ------ | ---- | ----------- | 4493| path | string | 是 | rawfile文件夹路径。 | 4494 4495**返回值:** 4496 4497| 类型 | 说明 | 4498| ------------------------- | ----------- | 4499| Promise<Array\<string\>> | rawfile文件目录下的文件夹及文件列表。 | 4500 4501**错误码:** 4502 4503以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 4504 4505| 错误码ID | 错误信息 | 4506| -------- | ---------------------------------------- | 4507| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4508| 9001005 | Invalid relative path. | 4509 4510**示例:** 4511 ```ts 4512 import { BusinessError } from '@kit.BasicServicesKit'; 4513 4514 try { // 传入""表示获取rawfile根目录下的文件列表 4515 this.context.resourceManager.getRawFileList("").then((value: Array<string>) => { 4516 let rawFile = value; 4517 }).catch((error: BusinessError) => { 4518 console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); 4519 }); 4520 } catch (error) { 4521 let code = (error as BusinessError).code; 4522 let message = (error as BusinessError).message; 4523 console.error(`promise getRawFileList failed, error code: ${code}, message: ${message}.`); 4524 } 4525 ``` 4526 4527### getRawFdSync<sup>10+</sup> 4528 4529getRawFdSync(path: string): RawFileDescriptor 4530 4531用户获取resources/rawfile目录下rawfile文件所在hap的descriptor信息。 4532 4533**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4534 4535**系统能力:** SystemCapability.Global.ResourceManager 4536 4537**参数:** 4538 4539| 参数名 | 类型 | 必填 | 说明 | 4540| -------- | ---------------------------------------- | ---- | -------------------------------- | 4541| path | string | 是 | rawfile文件路径。 | 4542 4543**返回值:** 4544 4545| 类型 | 说明 | 4546| ------------------------- | ----------- | 4547| [RawFileDescriptor](#rawfiledescriptor9) | rawfile文件所在hap的descriptor信息。 | 4548 4549**错误码:** 4550 4551以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 4552 4553| 错误码ID | 错误信息 | 4554| -------- | ---------------------------------------- | 4555| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4556| 9001005 | Invalid relative path. | 4557 4558**示例:** 4559 ```ts 4560 import { BusinessError } from '@kit.BasicServicesKit'; 4561 4562 try { 4563 this.context.resourceManager.getRawFdSync("test.txt"); 4564 } catch (error) { 4565 let code = (error as BusinessError).code; 4566 let message = (error as BusinessError).message; 4567 console.error(`getRawFdSync failed, error code: ${code}, message: ${message}.`); 4568 } 4569 ``` 4570 4571### getRawFd<sup>9+</sup> 4572 4573getRawFd(path: string, callback: _AsyncCallback<RawFileDescriptor>): void 4574 4575用户获取resources/rawfile目录下对应rawfile文件所在hap的descriptor信息,使用callback异步回调。 4576 4577**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4578 4579**系统能力**:SystemCapability.Global.ResourceManager 4580 4581**参数:** 4582 4583| 参数名 | 类型 | 必填 | 说明 | 4584| -------- | ---------------------------------------- | ---- | -------------------------------- | 4585| path | string | 是 | rawfile文件路径。 | 4586| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<[RawFileDescriptor](#rawfiledescriptor9)> | 是 | 返回获取的rawfile文件所在hap的descriptor信息。 | 4587 4588**错误码:** 4589 4590以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 4591 4592| 错误码ID | 错误信息 | 4593| -------- | ---------------------------------------- | 4594| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4595| 9001005 | Invalid relative path. | 4596 4597**示例:** 4598 ```ts 4599 import { BusinessError } from '@kit.BasicServicesKit'; 4600 import { resourceManager } from '@kit.LocalizationKit' 4601 4602 try { 4603 this.context.resourceManager.getRawFd("test.txt", (error: BusinessError, value: resourceManager.RawFileDescriptor) => { 4604 if (error != null) { 4605 console.error(`callback getRawFd failed error code: ${error.code}, message: ${error.message}.`); 4606 } else { 4607 let fd = value.fd; 4608 let offset = value.offset; 4609 let length = value.length; 4610 } 4611 }); 4612 } catch (error) { 4613 let code = (error as BusinessError).code; 4614 let message = (error as BusinessError).message; 4615 console.error(`callback getRawFd failed, error code: ${code}, message: ${message}.`); 4616 } 4617 ``` 4618 4619### getRawFd<sup>9+</sup> 4620 4621getRawFd(path: string): Promise<RawFileDescriptor> 4622 4623用户获取resources/rawfile目录下rawfile文件所在hap的descriptor信息,使用Promise异步回调。 4624 4625**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4626 4627**系统能力**:SystemCapability.Global.ResourceManager 4628 4629**参数:** 4630 4631| 参数名 | 类型 | 必填 | 说明 | 4632| ---- | ------ | ---- | ----------- | 4633| path | string | 是 | rawfile文件路径。 | 4634 4635**返回值:** 4636 4637| 类型 | 说明 | 4638| ---------------------------------------- | ------------------- | 4639| Promise<[RawFileDescriptor](#rawfiledescriptor9)> | rawfile文件所在hap的descriptor信息。 | 4640 4641**错误码:** 4642 4643以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 4644 4645| 错误码ID | 错误信息 | 4646| -------- | ---------------------------------------- | 4647| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4648| 9001005 | Invalid relative path. | 4649 4650**示例:** 4651 ```ts 4652 import { BusinessError } from '@kit.BasicServicesKit'; 4653 import { resourceManager } from '@kit.LocalizationKit' 4654 4655 try { 4656 this.context.resourceManager.getRawFd("test.txt").then((value: resourceManager.RawFileDescriptor) => { 4657 let fd = value.fd; 4658 let offset = value.offset; 4659 let length = value.length; 4660 }).catch((error: BusinessError) => { 4661 console.error(`promise getRawFd error error code: ${error.code}, message: ${error.message}.`); 4662 }); 4663 } catch (error) { 4664 let code = (error as BusinessError).code; 4665 let message = (error as BusinessError).message; 4666 console.error(`promise getRawFd failed, error code: ${code}, message: ${message}.`); 4667 } 4668 ``` 4669 4670### closeRawFdSync<sup>10+</sup> 4671 4672closeRawFdSync(path: string): void 4673 4674用户关闭resources/rawfile目录下rawfile文件所在hap的descriptor信息。 4675 4676**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4677 4678**系统能力**:SystemCapability.Global.ResourceManager 4679 4680**参数:** 4681 4682| 参数名 | 类型 | 必填 | 说明 | 4683| -------- | ------------------------- | ---- | ----------- | 4684| path | string | 是 | rawfile文件路径 。| 4685 4686**错误码:** 4687 4688以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 4689 4690| 错误码ID | 错误信息 | 4691| -------- | ---------------------------------------- | 4692| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4693| 9001005 | The resource not found by path. | 4694 4695**示例:** 4696 ```ts 4697 import { BusinessError } from '@kit.BasicServicesKit'; 4698 4699 try { 4700 this.context.resourceManager.closeRawFdSync("test.txt"); 4701 } catch (error) { 4702 let code = (error as BusinessError).code; 4703 let message = (error as BusinessError).message; 4704 console.error(`closeRawFd failed, error code: ${code}, message: ${message}.`); 4705 } 4706 ``` 4707 4708### closeRawFd<sup>9+</sup> 4709 4710closeRawFd(path: string, callback: _AsyncCallback<void>): void 4711 4712用户关闭resources/rawfile目录下rawfile文件所在hap的descriptor信息,使用callback异步回调。 4713 4714**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4715 4716**系统能力**:SystemCapability.Global.ResourceManager 4717 4718**参数:** 4719 4720| 参数名 | 类型 | 必填 | 说明 | 4721| -------- | ------------------------- | ---- | ----------- | 4722| path | string | 是 | rawfile文件路径。 | 4723| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<void> | 是 | 异步回调。 | 4724 4725**错误码:** 4726 4727以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 4728 4729| 错误码ID | 错误信息 | 4730| -------- | ---------------------------------------- | 4731| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4732| 9001005 | The resource not found by path. | 4733 4734**示例:** 4735 ```ts 4736 import { BusinessError } from '@kit.BasicServicesKit'; 4737 4738 try { 4739 this.context.resourceManager.closeRawFd("test.txt", (error: BusinessError) => { 4740 if (error != null) { 4741 console.error("error is " + error); 4742 } 4743 }); 4744 } catch (error) { 4745 let code = (error as BusinessError).code; 4746 let message = (error as BusinessError).message; 4747 console.error(`callback closeRawFd failed, error code: ${code}, message: ${message}.`); 4748 } 4749 ``` 4750 4751### closeRawFd<sup>9+</sup> 4752 4753closeRawFd(path: string): Promise<void> 4754 4755用户关闭resources/rawfile目录下rawfile文件所在hap的descriptor信息,使用Promise异步回调。 4756 4757**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4758 4759**系统能力**:SystemCapability.Global.ResourceManager 4760 4761**参数:** 4762 4763| 参数名 | 类型 | 必填 | 说明 | 4764| ---- | ------ | ---- | ----------- | 4765| path | string | 是 | rawfile文件路径。 | 4766 4767**返回值:** 4768 4769| 类型 | 说明 | 4770| ------------------- | ---- | 4771| Promise<void> | 无返回结果的promise对象。 | 4772 4773**错误码:** 4774 4775以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 4776 4777| 错误码ID | 错误信息 | 4778| -------- | ---------------------------------------- | 4779| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 4780| 9001005 | Invalid relative path. | 4781 4782**示例:** 4783 ```ts 4784 import { BusinessError } from '@kit.BasicServicesKit'; 4785 4786 try { 4787 this.context.resourceManager.closeRawFd("test.txt"); 4788 } catch (error) { 4789 let code = (error as BusinessError).code; 4790 let message = (error as BusinessError).message; 4791 console.error(`promise closeRawFd failed, error code: ${code}, message: ${message}.`); 4792 } 4793 ``` 4794 4795### getConfigurationSync<sup>10+</sup> 4796 4797getConfigurationSync(): Configuration 4798 4799用户获取设备的Configuration,使用同步形式返回。 4800 4801**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4802 4803**系统能力**:SystemCapability.Global.ResourceManager 4804 4805**返回值:** 4806 4807| 类型 | 说明 | 4808| ---------------------------------------- | ---------------- | 4809| [Configuration](#configuration) | 设备的Configuration。 | 4810 4811**示例:** 4812 ```ts 4813 try { 4814 let value = this.context.resourceManager.getConfigurationSync(); 4815 let direction = value.direction; 4816 let locale = value.locale; 4817 } catch (error) { 4818 console.error("getConfigurationSync error is " + error); 4819 } 4820 ``` 4821 4822### getConfiguration 4823 4824getConfiguration(callback: _AsyncCallback<Configuration>): void 4825 4826用户获取设备的Configuration,使用callback异步回调。 4827 4828**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4829 4830**系统能力**:SystemCapability.Global.ResourceManager 4831 4832**参数:** 4833 4834| 参数名 | 类型 | 必填 | 说明 | 4835| -------- | ---------------------------------------- | ---- | ------------------------- | 4836| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<[Configuration](#configuration)> | 是 | 返回设备的Configuration。 | 4837 4838**示例:** 4839 ```ts 4840 import { resourceManager } from '@kit.LocalizationKit' 4841 4842 try { 4843 this.context.resourceManager.getConfiguration((error: BusinessError, value: resourceManager.Configuration) => { 4844 if (error != null) { 4845 console.error("getConfiguration callback error is " + error); 4846 } else { 4847 let direction = value.direction; 4848 let locale = value.locale; 4849 } 4850 }); 4851 } catch (error) { 4852 console.error("getConfiguration callback error is " + error); 4853 } 4854 ``` 4855 4856### getConfiguration 4857 4858getConfiguration(): Promise<Configuration> 4859 4860用户获取设备的Configuration,使用Promise异步回调。 4861 4862**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4863 4864**系统能力**:SystemCapability.Global.ResourceManager 4865 4866**返回值:** 4867 4868| 类型 | 说明 | 4869| ---------------------------------------- | ---------------- | 4870| Promise<[Configuration](#configuration)> | 设备的Configuration。 | 4871 4872**示例:** 4873 ```ts 4874 import { BusinessError } from '@kit.BasicServicesKit'; 4875 import { resourceManager } from '@kit.LocalizationKit' 4876 4877 try { 4878 this.context.resourceManager.getConfiguration().then((value: resourceManager.Configuration) => { 4879 let direction = value.direction; 4880 let locale = value.locale; 4881 }).catch((error: BusinessError) => { 4882 console.error("getConfiguration promise error is " + error); 4883 }); 4884 } catch (error) { 4885 console.error("getConfiguration promise error is " + error); 4886 } 4887 ``` 4888 4889### getDeviceCapabilitySync<sup>10+</sup> 4890 4891getDeviceCapabilitySync(): DeviceCapability 4892 4893用户获取设备的DeviceCapability,使用同步形式返回。 4894 4895**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4896 4897**系统能力**:SystemCapability.Global.ResourceManager 4898 4899**返回值:** 4900 4901| 类型 | 说明 | 4902| ---------------------------------------- | ------------------- | 4903| [DeviceCapability](#devicecapability) | 设备的DeviceCapability。 | 4904 4905**示例:** 4906 ```ts 4907 try { 4908 let value = this.context.resourceManager.getDeviceCapabilitySync(); 4909 let screenDensity = value.screenDensity; 4910 let deviceType = value.deviceType; 4911 } catch (error) { 4912 console.error("getDeviceCapabilitySync error is " + error); 4913 } 4914 ``` 4915 4916### getDeviceCapability 4917 4918getDeviceCapability(callback: _AsyncCallback<DeviceCapability>): void 4919 4920用户获取设备的DeviceCapability,使用callback异步回调。 4921 4922**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4923 4924**系统能力**:SystemCapability.Global.ResourceManager 4925 4926**参数:** 4927 4928| 参数名 | 类型 | 必填 | 说明 | 4929| -------- | ---------------------------------------- | ---- | ---------------------------- | 4930| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<[DeviceCapability](#devicecapability)> | 是 | 返回设备的DeviceCapability。 | 4931 4932**示例:** 4933 ```ts 4934 import { resourceManager } from '@kit.LocalizationKit' 4935 4936 try { 4937 this.context.resourceManager.getDeviceCapability((error: BusinessError, value: resourceManager.DeviceCapability) => { 4938 if (error != null) { 4939 console.error("getDeviceCapability callback error is " + error); 4940 } else { 4941 let screenDensity = value.screenDensity; 4942 let deviceType = value.deviceType; 4943 } 4944 }); 4945 } catch (error) { 4946 console.error("getDeviceCapability callback error is " + error); 4947 } 4948 ``` 4949 4950### getDeviceCapability 4951 4952getDeviceCapability(): Promise<DeviceCapability> 4953 4954用户获取设备的DeviceCapability,使用Promise异步回调。 4955 4956**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4957 4958**系统能力**:SystemCapability.Global.ResourceManager 4959 4960**返回值:** 4961 4962| 类型 | 说明 | 4963| ---------------------------------------- | ------------------- | 4964| Promise<[DeviceCapability](#devicecapability)> | 设备的DeviceCapability。 | 4965 4966**示例:** 4967 ```ts 4968 import { BusinessError } from '@kit.BasicServicesKit'; 4969 import { resourceManager } from '@kit.LocalizationKit' 4970 4971 try { 4972 this.context.resourceManager.getDeviceCapability().then((value: resourceManager.DeviceCapability) => { 4973 let screenDensity = value.screenDensity; 4974 let deviceType = value.deviceType; 4975 }).catch((error: BusinessError) => { 4976 console.error("getDeviceCapability promise error is " + error); 4977 }); 4978 } catch (error) { 4979 console.error("getDeviceCapability promise error is " + error); 4980 } 4981 ``` 4982 4983### addResource<sup>10+</sup> 4984 4985addResource(path: string) : void 4986 4987应用运行时,加载指定的资源路径,实现资源覆盖。 4988 4989**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 4990 4991**系统能力**:SystemCapability.Global.ResourceManager 4992 4993**参数:** 4994 4995| 参数名 | 类型 | 必填 | 说明 | 4996| -------- | ---------------------- | ---- | ---- | 4997| path | string | 是 | 资源路径。 | 4998 4999**错误码:** 5000 5001以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 5002 5003| 错误码ID | 错误信息 | 5004| -------- | ---------------------------------------- | 5005| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5006| 9001010 | Invalid overlay path. | 5007 5008**示例:** 5009 ```ts 5010 import { BusinessError } from '@kit.BasicServicesKit'; 5011 5012 let path = this.context.bundleCodeDir + "/library1-default-signed.hsp"; 5013 try { 5014 this.context.resourceManager.addResource(path); 5015 } catch (error) { 5016 let code = (error as BusinessError).code; 5017 let message = (error as BusinessError).message; 5018 console.error(`addResource failed, error code: ${code}, message: ${message}.`); 5019 } 5020 ``` 5021 5022### removeResource<sup>10+</sup> 5023 5024removeResource(path: string) : void 5025 5026用户运行时,移除指定的资源路径,还原被覆盖前的资源。 5027 5028**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 5029 5030**系统能力**:SystemCapability.Global.ResourceManager 5031 5032**参数:** 5033 5034| 参数名 | 类型 | 必填 | 说明 | 5035| -------- | ---------------------- | ---- | ---- | 5036| path | string | 是 | 资源路径。 | 5037 5038**错误码:** 5039 5040以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 5041 5042| 错误码ID | 错误信息 | 5043| -------- | ---------------------------------------- | 5044| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5045| 9001010 | Invalid overlay path. | 5046 5047**示例:** 5048 ```ts 5049 import { BusinessError } from '@kit.BasicServicesKit'; 5050 5051 let path = this.context.bundleCodeDir + "/library1-default-signed.hsp"; 5052 try { 5053 this.context.resourceManager.removeResource(path); 5054 } catch (error) { 5055 let code = (error as BusinessError).code; 5056 let message = (error as BusinessError).message; 5057 console.error(`removeResource failed, error code: ${code}, message: ${message}.`); 5058 } 5059 ``` 5060 5061### getLocales<sup>11+</sup> 5062 5063getLocales(includeSystem?: boolean): Array\<string> 5064 5065获取应用的语言列表。 5066 5067**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 5068 5069**系统能力**:SystemCapability.Global.ResourceManager 5070 5071**参数:** 5072 5073| 参数名 | 类型 | 必填 | 说明 | 5074| -------------- | ------- | ------ | -------------------- | 5075| includeSystem | boolean | 否 | 是否包含系统资源,默认值为false。 <br> false:表示仅获取应用资源的语言列表。 <br>true:表示获取系统资源和应用资源的语言列表。 <br>当系统资源管理对象获取语言列表时,includeSystem值无效,返回获取系统资源语言列表 。| 5076 5077**返回值:** 5078 5079| 类型 | 说明 | 5080| ------------------------- | ----------- | 5081| Array\<string> | 返回获取的语言列表,列表中的字符串由语言、脚本(可选)、地区(可选),按照顺序使用中划线“-”连接组成。| 5082 5083**示例:** 5084 ```ts 5085 import { resourceManager } from '@kit.LocalizationKit' 5086 import { BusinessError } from '@kit.BasicServicesKit'; 5087 5088 try { 5089 this.context.resourceManager.getLocales(); // 仅获取应用资源语言列表 5090 } catch (error) { 5091 let code = (error as BusinessError).code; 5092 let message = (error as BusinessError).message; 5093 console.error(`getLocales failed, error code: ${code}, message: ${message}.`); 5094 } 5095 5096 try { 5097 resourceManager.getSystemResourceManager().getLocales(); // 仅获取系统资源语言列表 5098 } catch (error) { 5099 let code = (error as BusinessError).code; 5100 let message = (error as BusinessError).message; 5101 console.error(`getLocales failed, error code: ${code}, message: ${message}.`); 5102 } 5103 5104 try { 5105 this.context.resourceManager.getLocales(true); // 获取应用资源和系统资源语言列表 5106 } catch (error) { 5107 let code = (error as BusinessError).code; 5108 let message = (error as BusinessError).message; 5109 console.error(`getLocales failed, error code: ${code}, message: ${message}.`); 5110 } 5111 ``` 5112 5113### getSymbol<sup>11+</sup> 5114 5115getSymbol(resId: number):number 5116 5117用户获取指定资源ID对应的符号值,使用同步方式返回。 5118 5119**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 5120 5121**系统能力**:SystemCapability.Global.ResourceManager 5122 5123**参数:** 5124 5125| 参数名 | 类型 | 必填 | 说明 | 5126| ----- | ------ | ---- | ----- | 5127| resId | number | 是 | 资源ID值。 | 5128 5129**返回值:** 5130 5131| 类型 | 说明 | 5132| ------ | ----------- | 5133| number | 资源ID值对应的符号值(十进制)。 | 5134 5135**错误码:** 5136 5137以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 5138 5139| 错误码ID | 错误信息 | 5140| -------- | ---------------------------------------- | 5141| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5142| 9001001 | Invalid resource ID. | 5143| 9001002 | No matching resource is found based on the resource ID. | 5144| 9001006 | The resource is referenced cyclically. | 5145 5146**示例:** 5147 ```ts 5148 import { BusinessError } from '@kit.BasicServicesKit'; 5149 5150 try { 5151 this.context.resourceManager.getSymbol($r('app.symbol.test').id); 5152 } catch (error) { 5153 let code = (error as BusinessError).code; 5154 let message = (error as BusinessError).message; 5155 console.error(`getSymbol failed, error code: ${code}, message: ${message}.`); 5156 } 5157 ``` 5158 5159### getSymbol<sup>11+</sup> 5160getSymbol(resource: Resource): number 5161 5162用户获取指定resource对象对应的符号值,使用同步方式返回。 5163 5164**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 5165 5166**系统能力**:SystemCapability.Global.ResourceManager 5167 5168**模型约束**:此接口仅可在Stage模型下使用。 5169 5170**参数:** 5171 5172| 参数名 | 类型 | 必填 | 说明 | 5173| -------- | ---------------------- | ---- | ---- | 5174| resource | [Resource](#resource9) | 是 | 资源信息。 | 5175 5176**返回值:** 5177 5178| 类型 | 说明 | 5179| ------ | ----------- | 5180| number | resource对象对应的符号值(十进制)。 | 5181 5182**错误码:** 5183 5184以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 5185 5186| 错误码ID | 错误信息 | 5187| -------- | ---------------------------------------- | 5188| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5189| 9001001 | Invalid resource ID. | 5190| 9001002 | No matching resource is found based on the resource ID. | 5191| 9001006 | The resource is referenced cyclically. | 5192 5193**示例:** 5194 ```ts 5195 import { resourceManager } from '@kit.LocalizationKit' 5196 import { BusinessError } from '@kit.BasicServicesKit'; 5197 5198 let resource: resourceManager.Resource = { 5199 bundleName: "com.example.myapplication", 5200 moduleName: "entry", 5201 id: $r('app.symbol.test').id 5202 }; 5203 try { 5204 this.context.resourceManager.getSymbol(resource); 5205 } catch (error) { 5206 let code = (error as BusinessError).code; 5207 let message = (error as BusinessError).message; 5208 console.error(`getSymbol failed, error code: ${code}, message: ${message}.`); 5209 } 5210 ``` 5211 5212### getSymbolByName<sup>11+</sup> 5213 5214getSymbolByName(resName: string) : number; 5215 5216用户获取指定资源名称对应的符号值,使用同步方式返回。 5217 5218**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 5219 5220**系统能力**:SystemCapability.Global.ResourceManager 5221 5222**参数:** 5223 5224| 参数名 | 类型 | 必填 | 说明 | 5225| ------- | ------ | ---- | ---- | 5226| resName | string | 是 | 资源名称。 | 5227 5228**返回值:** 5229 5230| 类型 | 说明 | 5231| ------ | ---------- | 5232| number | 资源名称对应的符号值(十进制)。 | 5233 5234**错误码:** 5235 5236以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 5237 5238| 错误码ID | 错误信息 | 5239| -------- | ---------------------------------------- | 5240| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5241| 9001003 | Invalid resource name. | 5242| 9001004 | No matching resource is found based on the resource name. | 5243| 9001006 | The resource is referenced cyclically. | 5244 5245**示例:** 5246 ```ts 5247 import { BusinessError } from '@kit.BasicServicesKit'; 5248 5249 try { 5250 this.context.resourceManager.getSymbolByName("test"); 5251 } catch (error) { 5252 let code = (error as BusinessError).code; 5253 let message = (error as BusinessError).message; 5254 console.error(`getSymbolByName failed, error code: ${code}, message: ${message}.`); 5255 } 5256 ``` 5257 5258### isRawDir<sup>12+</sup> 5259 5260isRawDir(path: string) : bool 5261 5262用户判断指定路径是否是rawfile下的目录,使用同步方式返回。 5263 5264**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 5265 5266**系统能力**:SystemCapability.Global.ResourceManager 5267 5268**参数:** 5269 5270| 参数名 | 类型 | 必填 | 说明 | 5271| ------- | ------ | ---- | ---- | 5272| path | string | 是 | rawfile路径。 | 5273 5274**返回值:** 5275 5276| 类型 | 说明 | 5277| ------ | ---------- | 5278| bool |是否是rawfile下的目录。<br>true:表示是rawfile下的目录 <br>false:表示不是rawfile下的目录| 5279 5280**错误码:** 5281 5282以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 5283 5284| 错误码ID | 错误信息 | 5285| -------- | ---------------------------------------- | 5286| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5287| 9001005 | Invalid relative path. | 5288 5289**示例:** 5290 ```ts 5291 import { BusinessError } from '@kit.BasicServicesKit'; 5292 5293 try { 5294 this.context.resourceManager.isRawDir("test.txt"); 5295 } catch (error) { 5296 let code = (error as BusinessError).code; 5297 let message = (error as BusinessError).message; 5298 console.error(`isRawDir failed, error code: ${code}, message: ${message}.`); 5299 } 5300 ``` 5301 5302### getOverrideResourceManager<sup>12+</sup> 5303 5304getOverrideResourceManager(configuration?: Configuration) : ResourceManager 5305 5306获取可以加载差异化资源的资源管理对象,使用同步方式返回。 5307 5308普通的资源管理对象获取的资源的样式(语言、深浅色、分辨率、横竖屏等)是由系统决定的,而通过该接口返回的对象,应用可以获取符合指定配置的资源,即差异化资源,比如浅色模式时可以获取深色资源。 5309 5310**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 5311 5312**系统能力**:SystemCapability.Global.ResourceManager 5313 5314**参数:** 5315 5316| 参数名 | 类型 | 必填 | 说明 | 5317| ------------- | ------------------------------- | ---- | ------------------------------------------------------------ | 5318| configuration | [Configuration](#configuration) | 否 | 指定想要获取的资源样式。<br>通过[getOverrideConfiguration](#getoverrideconfiguration12)获取差异化配置后,根据需求修改配置项,再作为参数传入该函数。<br>若缺省则获取与当前系统最匹配的资源。 | 5319 5320**返回值:** 5321 5322| 类型 | 说明 | 5323| --------------- | ---------------------------------- | 5324| ResourceManager | 可以加载差异化资源的资源管理对象。 | 5325 5326**错误码:** 5327 5328以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 5329 5330| 错误码ID | 错误信息 | 5331| -------- | ------------------------------------------------------------ | 5332| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types | 5333 5334**示例:** 5335 5336 ```ts 5337 import { BusinessError } from '@kit.BasicServicesKit'; 5338 import { resourceManager } from '@kit.LocalizationKit' 5339 5340 try { 5341 let resMgr = this.context.resourceManager 5342 let overrideConfig = resMgr.getOverrideConfiguration() 5343 overrideConfig.colorMode = resourceManager.ColorMode.DARK 5344 let overrideResMgr = resMgr.getOverrideResourceManager(overrideConfig) 5345 } catch (error) { 5346 let code = (error as BusinessError).code; 5347 let message = (error as BusinessError).message; 5348 console.error(`getOverrideResourceManager failed, error code: ${code}, message: ${message}.`); 5349 } 5350 ``` 5351 5352### getOverrideConfiguration<sup>12+</sup> 5353 5354getOverrideConfiguration() : Configuration 5355 5356获取差异化资源的配置,使用同步方式返回。普通资源管理对象与通过它的[getOverrideResourceManager](#getoverrideresourcemanager12)接口获取的差异化资源管理对象调用该方法可获得相同的返回值。 5357 5358**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 5359 5360**系统能力**:SystemCapability.Global.ResourceManager 5361 5362**返回值:** 5363 5364| 类型 | 说明 | 5365| ------------------------------- | ---------------- | 5366| [Configuration](#configuration) | 差异化资源的配置。 | 5367 5368**示例:** 5369 5370 ```ts 5371 import { BusinessError } from '@kit.BasicServicesKit'; 5372 import { resourceManager } from '@kit.LocalizationKit' 5373 5374 let overrideConfig = this.context.resourceManager.getOverrideConfiguration() 5375 ``` 5376 5377### updateOverrideConfiguration<sup>12+</sup> 5378 5379updateOverrideConfiguration(configuration: Configuration) : void 5380 5381更新差异化资源配置。普通资源管理对象与通过它的[getOverrideResourceManager](#getoverrideresourcemanager12)接口获取的差异化资源管理对象调用该方法均可更新差异化资源管理对象的配置。 5382 5383**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 5384 5385**系统能力**:SystemCapability.Global.ResourceManager 5386 5387**参数:** 5388 5389| 参数名 | 类型 | 必填 | 说明 | 5390| ------------- | ------------------------------- | ---- | ------------------------------------------------------------ | 5391| configuration | [Configuration](#configuration) | 是 | 指定差异化资源的配置项。通过[getOverrideConfiguration](#getoverrideconfiguration12)获取差异化配置后,根据需求修改配置项,再作为参数传入。 | 5392 5393**错误码:** 5394 5395以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 5396 5397| 错误码ID | 错误信息 | 5398| -------- | ------------------------------------------------------------ | 5399| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types | 5400 5401**示例:** 5402 5403 ```ts 5404 import { BusinessError } from '@kit.BasicServicesKit'; 5405 import { resourceManager } from '@kit.LocalizationKit' 5406 5407 try { 5408 let resMgr = this.context.resourceManager 5409 let overrideConfig = resMgr.getOverrideConfiguration() 5410 overrideConfig.colorMode = resourceManager.ColorMode.DARK 5411 let overrideResMgr = resMgr.updateOverrideConfiguration(overrideConfig) 5412 } catch (error) { 5413 let code = (error as BusinessError).code; 5414 let message = (error as BusinessError).message; 5415 console.error(`updateOverrideConfiguration failed, error code: ${code}, message: ${message}.`); 5416 } 5417 ``` 5418 5419### release<sup>(deprecated)</sup> 5420 5421release() 5422 5423用户释放创建的resourceManager, 此接口暂不支持。 5424 5425从API version 7开始支持,API version 12开始不再维护。 5426 5427**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 5428 5429**系统能力**:SystemCapability.Global.ResourceManager 5430 5431**示例:** 5432 ```ts 5433 try { 5434 this.context.resourceManager.release(); 5435 } catch (error) { 5436 console.error("release error is " + error); 5437 } 5438 ``` 5439 5440### getString<sup>(deprecated)</sup> 5441 5442getString(resId: number, callback: AsyncCallback<string>): void 5443 5444用户获取指定资源ID对应的字符串,使用callback异步回调。 5445 5446从API version 9开始不再维护,建议使用[getStringValue](#getstringvalue9)代替。 5447 5448**系统能力**:SystemCapability.Global.ResourceManager 5449 5450**参数:** 5451 5452| 参数名 | 类型 | 必填 | 说明 | 5453| -------- | --------------------------- | ---- | --------------- | 5454| resId | number | 是 | 资源ID值。 | 5455| callback | [AsyncCallback](#asynccallbackdeprecated)<string> | 是 | 返回获取的字符串。 | 5456 5457**示例:** 5458 ```ts 5459 resourceManager.getResourceManager((error, mgr) => { 5460 mgr.getString($r('app.string.test').id, (error: Error, value: string) => { 5461 if (error != null) { 5462 console.error("error is " + error); 5463 } else { 5464 let str = value; 5465 } 5466 }); 5467 }); 5468 ``` 5469 5470 5471### getString<sup>(deprecated)</sup> 5472 5473getString(resId: number): Promise<string> 5474 5475用户获取指定资源ID对应的字符串,使用Promise异步回调。 5476 5477从API version 9开始不再维护,建议使用[getStringValue](#getstringvalue9-1)代替。 5478 5479**系统能力**:SystemCapability.Global.ResourceManager 5480 5481**参数:** 5482 5483| 参数名 | 类型 | 必填 | 说明 | 5484| ----- | ------ | ---- | ----- | 5485| resId | number | 是 | 资源ID值。 | 5486 5487**返回值:** 5488 5489| 类型 | 说明 | 5490| --------------------- | ----------- | 5491| Promise<string> | 资源ID值对应的字符串。 | 5492 5493**示例:** 5494 ```ts 5495 import { BusinessError } from '@kit.BasicServicesKit'; 5496 5497 resourceManager.getResourceManager((error, mgr) => { 5498 mgr.getString($r('app.string.test').id).then((value: string) => { 5499 let str = value; 5500 }).catch((error: BusinessError) => { 5501 console.error("getstring promise error is " + error); 5502 }); 5503 }); 5504 ``` 5505 5506 5507### getStringArray<sup>(deprecated)</sup> 5508 5509getStringArray(resId: number, callback: AsyncCallback<Array<string>>): void 5510 5511用户获取指定资源ID对应的字符串数组,使用callback异步回调。 5512 5513从API version 9开始不再维护,建议使用[getStringArrayValue](#getstringarrayvalue9)代替。 5514 5515**系统能力**:SystemCapability.Global.ResourceManager 5516 5517**参数:** 5518 5519| 参数名 | 类型 | 必填 | 说明 | 5520| -------- | ---------------------------------------- | ---- | ----------------- | 5521| resId | number | 是 | 资源ID值。 | 5522| callback | [AsyncCallback](#asynccallbackdeprecated)<Array<string>> | 是 | 返回获取的字符串数组。 | 5523 5524**示例:** 5525 ```ts 5526 resourceManager.getResourceManager((error, mgr) => { 5527 mgr.getStringArray($r('app.strarray.test').id, (error: Error, value: Array<string>) => { 5528 if (error != null) { 5529 console.error("error is " + error); 5530 } else { 5531 let strArray = value; 5532 } 5533 }); 5534 }); 5535 ``` 5536 5537 5538### getStringArray<sup>(deprecated)</sup> 5539 5540getStringArray(resId: number): Promise<Array<string>> 5541 5542用户获取指定资源ID对应的字符串数组,使用Promise异步回调。 5543 5544从API version 9开始不再维护,建议使用[getStringArrayValue](#getstringarrayvalue9-1)代替。 5545 5546**系统能力**:SystemCapability.Global.ResourceManager 5547 5548**参数:** 5549 5550| 参数名 | 类型 | 必填 | 说明 | 5551| ----- | ------ | ---- | ----- | 5552| resId | number | 是 | 资源ID值。 | 5553 5554**返回值:** 5555 5556| 类型 | 说明 | 5557| ---------------------------------- | ------------- | 5558| Promise<Array<string>> | 资源ID值对应的字符串数组。 | 5559 5560**示例:** 5561 ```ts 5562 import { BusinessError } from '@kit.BasicServicesKit'; 5563 5564 resourceManager.getResourceManager((error, mgr) => { 5565 mgr.getStringArray($r('app.strarray.test').id).then((value: Array<string>) => { 5566 let strArray = value; 5567 }).catch((error: BusinessError) => { 5568 console.error("getStringArray promise error is " + error); 5569 }); 5570 }); 5571 ``` 5572 5573 5574### getMedia<sup>(deprecated)</sup> 5575 5576getMedia(resId: number, callback: AsyncCallback<Uint8Array>): void 5577 5578用户获取指定资源ID对应的媒体文件内容,使用callback异步回调。 5579 5580从API version 9开始不再维护,建议使用[getMediaContent](#getmediacontent9)代替。 5581 5582**系统能力**:SystemCapability.Global.ResourceManager 5583 5584**参数:** 5585 5586| 参数名 | 类型 | 必填 | 说明 | 5587| -------- | ------------------------------- | ---- | ------------------ | 5588| resId | number | 是 | 资源ID值。 | 5589| callback | [AsyncCallback](#asynccallbackdeprecated)<Uint8Array> | 是 | 返回获取的媒体文件内容。 | 5590 5591**示例:** 5592 ```ts 5593 resourceManager.getResourceManager((error, mgr) => { 5594 mgr.getMedia($r('app.media.test').id, (error: Error, value: Uint8Array) => { 5595 if (error != null) { 5596 console.error("error is " + error); 5597 } else { 5598 let media = value; 5599 } 5600 }); 5601 }); 5602 ``` 5603 5604### getMedia<sup>(deprecated)</sup> 5605 5606getMedia(resId: number): Promise<Uint8Array> 5607 5608用户获取指定资源ID对应的媒体文件内容,使用Promise异步回调。 5609 5610从API version 9开始不再维护,建议使用[getMediaContent](#getmediacontent9-1)代替。 5611 5612**系统能力**:SystemCapability.Global.ResourceManager 5613 5614**参数:** 5615 5616| 参数名 | 类型 | 必填 | 说明 | 5617| ----- | ------ | ---- | ----- | 5618| resId | number | 是 | 资源ID值。 | 5619 5620**返回值:** 5621 5622| 类型 | 说明 | 5623| ------------------------- | -------------- | 5624| Promise<Uint8Array> | 资源ID值对应的媒体文件内容。 | 5625 5626**示例:** 5627 ```ts 5628 import { BusinessError } from '@kit.BasicServicesKit'; 5629 5630 resourceManager.getResourceManager((error, mgr) => { 5631 mgr.getMedia($r('app.media.test').id).then((value: Uint8Array) => { 5632 let media = value; 5633 }).catch((error: BusinessError) => { 5634 console.error("getMedia promise error is " + error); 5635 }); 5636 }); 5637 ``` 5638 5639 5640### getMediaBase64<sup>(deprecated)</sup> 5641 5642getMediaBase64(resId: number, callback: AsyncCallback<string>): void 5643 5644用户获取指定资源ID对应的图片资源Base64编码,使用callback异步回调。 5645 5646从API version 9开始不再维护,建议使用[getMediaContentBase64](#getmediacontentbase649)代替。 5647 5648**系统能力**:SystemCapability.Global.ResourceManager 5649 5650**参数:** 5651 5652| 参数名 | 类型 | 必填 | 说明 | 5653| -------- | --------------------------- | ---- | ------------------------ | 5654| resId | number | 是 | 资源ID值。 | 5655| callback | [AsyncCallback](#asynccallbackdeprecated)<string> | 是 | 异步回调,用于返回获取的图片资源Base64编码。 | 5656 5657**示例:** 5658 ```ts 5659 resourceManager.getResourceManager((error, mgr) => { 5660 mgr.getMediaBase64($r('app.media.test').id, ((error: Error, value: string) => { 5661 if (error != null) { 5662 console.error("error is " + error); 5663 } else { 5664 let media = value; 5665 } 5666 }); 5667 }); 5668 ``` 5669 5670 5671### getMediaBase64<sup>(deprecated)</sup> 5672 5673getMediaBase64(resId: number): Promise<string> 5674 5675用户获取指定资源ID对应的图片资源Base64编码,使用Promise异步回调。 5676 5677从API version 9开始不再维护,建议使用[getMediaContentBase64](#getmediacontentbase649-1)代替。 5678 5679**系统能力**:SystemCapability.Global.ResourceManager 5680 5681**参数:** 5682 5683| 参数名 | 类型 | 必填 | 说明 | 5684| ----- | ------ | ---- | ----- | 5685| resId | number | 是 | 资源ID值。 | 5686 5687**返回值:** 5688 5689| 类型 | 说明 | 5690| --------------------- | -------------------- | 5691| Promise<string> | 资源ID值对应的图片资源Base64编码。 | 5692 5693**示例:** 5694 ```ts 5695 import { BusinessError } from '@kit.BasicServicesKit'; 5696 5697 resourceManager.getResourceManager((error, mgr) => { 5698 mgr.getMediaBase64($r('app.media.test').id).then((value: string) => { 5699 let media = value; 5700 }).catch((error: BusinessError) => { 5701 console.error("getMediaBase64 promise error is " + error); 5702 }); 5703 }); 5704 ``` 5705 5706### getPluralStringValueSync<sup>(deprecated)</sup> 5707 5708getPluralStringValueSync(resId: number, num: number): string 5709 5710获取指定资源ID,指定资源数量的单复数字符串,使用同步方式返回。 5711 5712> **说明** 5713> 5714> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 5715> 5716> 从API version 10开始支持,从API version 18开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18)替代。 5717 5718**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 5719 5720**系统能力**:SystemCapability.Global.ResourceManager 5721 5722**参数:** 5723 5724| 参数名 | 类型 | 必填 | 说明 | 5725| ------ | ------ | ---- | ------------------------------------------------------------ | 5726| resId | number | 是 | 资源ID值。 | 5727| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 5728 5729**返回值:** 5730 5731| 类型 | 说明 | 5732| ------ | ------------------------------------------------ | 5733| string | 根据指定数量获取指定ID字符串表示的单复数字符串。 | 5734 5735**错误码:** 5736 5737以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 5738 5739| 错误码ID | 错误信息 | 5740| -------- | ------------------------------------------------------------ | 5741| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5742| 9001001 | Invalid resource ID. | 5743| 9001002 | No matching resource is found based on the resource ID. | 5744| 9001006 | The resource is referenced cyclically. | 5745 5746**示例:** 5747 5748 ```ts 5749 import { BusinessError } from '@kit.BasicServicesKit'; 5750 5751 try { 5752 this.context.resourceManager.getPluralStringValueSync($r('app.plural.test').id, 1); 5753 } catch (error) { 5754 let code = (error as BusinessError).code; 5755 let message = (error as BusinessError).message; 5756 console.error(`getPluralStringValueSync failed, error code: ${code}, message: ${message}.`); 5757 } 5758 ``` 5759 5760### getPluralStringValueSync<sup>(deprecated)</sup> 5761 5762getPluralStringValueSync(resource: Resource, num: number): string 5763 5764获取指定资源信息,指定资源数量的单复数字符串,使用同步方式返回。 5765 5766> **说明** 5767> 5768> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 5769> 5770> 从API version 10开始支持,从API version 18开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18-1)替代。 5771 5772**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 5773 5774**系统能力**:SystemCapability.Global.ResourceManager 5775 5776**模型约束**:此接口仅可在Stage模型下使用。 5777 5778**参数:** 5779 5780| 参数名 | 类型 | 必填 | 说明 | 5781| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 5782| resource | [Resource](#resource9) | 是 | 资源信息。 | 5783| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 5784 5785**返回值:** 5786 5787| 类型 | 说明 | 5788| ------ | ---------------------------------------------------- | 5789| string | 根据指定数量获取指定resource对象表示的单复数字符串。 | 5790 5791**错误码:** 5792 5793以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 5794 5795| 错误码ID | 错误信息 | 5796| -------- | ------------------------------------------------------------ | 5797| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5798| 9001001 | Invalid resource ID. | 5799| 9001002 | No matching resource is found based on the resource ID. | 5800| 9001006 | The resource is referenced cyclically. | 5801 5802**示例:** 5803 5804 ```ts 5805 import { resourceManager } from '@kit.LocalizationKit' 5806 import { BusinessError } from '@kit.BasicServicesKit'; 5807 5808 let resource: resourceManager.Resource = { 5809 bundleName: "com.example.myapplication", 5810 moduleName: "entry", 5811 id: $r('app.plural.test').id 5812 }; 5813 try { 5814 this.context.resourceManager.getPluralStringValueSync(resource, 1); 5815 } catch (error) { 5816 let code = (error as BusinessError).code; 5817 let message = (error as BusinessError).message; 5818 console.error(`getPluralStringValueSync failed, error code: ${code}, message: ${message}.`); 5819 } 5820 ``` 5821 5822### getPluralStringByNameSync<sup>(deprecated)</sup> 5823 5824getPluralStringByNameSync(resName: string, num: number): string 5825 5826获取指定资源名称,指定资源数量的单复数字符串,使用同步方式返回。 5827 5828> **说明** 5829> 5830> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 5831> 5832> 从API version 10开始支持,从API version 18开始废弃,建议使用[getIntPluralStringByNameSync](#getintpluralstringbynamesync18)替代。 5833 5834**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 5835 5836**系统能力**:SystemCapability.Global.ResourceManager 5837 5838**参数:** 5839 5840| 参数名 | 类型 | 必填 | 说明 | 5841| ------- | ------ | ---- | ------------------------------------------------------------ | 5842| resName | string | 是 | 资源名称。 | 5843| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 5844 5845**返回值:** 5846 5847| 类型 | 说明 | 5848| ------ | ------------------------------------------------ | 5849| string | 根据指定数量获取指定资源名称表示的单复数字符串。 | 5850 5851**错误码:** 5852 5853以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 5854 5855| 错误码ID | 错误信息 | 5856| -------- | ------------------------------------------------------------ | 5857| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5858| 9001003 | Invalid resource name. | 5859| 9001004 | No matching resource is found based on the resource name. | 5860| 9001006 | The resource is referenced cyclically. | 5861 5862**示例:** 5863 5864 ```ts 5865 import { BusinessError } from '@kit.BasicServicesKit'; 5866 5867 try { 5868 this.context.resourceManager.getPluralStringByNameSync("test", 1); 5869 } catch (error) { 5870 let code = (error as BusinessError).code; 5871 let message = (error as BusinessError).message; 5872 console.error(`getPluralStringByNameSync failed, error code: ${code}, message: ${message}.`); 5873 } 5874 ``` 5875 5876### getPluralStringValue<sup>(deprecated)</sup> 5877 5878getPluralStringValue(resId: number, num: number, callback: _AsyncCallback<string>): void 5879 5880获取指定资源ID,指定资源数量的单复数字符串,使用callback异步回调。 5881 5882> **说明** 5883> 5884> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 5885> 5886> 从API version 9开始支持,从API version 18开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18)替代。 5887 5888**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 5889 5890**系统能力**:SystemCapability.Global.ResourceManager 5891 5892**参数:** 5893 5894| 参数名 | 类型 | 必填 | 说明 | 5895| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 5896| resId | number | 是 | 资源ID值。 | 5897| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 5898| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 根据指定数量,获取指定ID字符串表示的单复数字符串。 | 5899 5900**错误码:** 5901 5902以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 5903 5904| 错误码ID | 错误信息 | 5905| -------- | ------------------------------------------------------------ | 5906| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5907| 9001001 | Invalid resource ID. | 5908| 9001002 | No matching resource is found based on the resource ID. | 5909| 9001006 | The resource is referenced cyclically. | 5910 5911**示例:** 5912 5913 ```ts 5914 import { BusinessError } from '@kit.BasicServicesKit'; 5915 5916 try { 5917 this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1, (error: BusinessError, value: string) => { 5918 if (error != null) { 5919 console.error("error is " + error); 5920 } else { 5921 let str = value; 5922 } 5923 }); 5924 } catch (error) { 5925 let code = (error as BusinessError).code; 5926 let message = (error as BusinessError).message; 5927 console.error(`callback getPluralStringValue failed, error code: ${code}, message: ${message}.`); 5928 } 5929 ``` 5930 5931### getPluralStringValue<sup>(deprecated)</sup> 5932 5933getPluralStringValue(resId: number, num: number): Promise<string> 5934 5935获取指定资源ID,指定资源数量的单复数字符串,使用Promise异步回调。 5936 5937> **说明** 5938> 5939> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 5940> 5941> 从API version 9开始支持,从API version 18开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18)替代。 5942 5943**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 5944 5945**系统能力**:SystemCapability.Global.ResourceManager 5946 5947**参数:** 5948 5949| 参数名 | 类型 | 必填 | 说明 | 5950| ------ | ------ | ---- | ------------------------------------------------------------ | 5951| resId | number | 是 | 资源ID值。 | 5952| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 5953 5954**返回值:** 5955 5956| 类型 | 说明 | 5957| --------------------- | ---------------------------------------------------- | 5958| Promise<string> | 根据提供的数量,获取对应ID字符串表示的单复数字符串。 | 5959 5960**错误码:** 5961 5962以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 5963 5964| 错误码ID | 错误信息 | 5965| -------- | ------------------------------------------------------------ | 5966| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 5967| 9001001 | Invalid resource ID. | 5968| 9001002 | No matching resource is found based on the resource ID. | 5969| 9001006 | The resource is referenced cyclically. | 5970 5971**示例:** 5972 5973 ```ts 5974 import { BusinessError } from '@kit.BasicServicesKit'; 5975 5976 try { 5977 this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1).then((value: string) => { 5978 let str = value; 5979 }).catch((error: BusinessError) => { 5980 console.error("getPluralStringValue promise error is " + error); 5981 }); 5982 } catch (error) { 5983 let code = (error as BusinessError).code; 5984 let message = (error as BusinessError).message; 5985 console.error(`promise getPluralStringValue failed, error code: ${code}, message: ${message}.`); 5986 } 5987 ``` 5988 5989### getPluralStringValue<sup>(deprecated)</sup> 5990 5991getPluralStringValue(resource: Resource, num: number, callback: _AsyncCallback<string>): void 5992 5993获取指定资源信息,指定资源数量的单复数字符串,使用callback异步回调。 5994 5995> **说明** 5996> 5997> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 5998> 5999> 从API version 9开始支持,从API version 18开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18-1)替代。 6000 6001**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 6002 6003**系统能力**:SystemCapability.Global.ResourceManager 6004 6005**模型约束**:此接口仅可在Stage模型下使用。 6006 6007**参数:** 6008 6009| 参数名 | 类型 | 必填 | 说明 | 6010| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 6011| resource | [Resource](#resource9) | 是 | 资源信息。 | 6012| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 6013| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 根据指定数量,获取指定resource对象表示的单复数字符串。 | 6014 6015**错误码:** 6016 6017以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)。 6018 6019| 错误码ID | 错误信息 | 6020| -------- | ------------------------------------------------------------ | 6021| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 6022| 9001001 | Invalid resource ID. | 6023| 9001002 | No matching resource is found based on the resource ID. | 6024| 9001006 | The resource is referenced cyclically. | 6025 6026**示例:** 6027 6028 ```ts 6029 import { resourceManager } from '@kit.LocalizationKit' 6030 import { BusinessError } from '@kit.BasicServicesKit'; 6031 6032 let resource: resourceManager.Resource = { 6033 bundleName: "com.example.myapplication", 6034 moduleName: "entry", 6035 id: $r('app.plural.test').id 6036 }; 6037 try { 6038 this.context.resourceManager.getPluralStringValue(resource, 1, (error: BusinessError, value: string) => { 6039 if (error != null) { 6040 console.error("error is " + error); 6041 } else { 6042 let str = value; 6043 } 6044 }); 6045 } catch (error) { 6046 let code = (error as BusinessError).code; 6047 let message = (error as BusinessError).message; 6048 console.error(`callback getPluralStringValue failed, error code: ${code}, message: ${message}.`); 6049 } 6050 ``` 6051 6052### getPluralStringValue<sup>(deprecated)</sup> 6053 6054getPluralStringValue(resource: Resource, num: number): Promise<string> 6055 6056获取指定资源信息,指定资源数量的单复数字符串,使用Promise异步回调。 6057 6058> **说明** 6059> 6060> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 6061> 6062> 从API version 9开始支持,从API version 18开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18-1)替代。 6063 6064**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 6065 6066**系统能力**:SystemCapability.Global.ResourceManager 6067 6068**模型约束**:此接口仅可在Stage模型下使用。 6069 6070**参数:** 6071 6072| 参数名 | 类型 | 必填 | 说明 | 6073| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 6074| resource | [Resource](#resource9) | 是 | 资源信息。 | 6075| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 6076 6077**返回值:** 6078 6079| 类型 | 说明 | 6080| --------------------- | -------------------------------------------------------- | 6081| Promise<string> | 根据提供的数量,获取对应resource对象表示的单复数字符串。 | 6082 6083**错误码:** 6084 6085以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 6086 6087| 错误码ID | 错误信息 | 6088| -------- | ------------------------------------------------------------ | 6089| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 6090| 9001001 | Invalid resource ID. | 6091| 9001002 | No matching resource is found based on the resource ID. | 6092| 9001006 | The resource is referenced cyclically. | 6093 6094**示例:** 6095 6096 ```ts 6097 import { resourceManager } from '@kit.LocalizationKit' 6098 import { BusinessError } from '@kit.BasicServicesKit'; 6099 6100 let resource: resourceManager.Resource = { 6101 bundleName: "com.example.myapplication", 6102 moduleName: "entry", 6103 id: $r('app.plural.test').id 6104 }; 6105 try { 6106 this.context.resourceManager.getPluralStringValue(resource, 1).then((value: string) => { 6107 let str = value; 6108 }).catch((error: BusinessError) => { 6109 console.error("getPluralStringValue promise error is " + error); 6110 }); 6111 } catch (error) { 6112 let code = (error as BusinessError).code; 6113 let message = (error as BusinessError).message; 6114 console.error(`promise getPluralStringValue failed, error code: ${code}, message: ${message}.`); 6115 } 6116 ``` 6117 6118### getPluralStringByName<sup>(deprecated)</sup> 6119 6120getPluralStringByName(resName: string, num: number, callback: _AsyncCallback<string>): void 6121 6122获取指定资源名称,指定资源数量的单复数字符串,使用callback异步回调。 6123 6124> **说明** 6125> 6126> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 6127> 6128> 从API version 9开始支持,从API version 18开始废弃,建议使用[getIntPluralStringByNameSync](#getintpluralstringbynamesync18)替代。 6129 6130**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 6131 6132**系统能力**:SystemCapability.Global.ResourceManager 6133 6134**参数:** 6135 6136| 参数名 | 类型 | 必填 | 说明 | 6137| -------- | --------------------------- | ---- | ------------------------------------------------------------ | 6138| resName | string | 是 | 资源名称。 | 6139| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 6140| callback | [_AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)<string> | 是 | 根据传入的数量值,获取资源名称对应的字符串资源。 | 6141 6142**错误码:** 6143 6144以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 6145 6146| 错误码ID | 错误信息 | 6147| -------- | ------------------------------------------------------------ | 6148| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 6149| 9001003 | Invalid resource name. | 6150| 9001004 | No matching resource is found based on the resource name. | 6151| 9001006 | The resource is referenced cyclically. | 6152 6153**示例:** 6154 6155 ```ts 6156 import { BusinessError } from '@kit.BasicServicesKit'; 6157 6158 try { 6159 this.context.resourceManager.getPluralStringByName("test", 1, (error: BusinessError, value: string) => { 6160 if (error != null) { 6161 console.error("error is " + error); 6162 } else { 6163 let str = value; 6164 } 6165 }); 6166 } catch (error) { 6167 let code = (error as BusinessError).code; 6168 let message = (error as BusinessError).message; 6169 console.error(`callback getPluralStringByName failed, error code: ${code}, message: ${message}.`); 6170 } 6171 ``` 6172 6173### getPluralStringByName<sup>(deprecated)</sup> 6174 6175getPluralStringByName(resName: string, num: number): Promise<string> 6176 6177获取指定资源名称,指定资源数量的单复数字符串,使用Promise异步回调。 6178 6179> **说明** 6180> 6181> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 6182> 6183> 从API version 9开始支持,从API version 18开始废弃,建议使用[getIntPluralStringByNameSync](#getintpluralstringbynamesync18)替代。 6184 6185**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 6186 6187**系统能力**:SystemCapability.Global.ResourceManager 6188 6189**参数:** 6190 6191| 参数名 | 类型 | 必填 | 说明 | 6192| ------- | ------ | ---- | ------------------------------------------------------------ | 6193| resName | string | 是 | 资源名称。 | 6194| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 6195 6196**返回值:** 6197 6198| 类型 | 说明 | 6199| --------------------- | ------------------------------------------------ | 6200| Promise<string> | 根据传入的数量值,获取资源名称对应的字符串资源。 | 6201 6202**错误码:** 6203 6204以下错误码的详细介绍请参见[资源管理错误码](errorcode-resource-manager.md)和[通用错误码](../errorcode-universal.md)。 6205 6206| 错误码ID | 错误信息 | 6207| -------- | ------------------------------------------------------------ | 6208| 401 | If the input parameter invalid. Possible causes: Incorrect parameter types. | 6209| 9001003 | Invalid resource name. | 6210| 9001004 | No matching resource is found based on the resource name. | 6211| 9001006 | The resource is referenced cyclically. | 6212 6213**示例:** 6214 6215 ```ts 6216 import { BusinessError } from '@kit.BasicServicesKit'; 6217 6218 try { 6219 this.context.resourceManager.getPluralStringByName("test", 1).then((value: string) => { 6220 let str = value; 6221 }).catch((error: BusinessError) => { 6222 console.error("getPluralStringByName promise error is " + error); 6223 }); 6224 } catch (error) { 6225 let code = (error as BusinessError).code; 6226 let message = (error as BusinessError).message; 6227 console.error(`promise getPluralStringByName failed, error code: ${code}, message: ${message}.`); 6228 } 6229 ``` 6230 6231### getPluralString<sup>(deprecated)</sup> 6232 6233getPluralString(resId: number, num: number): Promise<string> 6234 6235获取指定资源ID,指定资源数量的单复数字符串,使用Promise异步回调。 6236 6237> **说明** 6238> 6239> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 6240> 6241> 从API version 6开始支持,从API version 9开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18)替代。 6242 6243**系统能力**:SystemCapability.Global.ResourceManager 6244 6245**参数:** 6246 6247| 参数名 | 类型 | 必填 | 说明 | 6248| ----- | ------ | ---- | ----- | 6249| resId | number | 是 | 资源ID值。 | 6250| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 6251 6252**返回值:** 6253 6254| 类型 | 说明 | 6255| --------------------- | ------------------------- | 6256| Promise<string> | 根据提供的数量获取对应ID字符串表示的单复数字符串。 | 6257 6258**示例:** 6259 6260 ```ts 6261 import { BusinessError } from '@kit.BasicServicesKit'; 6262 6263 resourceManager.getResourceManager((error, mgr) => { 6264 mgr.getPluralString($r("app.plural.test").id, 1).then((value: string) => { 6265 let str = value; 6266 }).catch((error: BusinessError) => { 6267 console.error("getPluralString promise error is " + error); 6268 }); 6269 }); 6270 ``` 6271 6272 6273### getPluralString<sup>(deprecated)</sup> 6274 6275getPluralString(resId: number, num: number, callback: AsyncCallback<string>): void 6276 6277获取指定资源ID,指定资源数量的单复数字符串,使用callback异步回调。 6278 6279> **说明** 6280> 6281> 中文环境下,字符串不区分单复数;英文环境下,字符串区分单复数。 6282> 6283> 从API version 6开始支持,从API version 9开始废弃,建议使用[getIntPluralStringValueSync](#getintpluralstringvaluesync18)替代。 6284 6285**系统能力**:SystemCapability.Global.ResourceManager 6286 6287**参数:** 6288 6289| 参数名 | 类型 | 必填 | 说明 | 6290| -------- | --------------------------- | ---- | ------------------------------- | 6291| resId | number | 是 | 资源ID值。 | 6292| num | number | 是 | 数量值。根据当前语言的复数规则获取该数量值对应的字符串数字,语言的复数规则参见[语言单复数规则](https://www.unicode.org/cldr/charts/45/supplemental/language_plural_rules.html)。 | 6293| callback | [AsyncCallback](#asynccallbackdeprecated)<string> | 是 | 异步回调,返回根据指定数量获取指定ID字符串表示的单复数字符串。 | 6294 6295**示例:** 6296 6297 ```ts 6298 resourceManager.getResourceManager((error, mgr) => { 6299 mgr.getPluralString($r("app.plural.test").id, 1, (error: Error, value: string) => { 6300 if (error != null) { 6301 console.error("error is " + error); 6302 } else { 6303 let str = value; 6304 } 6305 }); 6306 }); 6307 ``` 6308 6309 6310### getRawFile<sup>(deprecated)</sup> 6311 6312getRawFile(path: string, callback: AsyncCallback<Uint8Array>): void 6313 6314用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback异步回调。 6315 6316> **说明** 6317> 6318> 从API version 8开始支持,从API version 9开始废弃,建议使用[getRawFileContent](#getrawfilecontent9)替代。 6319 6320**系统能力**:SystemCapability.Global.ResourceManager 6321 6322**参数:** 6323 6324| 参数名 | 类型 | 必填 | 说明 | 6325| -------- | ------------------------------- | ---- | ----------------------- | 6326| path | string | 是 | rawfile文件路径。 | 6327| callback | [AsyncCallback](#asynccallbackdeprecated)<Uint8Array> | 是 | 异步回调,用于返回获取的rawfile文件内容。 | 6328 6329**示例:** 6330 ```ts 6331 resourceManager.getResourceManager((error, mgr) => { 6332 mgr.getRawFile("test.txt", (error: Error, value: Uint8Array) => { 6333 if (error != null) { 6334 console.error("error is " + error); 6335 } else { 6336 let rawFile = value; 6337 } 6338 }); 6339 }); 6340 ``` 6341 6342 6343### getRawFile<sup>(deprecated)</sup> 6344 6345getRawFile(path: string): Promise<Uint8Array> 6346 6347用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise异步回调。 6348 6349> **说明** 6350> 6351> 从API version 8开始支持,从API version 9开始废弃,建议使用[getRawFileContent](#getrawfilecontent9-1)替代。 6352 6353**系统能力**:SystemCapability.Global.ResourceManager 6354 6355**参数:** 6356 6357| 参数名 | 类型 | 必填 | 说明 | 6358| ---- | ------ | ---- | ----------- | 6359| path | string | 是 | rawfile文件路径。 | 6360 6361**返回值:** 6362 6363| 类型 | 说明 | 6364| ------------------------- | ----------- | 6365| Promise<Uint8Array> | rawfile文件内容。 | 6366 6367**示例:** 6368 ```ts 6369 import { BusinessError } from '@kit.BasicServicesKit'; 6370 6371 resourceManager.getResourceManager((error, mgr) => { 6372 mgr.getRawFile("test.txt").then((value: Uint8Array) => { 6373 let rawFile = value; 6374 }).catch((error: BusinessError) => { 6375 console.error("getRawFile promise error is " + error); 6376 }); 6377 }); 6378 ``` 6379 6380 6381### getRawFileDescriptor<sup>(deprecated)</sup> 6382 6383getRawFileDescriptor(path: string, callback: AsyncCallback<RawFileDescriptor>): void 6384 6385用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用callback异步回调。 6386 6387> **说明** 6388> 6389> 从API version 8开始支持,从API version 9开始废弃,建议使用[getRawFd](#getrawfd9)替代。 6390 6391**系统能力**:SystemCapability.Global.ResourceManager 6392 6393**参数:** 6394 6395| 参数名 | 类型 | 必填 | 说明 | 6396| -------- | ---------------------------------------- | ---- | -------------------------------- | 6397| path | string | 是 | rawfile文件路径。 | 6398| callback | [AsyncCallback](#asynccallbackdeprecated)<[RawFileDescriptor](#rawfiledescriptor9)> | 是 | 异步回调,用于返回获取的rawfile文件的descriptor。 | 6399 6400**示例:** 6401 ```ts 6402 import { resourceManager } from '@kit.LocalizationKit' 6403 6404 resourceManager.getResourceManager((error, mgr) => { 6405 mgr.getRawFileDescriptor("test.txt", (error: Error, value: resourceManager.RawFileDescriptor) => { 6406 if (error != null) { 6407 console.error("error is " + error); 6408 } else { 6409 let fd = value.fd; 6410 let offset = value.offset; 6411 let length = value.length; 6412 } 6413 }); 6414 }); 6415 ``` 6416 6417### getRawFileDescriptor<sup>(deprecated)</sup> 6418 6419getRawFileDescriptor(path: string): Promise<RawFileDescriptor> 6420 6421用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用Promise异步回调。 6422 6423> **说明** 6424> 6425> 从API version 8开始支持,从API version 9开始废弃,建议使用[getRawFd](#getrawfd9-1)替代。 6426 6427**系统能力**:SystemCapability.Global.ResourceManager 6428 6429**参数:** 6430 6431| 参数名 | 类型 | 必填 | 说明 | 6432| ---- | ------ | ---- | ----------- | 6433| path | string | 是 | rawfile文件路径。 | 6434 6435**返回值:** 6436 6437| 类型 | 说明 | 6438| ---------------------------------------- | ------------------- | 6439| Promise<[RawFileDescriptor](#rawfiledescriptor9)> | rawfile文件descriptor。 | 6440 6441**示例:** 6442 ```ts 6443 import { BusinessError } from '@kit.BasicServicesKit'; 6444 6445 resourceManager.getResourceManager((error, mgr) => { 6446 mgr.getRawFileDescriptor("test.txt").then((value: resourceManager.RawFileDescriptor) => { 6447 let fd = value.fd; 6448 let offset = value.offset; 6449 let length = value.length; 6450 }).catch((error: BusinessError) => { 6451 console.error("getRawFileDescriptor promise error is " + error); 6452 }); 6453 }); 6454 ``` 6455 6456### closeRawFileDescriptor<sup>(deprecated)</sup> 6457 6458closeRawFileDescriptor(path: string, callback: AsyncCallback<void>): void 6459 6460用户关闭resources/rawfile目录下rawfile文件的descriptor,使用callback异步回调。 6461 6462> **说明** 6463> 6464> 从API version 8开始支持,从API version 9开始废弃,建议使用[closeRawFd](#closerawfd9)替代。 6465 6466**系统能力**:SystemCapability.Global.ResourceManager 6467 6468**参数:** 6469 6470 6471 6472| 参数名 | 类型 | 必填 | 说明 | 6473| -------- | ------------------------- | ---- | ----------- | 6474| path | string | 是 | rawfile文件路径。 | 6475| callback | [AsyncCallback](#asynccallbackdeprecated)<void> | 是 | 异步回调。 | 6476 6477**示例:** 6478 ```ts 6479 resourceManager.getResourceManager((error, mgr) => { 6480 mgr.closeRawFileDescriptor("test.txt", (error: Error) => { 6481 if (error != null) { 6482 console.error("error is " + error); 6483 } 6484 }); 6485 }); 6486 ``` 6487 6488### closeRawFileDescriptor<sup>(deprecated)</sup> 6489 6490closeRawFileDescriptor(path: string): Promise<void> 6491 6492用户关闭resources/rawfile目录下rawfile文件的descriptor,使用Promise异步回调。 6493 6494> **说明** 6495> 6496> 从API version 8开始支持,从API version 9开始废弃,建议使用[closeRawFd](#closerawfd9-1)替代。 6497 6498**系统能力**:SystemCapability.Global.ResourceManager 6499 6500**参数:** 6501 6502| 参数名 | 类型 | 必填 | 说明 | 6503| ---- | ------ | ---- | ----------- | 6504| path | string | 是 | rawfile文件路径。 | 6505 6506**返回值:** 6507 6508| 类型 | 说明 | 6509| ------------------- | ---- | 6510| Promise<void> | 无返回值。 | 6511 6512**示例:** 6513 ```ts 6514 resourceManager.getResourceManager((error, mgr) => { 6515 mgr.closeRawFileDescriptor("test.txt"); 6516 }); 6517 ``` 6518 6519## AsyncCallback<sup>(deprecated)</sup> 6520 6521``` 6522AsyncCallback<T> { 6523 (err: Error, data: T): void; 6524} 6525``` 6526 6527异步回调函数,携带错误参数和异步返回值。 6528 6529> **说明** 6530> 6531> 从API version 6开始支持,从API version 9开始废弃,建议使用[AsyncCallback](../apis-basic-services-kit/js-apis-base.md#asynccallback)替代。 6532 6533**系统能力:** SystemCapability.Global.ResourceManager 6534 6535| 参数名 | 类型 | 必填 | 说明 | 6536| ---- | ------------------------------------------------------------ | ---- | ---------------------------- | 6537| err | Error | 是 | 接口调用失败的错误信息。 | 6538| data | T | 是 | 接口调用时的回调信息。| 6539 6540## 附录 6541 6542- 示例代码中用到的'app.string.test'文件内容如下: 6543 6544 ```json 6545 { 6546 "string": [ 6547 { 6548 "name": "test", 6549 "value": "10" 6550 } 6551 ] 6552 } 6553 ``` 6554 6555 ```json 6556 { 6557 "string": [ 6558 { 6559 "name": "test", 6560 "value": "%s %d %f" 6561 } 6562 ] 6563 } 6564 ``` 6565 6566- 示例代码中用到的'app.strarray.test'文件内容如下: 6567 6568 ```json 6569 { 6570 "strarray": [ 6571 { 6572 "name": "test", 6573 "value": [ 6574 { 6575 "value": "strarray_test" 6576 } 6577 ] 6578 } 6579 ] 6580 } 6581 ``` 6582 6583- 示例代码中用到的'app.plural.test'文件内容如下: 6584 ```json 6585 { 6586 "plural": [ 6587 { 6588 "name": "test", 6589 "value": [ 6590 { 6591 "quantity": "one", 6592 "value": "%d apple" 6593 }, 6594 { 6595 "quantity": "other", 6596 "value": "%d apples" 6597 } 6598 ] 6599 } 6600 ] 6601 } 6602 ``` 6603 6604- 示例代码中用到的'app.plural.format_test'文件内容如下: 6605 6606 ```json 6607 { 6608 "plural": [ 6609 { 6610 "name": "format_test", 6611 "value": [ 6612 { 6613 "quantity": "one", 6614 "value": "%d apple, %s, %f" 6615 }, 6616 { 6617 "quantity": "other", 6618 "value": "%d apples, %s, %f" 6619 } 6620 ] 6621 } 6622 ] 6623 } 6624 ``` 6625 6626- 示例代码中用到的'app.boolean.boolean_test'文件内容如下: 6627 ```json 6628 { 6629 "boolean": [ 6630 { 6631 "name": "boolean_test", 6632 "value": true 6633 } 6634 ] 6635 } 6636 ``` 6637 6638- 示例代码中用到的"integer_test"和"float_test"文件内容如下: 6639 ```json 6640 { 6641 "integer": [ 6642 { 6643 "name": "integer_test", 6644 "value": 100 6645 } 6646 ] 6647 } 6648 ``` 6649 6650 ```json 6651 { 6652 "float": [ 6653 { 6654 "name": "float_test", 6655 "value": "30.6" 6656 } 6657 ] 6658 } 6659 ``` 6660- 示例代码中用到的'app.color.test'文件内容如下: 6661 ```json 6662 { 6663 "color": [ 6664 { 6665 "name": "test", 6666 "value": "#FFFFFF" 6667 } 6668 ] 6669 } 6670 ```