1# @ohos.resourceManager (Resource Manager) 2 3The Resource Manager module provides APIs to obtain information about application resources based on the current configuration, including the language, region, screen direction, MCC/MNC, as well as device capability and density. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9 10## Modules to Import 11 12```js 13import resourceManager from '@ohos.resourceManager'; 14``` 15 16## Instruction 17 18Since API version 9, the stage model allows an application to obtain a **ResourceManager** object based on **context** and call its resource management APIs without first importing the required bundle. This approach, however, is not applicable to the FA model. For the FA model, you need to import the required bundle and then call the [getResourceManager](#resourcemanagergetresourcemanager) API to obtain a **ResourceManager** object. 19For details about how to reference context in the stage model, see [Context in the Stage Model](../..//application-models/application-context-stage.md). 20 21```ts 22import UIAbility from '@ohos.app.ability.UIAbility'; 23 24export default class EntryAbility extends UIAbility { 25 onWindowStageCreate(windowStage) { 26 let context = this.context; 27 let resourceManager = context.resourceManager; 28 } 29} 30``` 31 32## resourceManager.getResourceManager 33 34getResourceManager(callback: AsyncCallback<ResourceManager>): void 35 36Obtains the **ResourceManager** object of this application. This API uses an asynchronous callback to return the result. 37 38**Model restriction**: This API can be used only in the FA model. 39 40**System capability**: SystemCapability.Global.ResourceManager 41 42**Parameters** 43 44| Name | Type | Mandatory | Description | 45| -------- | ---------------------------------------- | ---- | ----------------------------- | 46| callback | AsyncCallback<[ResourceManager](#resourcemanager)> | Yes | Callback used to return the result.| 47 48**Example** 49 ```js 50 resourceManager.getResourceManager((error, mgr) => { 51 if (error != null) { 52 console.log("error is " + error); 53 return; 54 } 55 mgr.getString(0x1000000, (error, value) => { 56 if (error != null) { 57 console.log("error is " + error); 58 } else { 59 let str = value; 60 } 61 }); 62 }); 63 ``` 64> **NOTE**<br>In the sample code, **0x1000000** indicates the resource ID, which can be found in the compiled **ResourceTable.txt** file. 65 66 67## resourceManager.getResourceManager 68 69getResourceManager(bundleName: string, callback: AsyncCallback<ResourceManager>): void 70 71Obtains the **ResourceManager** object of an application based on the specified bundle name. This API uses an asynchronous callback to return the result. 72 73**Model restriction**: This API can be used only in the FA model. 74 75**System capability**: SystemCapability.Global.ResourceManager 76 77**Parameters** 78 79| Name | Type | Mandatory | Description | 80| ---------- | ---------------------------------------- | ---- | ----------------------------- | 81| bundleName | string | Yes | Bundle name of the application. | 82| callback | AsyncCallback<[ResourceManager](#resourcemanager)> | Yes | Callback used to return the result.| 83 84**Example** 85 ```js 86 resourceManager.getResourceManager("com.example.myapplication", (error, mgr) => { 87 }); 88 ``` 89 90 91## resourceManager.getResourceManager 92 93getResourceManager(): Promise<ResourceManager> 94 95Obtains the **ResourceManager** object of this application. This API uses a promise to return the result. 96 97**Model restriction**: This API can be used only in the FA model. 98 99**System capability**: SystemCapability.Global.ResourceManager 100 101**Return value** 102 103| Type | Description | 104| ---------------------------------------- | ----------------- | 105| Promise<[ResourceManager](#resourcemanager)> | Promise used to return the result.| 106 107**Example** 108 ```js 109 resourceManager.getResourceManager().then(mgr => { 110 mgr.getString(0x1000000, (error, value) => { 111 if (error != null) { 112 console.log("error is " + error); 113 } else { 114 let str = value; 115 } 116 }); 117 }).catch(error => { 118 console.log("error is " + error); 119 }); 120 ``` 121> **NOTE**<br>In the sample code, **0x1000000** indicates the resource ID, which can be found in the compiled **ResourceTable.txt** file. 122 123 124## resourceManager.getResourceManager 125 126getResourceManager(bundleName: string): Promise<ResourceManager> 127 128Obtains the **ResourceManager** object of an application based on the specified bundle name. This API uses a promise to return the result. 129 130**Model restriction**: This API can be used only in the FA model. 131 132**System capability**: SystemCapability.Global.ResourceManager 133 134**Parameters** 135 136| Name | Type | Mandatory | Description | 137| ---------- | ------ | ---- | ------------- | 138| bundleName | string | Yes | Bundle name of the application.| 139 140**Return value** 141 142| Type | Description | 143| ---------------------------------------- | ------------------ | 144| Promise<[ResourceManager](#resourcemanager)> | Promise used to return the result.| 145 146**Example** 147 ```js 148 resourceManager.getResourceManager("com.example.myapplication").then(mgr => { 149 }).catch(error => { 150 }); 151 ``` 152 153 154## Direction 155 156Enumerates the screen directions. 157 158**System capability**: SystemCapability.Global.ResourceManager 159 160| Name | Value | Description | 161| -------------------- | ---- | ---- | 162| DIRECTION_VERTICAL | 0 | Portrait. | 163| DIRECTION_HORIZONTAL | 1 | Landscape. | 164 165 166## DeviceType 167 168Enumerates the device types. 169 170**System capability**: SystemCapability.Global.ResourceManager 171 172| Name | Value | Description | 173| -------------------- | ---- | ---- | 174| DEVICE_TYPE_PHONE | 0x00 | Phone | 175| DEVICE_TYPE_TABLET | 0x01 | Tablet | 176| DEVICE_TYPE_CAR | 0x02 | Head unit | 177| DEVICE_TYPE_PC | 0x03 | PC | 178| DEVICE_TYPE_TV | 0x04 | TV | 179| DEVICE_TYPE_WEARABLE | 0x06 | Wearable | 180 181 182## ScreenDensity 183 184Enumerates the screen density types. 185 186**System capability**: SystemCapability.Global.ResourceManager 187 188| Name | Value | Description | 189| -------------- | ---- | ---------- | 190| SCREEN_SDPI | 120 | Screen density with small-scale dots per inch (SDPI). | 191| SCREEN_MDPI | 160 | Screen density with medium-scale dots per inch (MDPI). | 192| SCREEN_LDPI | 240 | Screen density with large-scale dots per inch (LDPI). | 193| SCREEN_XLDPI | 320 | Screen density with extra-large-scale dots per inch (XLDPI). | 194| SCREEN_XXLDPI | 480 | Screen density with extra-extra-large-scale dots per inch (XXLDPI). | 195| SCREEN_XXXLDPI | 640 | Screen density with extra-extra-extra-large-scale dots per inch (XXXLDPI).| 196 197 198## Configuration 199 200Defines the device configuration. 201 202**System capability**: SystemCapability.Global.ResourceManager 203 204**Parameters** 205 206| Name | Type | Readable | Writable | Description | 207| --------- | ----------------------- | ---- | ---- | -------- | 208| direction | [Direction](#direction) | Yes | No | Screen direction of the device.| 209| locale | string | Yes | No | Current system language. | 210 211**Example** 212 213 ```js 214resourceManager.getResourceManager((error, mgr) => { 215 mgr.getConfiguration((error, value) => { 216 let direction = value.direction; 217 let locale = value.locale; 218 }); 219 }); 220 ``` 221 222## DeviceCapability 223 224Defines the device capability. 225 226**System capability**: SystemCapability.Global.ResourceManager 227 228**Parameters** 229 230| Name | Type | Readable | Writable | Description | 231| ------------- | ------------------------------- | ---- | ---- | -------- | 232| screenDensity | [ScreenDensity](#screendensity) | Yes | No | Screen density of the device.| 233| deviceType | [DeviceType](#devicetype) | Yes | No | Type of the device. | 234 235**Example** 236 237 ```js 238resourceManager.getResourceManager((error, mgr) => { 239 mgr.getDeviceCapability((error, value) => { 240 let screenDensity = value.screenDensity; 241 let deviceType = value.deviceType; 242 }); 243 }); 244 ``` 245 246## RawFileDescriptor<sup>8+</sup> 247 248Defines the descriptor of the raw file. 249 250**System capability**: SystemCapability.Global.ResourceManager 251 252**Parameters** 253 254| Name | Type | Readable | Writable | Description | 255| ------ | ------ | ---- | ---- | ------------------ | 256| fd | number | Yes | No| Descriptor of the raw file.| 257| offset | number | Yes | No| Start offset of the raw file. | 258| length | number | Yes | No| Length of the raw file. | 259 260## Resource<sup>9+</sup> 261 262Defines the resource information of an application. 263 264**System capability**: SystemCapability.Global.ResourceManager 265 266**Parameters** 267 268| Name | Type | Readable | Writable |Description | 269| ---------- | ------ | ----- | ---- | ---------------| 270| bundleName | string | Yes | No| Bundle name of the application.| 271| moduleName | string | Yes | No| Module name of the application.| 272| id | number | Yes | No| Resource ID. | 273 274 275## ResourceManager 276 277Defines the capability of accessing application resources. 278 279> **NOTE** 280> 281> - The APIs involved in **ResourceManager** are applicable only to the TypeScript-based declarative development paradigm. 282> 283> - Resource files are defined in the **resources** directory of the project. You can obtain the resource ID using **$r(resource address).id**, for example, **$r('app.string.test').id**. 284 285### getStringValue<sup>9+</sup> 286 287getStringValue(resId: number, callback: AsyncCallback<string>): void 288 289Obtains the string corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 290 291**System capability**: SystemCapability.Global.ResourceManager 292 293**Parameters** 294 295| Name | Type | Mandatory | Description | 296| -------- | --------------------------- | ---- | --------------- | 297| resId | number | Yes | Resource ID. | 298| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 299 300**Error codes** 301 302For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 303 304| ID| Error Message| 305| -------- | ---------------------------------------- | 306| 9001001 | If the module resId invalid. | 307| 9001002 | If the resource not found by resId. | 308| 9001006 | If the resource re-ref too much. | 309 310**Example (stage)** 311 ```ts 312 try { 313 this.context.resourceManager.getStringValue($r('app.string.test').id, (error, value) => { 314 if (error != null) { 315 console.log("error is " + error); 316 } else { 317 let str = value; 318 } 319 }); 320 } catch (error) { 321 console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`) 322 } 323 ``` 324 325 326### getStringValue<sup>9+</sup> 327 328getStringValue(resId: number): Promise<string> 329 330Obtains the string corresponding to the specified resource ID. This API uses a promise to return the result. 331 332**System capability**: SystemCapability.Global.ResourceManager 333 334**Parameters** 335 336| Name | Type | Mandatory | Description | 337| ----- | ------ | ---- | ----- | 338| resId | number | Yes | Resource ID.| 339 340**Return value** 341 342| Type | Description | 343| --------------------- | ----------- | 344| Promise<string> | Promise used to return the result.| 345 346For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 347 348**Error codes** 349 350| ID| Error Message| 351| -------- | ---------------------------------------- | 352| 9001001 | If the resId invalid. | 353| 9001002 | If the resource not found by resId. | 354| 9001006 | If the resource re-ref too much. | 355 356**Example** 357 ```ts 358 try { 359 this.context.resourceManager.getStringValue($r('app.string.test').id).then(value => { 360 let str = value; 361 }).catch(error => { 362 console.log("getStringValue promise error is " + error); 363 }); 364 } catch (error) { 365 console.error(`promise getStringValue failed, error code: ${error.code}, message: ${error.message}.`) 366 } 367 ``` 368 369 370### getStringValue<sup>9+</sup> 371 372getStringValue(resource: Resource, callback: AsyncCallback<string>): void 373 374Obtains the string corresponding to the specified resource object. This API uses an asynchronous callback to return the result. 375 376**System capability**: SystemCapability.Global.ResourceManager 377 378**Parameters** 379 380| Name | Type | Mandatory | Description | 381| -------- | --------------------------- | ---- | --------------- | 382| resource | [Resource](#resource9) | Yes | Resource object. | 383| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 384 385For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 386 387**Error codes** 388 389| ID| Error Message| 390| -------- | ---------------------------------------- | 391| 9001001 | If the resId invalid. | 392| 9001002 | If the resource not found by resId. | 393| 9001006 | If the resource re-ref too much. | 394 395**Example** 396 ```ts 397 let resource = { 398 bundleName: "com.example.myapplication", 399 moduleName: "entry", 400 id: $r('app.string.test').id 401 }; 402 try { 403 this.context.resourceManager.getStringValue(resource, (error, value) => { 404 if (error != null) { 405 console.log("error is " + error); 406 } else { 407 let str = value; 408 } 409 }); 410 } catch (error) { 411 console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`) 412 } 413 414 ``` 415 416 417### getStringValue<sup>9+</sup> 418 419getStringValue(resource: Resource): Promise<string> 420 421Obtains the string corresponding to the specified resource object. This API uses a promise to return the result. 422 423**System capability**: SystemCapability.Global.ResourceManager 424 425**Parameters** 426 427| Name | Type | Mandatory | Description | 428| -------- | ---------------------- | ---- | ---- | 429| resource | [Resource](#resource9) | Yes | Resource object.| 430 431**Return value** 432 433| Type | Description | 434| --------------------- | ---------------- | 435| Promise<string> | Promise used to return the result.| 436 437For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 438 439**Error codes** 440 441| ID| Error Message| 442| -------- | ---------------------------------------- | 443| 9001001 | If the resId invalid. | 444| 9001002 | If the resource not found by resId. | 445| 9001006 | If the resource re-ref too much. | 446 447**Example** 448 ```ts 449 let resource = { 450 bundleName: "com.example.myapplication", 451 moduleName: "entry", 452 id: $r('app.string.test').id 453 }; 454 try { 455 this.context.resourceManager.getStringValue(resource).then(value => { 456 let str = value; 457 }).catch(error => { 458 console.log("getStringValue promise error is " + error); 459 }); 460 } catch (error) { 461 console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`) 462 } 463 ``` 464 465 466### getStringArrayValue<sup>9+</sup> 467 468getStringArrayValue(resId: number, callback: AsyncCallback<Array<string>>): void 469 470Obtains the string array corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 471 472**System capability**: SystemCapability.Global.ResourceManager 473 474**Parameters** 475 476| Name | Type | Mandatory | Description | 477| -------- | ---------------------------------------- | ---- | ----------------- | 478| resId | number | Yes | Resource ID. | 479| callback | AsyncCallback<Array<string>> | Yes | Callback used to return the result.| 480 481For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 482 483**Error codes** 484 485| ID| Error Message| 486| -------- | ---------------------------------------- | 487| 9001001 | If the resId invalid. | 488| 9001002 | If the resource not found by resId. | 489| 9001006 | If the resource re-ref too much. | 490 491**Example** 492 ```ts 493 try { 494 this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id, (error, value) => { 495 if (error != null) { 496 console.log("error is " + error); 497 } else { 498 let strArray = value; 499 } 500 }); 501 } catch (error) { 502 console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`) 503 } 504 ``` 505 506 507### getStringArrayValue<sup>9+</sup> 508 509getStringArrayValue(resId: number): Promise<Array<string>> 510 511Obtains the string array corresponding to the specified resource ID. This API uses a promise to return the result. 512 513**System capability**: SystemCapability.Global.ResourceManager 514 515**Parameters** 516 517| Name | Type | Mandatory | Description | 518| ----- | ------ | ---- | ----- | 519| resId | number | Yes | Resource ID.| 520 521**Return value** 522 523| Type | Description | 524| ---------------------------------- | ------------- | 525| Promise<Array<string>> | Promise used to return the result.| 526 527For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 528 529**Error codes** 530 531| ID| Error Message| 532| -------- | ---------------------------------------- | 533| 9001001 | If the resId invalid. | 534| 9001002 | If the resource not found by resId. | 535| 9001006 | If the resource re-ref too much. | 536 537**Example** 538 ```ts 539 try { 540 this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id).then(value => { 541 let strArray = value; 542 }).catch(error => { 543 console.log("getStringArrayValue promise error is " + error); 544 }); 545 } catch (error) { 546 console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`) 547 } 548 ``` 549 550### getStringArrayValue<sup>9+</sup> 551 552getStringArrayValue(resource: Resource, callback: AsyncCallback<Array<string>>): void 553 554Obtains the string array corresponding to the specified resource object. This API uses an asynchronous callback to return the result. 555 556**System capability**: SystemCapability.Global.ResourceManager 557 558**Parameters** 559 560| Name | Type | Mandatory | Description | 561| -------- | ---------------------------------------- | ---- | ----------------- | 562| resource | [Resource](#resource9) | Yes | Resource object. | 563| callback | AsyncCallback<Array<string>> | Yes | Callback used to return the result.| 564 565For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 566 567**Error codes** 568 569| ID| Error Message| 570| -------- | ---------------------------------------- | 571| 9001001 | If the resId invalid. | 572| 9001002 | If the resource not found by resId. | 573| 9001006 | If the resource re-ref too much. | 574 575**Example** 576 ```ts 577 let resource = { 578 bundleName: "com.example.myapplication", 579 moduleName: "entry", 580 id: $r('app.strarray.test').id 581 }; 582 try { 583 this.context.resourceManager.getStringArrayValue(resource, (error, value) => { 584 if (error != null) { 585 console.log("error is " + error); 586 } else { 587 let strArray = value; 588 } 589 }); 590 } catch (error) { 591 console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`) 592 } 593 ``` 594 595### getStringArrayValue<sup>9+</sup> 596 597getStringArrayValue(resource: Resource): Promise<Array<string>> 598 599Obtains the string array corresponding to the specified resource object. This API uses a promise to return the result. 600 601**System capability**: SystemCapability.Global.ResourceManager 602 603**Parameters** 604 605| Name | Type | Mandatory | Description | 606| -------- | ---------------------- | ---- | ---- | 607| resource | [Resource](#resource9) | Yes | Resource object.| 608 609**Return value** 610 611| Type | Description | 612| ---------------------------------- | ------------------ | 613| Promise<Array<string>> | Promise used to return the result.| 614 615For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 616 617**Error codes** 618 619| ID| Error Message| 620| -------- | ---------------------------------------- | 621| 9001001 | If the resId invalid. | 622| 9001002 | If the resource not found by resId. | 623| 9001006 | If the resource re-ref too much. | 624 625**Example** 626 ```ts 627 let resource = { 628 bundleName: "com.example.myapplication", 629 moduleName: "entry", 630 id: $r('app.strarray.test').id 631 }; 632 try { 633 this.context.resourceManager.getStringArrayValue(resource).then(value => { 634 let strArray = value; 635 }).catch(error => { 636 console.log("getStringArray promise error is " + error); 637 }); 638 } catch (error) { 639 console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`) 640 } 641 ``` 642 643 644### getMediaContent<sup>9+</sup> 645 646getMediaContent(resId: number, callback: AsyncCallback<Uint8Array>): void 647 648Obtains the content of the media file corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 649 650**System capability**: SystemCapability.Global.ResourceManager 651 652**Parameters** 653 654| Name | Type | Mandatory | Description | 655| -------- | ------------------------------- | ---- | ------------------ | 656| resId | number | Yes | Resource ID. | 657| callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| 658 659For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 660 661**Error codes** 662 663| ID| Error Message| 664| -------- | ---------------------------------------- | 665| 9001001 | If the resId invalid. | 666| 9001002 | If the resource not found by resId. | 667 668**Example** 669 ```ts 670 try { 671 this.context.resourceManager.getMediaContent($r('app.media.test').id, (error, value) => { 672 if (error != null) { 673 console.log("error is " + error); 674 } else { 675 let media = value; 676 } 677 }); 678 } catch (error) { 679 console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`) 680 } 681 ``` 682 683 684### getMediaContent<sup>9+</sup> 685 686getMediaContent(resId: number): Promise<Uint8Array> 687 688Obtains the content of the media file corresponding to the specified resource ID. This API uses a promise to return the result. 689 690**System capability**: SystemCapability.Global.ResourceManager 691 692**Parameters** 693 694| Name | Type | Mandatory | Description | 695| ----- | ------ | ---- | ----- | 696| resId | number | Yes | Resource ID.| 697 698**Return value** 699 700| Type | Description | 701| ------------------------- | -------------- | 702| Promise<Uint8Array> | Promise used to return the result.| 703 704For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 705 706**Error codes** 707 708| ID| Error Message| 709| -------- | ---------------------------------------- | 710| 9001001 | If the resId invalid. | 711| 9001002 | If the resource not found by resId. | 712 713**Example** 714 ```ts 715 try { 716 this.context.resourceManager.getMediaContent($r('app.media.test').id).then(value => { 717 let media = value; 718 }).catch(error => { 719 console.log("getMediaContent promise error is " + error); 720 }); 721 } catch (error) { 722 console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`) 723 } 724 ``` 725 726### getMediaContent<sup>9+</sup> 727 728getMediaContent(resource: Resource, callback: AsyncCallback<Uint8Array>): void 729 730Obtains the content of the media file corresponding to the specified resource object. This API uses an asynchronous callback to return the result. 731 732**System capability**: SystemCapability.Global.ResourceManager 733 734**Parameters** 735 736| Name | Type | Mandatory | Description | 737| -------- | ------------------------------- | ---- | ------------------ | 738| resource | [Resource](#resource9) | Yes | Resource object. | 739| callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| 740 741For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 742 743**Error codes** 744 745| ID| Error Message| 746| -------- | ---------------------------------------- | 747| 9001001 | If the resId invalid. | 748| 9001002 | If the resource not found by resId. | 749 750**Example** 751 ```ts 752 let resource = { 753 bundleName: "com.example.myapplication", 754 moduleName: "entry", 755 id: $r('app.media.test').id 756 }; 757 try { 758 this.context.resourceManager.getMediaContent(resource, (error, value) => { 759 if (error != null) { 760 console.log("error is " + error); 761 } else { 762 let media = value; 763 } 764 }); 765 } catch (error) { 766 console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`) 767 } 768 ``` 769 770### getMediaContent<sup>9+</sup> 771 772getMediaContent(resource: Resource): Promise<Uint8Array> 773 774Obtains the content of the media file corresponding to the specified resource object. This API uses a promise to return the result. 775 776**System capability**: SystemCapability.Global.ResourceManager 777 778**Parameters** 779 780| Name | Type | Mandatory | Description | 781| -------- | ---------------------- | ---- | ---- | 782| resource | [Resource](#resource9) | Yes | Resource object.| 783 784**Return value** 785 786| Type | Description | 787| ------------------------- | ------------------- | 788| Promise<Uint8Array> | Promise used to return the result.| 789 790For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 791 792**Error codes** 793 794| ID| Error Message| 795| -------- | ---------------------------------------- | 796| 9001001 | If the resId invalid. | 797| 9001002 | If the resource not found by resId. | 798 799**Example** 800 ```ts 801 let resource = { 802 bundleName: "com.example.myapplication", 803 moduleName: "entry", 804 id: $r('app.media.test').id 805 }; 806 try { 807 this.context.resourceManager.getMediaContent(resource).then(value => { 808 let media = value; 809 }).catch(error => { 810 console.log("getMediaContent promise error is " + error); 811 }); 812 } catch (error) { 813 console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`) 814 } 815 ``` 816 817 818### getMediaContentBase64<sup>9+</sup> 819 820getMediaContentBase64(resId: number, callback: AsyncCallback<string>): void 821 822Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 823 824**System capability**: SystemCapability.Global.ResourceManager 825 826**Parameters** 827 828| Name | Type | Mandatory | Description | 829| -------- | --------------------------- | ---- | ------------------------ | 830| resId | number | Yes | Resource ID. | 831| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 832 833For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 834 835**Error codes** 836 837| ID| Error Message| 838| -------- | ---------------------------------------- | 839| 9001001 | If the resId invalid. | 840| 9001002 | If the resource not found by resId. | 841 842**Example** 843 ```ts 844 try { 845 this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, (error, value) => { 846 if (error != null) { 847 console.log("error is " + error); 848 } else { 849 let media = value; 850 } 851 }); 852 } catch (error) { 853 console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`) 854 } 855 ``` 856 857 858### getMediaContentBase64<sup>9+</sup> 859 860getMediaContentBase64(resId: number): Promise<string> 861 862Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses a promise to return the result. 863 864**System capability**: SystemCapability.Global.ResourceManager 865 866**Parameters** 867 868| Name | Type | Mandatory | Description | 869| ----- | ------ | ---- | ----- | 870| resId | number | Yes | Resource ID.| 871 872**Return value** 873 874| Type | Description | 875| --------------------- | -------------------- | 876| Promise<string> | Promise used to return the result.| 877 878For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 879 880**Error codes** 881 882| ID| Error Message| 883| -------- | ---------------------------------------- | 884| 9001001 | If the resId invalid. | 885| 9001002 | If the resource not found by resId. | 886 887**Example** 888 ```ts 889 try { 890 this.context.resourceManager.getMediaContentBase64($r('app.media.test').id).then(value => { 891 let media = value; 892 }).catch(error => { 893 console.log("getMediaContentBase64 promise error is " + error); 894 }); 895 } catch (error) { 896 console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`) 897 } 898 ``` 899 900### getMediaContentBase64<sup>9+</sup> 901 902getMediaContentBase64(resource: Resource, callback: AsyncCallback<string>): void 903 904Obtains the Base64 code of the image corresponding to the specified resource object. This API uses an asynchronous callback to return the result. 905 906**System capability**: SystemCapability.Global.ResourceManager 907 908**Parameters** 909 910| Name | Type | Mandatory | Description | 911| -------- | --------------------------- | ---- | ------------------------ | 912| resource | [Resource](#resource9) | Yes | Resource object. | 913| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 914 915For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 916 917**Error codes** 918 919| ID| Error Message| 920| -------- | ---------------------------------------- | 921| 9001001 | If the resId invalid. | 922| 9001002 | If the resource not found by resId. | 923 924**Example** 925 ```ts 926 let resource = { 927 bundleName: "com.example.myapplication", 928 moduleName: "entry", 929 id: $r('app.media.test').id 930 }; 931 try { 932 this.context.resourceManager.getMediaContentBase64(resource, (error, value) => { 933 if (error != null) { 934 console.log("error is " + error); 935 } else { 936 let media = value; 937 } 938 }); 939 } catch (error) { 940 console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`) 941 } 942 ``` 943 944### getMediaContentBase64<sup>9+</sup> 945 946getMediaContentBase64(resource: Resource): Promise<string> 947 948Obtains the Base64 code of the image corresponding to the specified resource object. This API uses a promise to return the result. 949 950**System capability**: SystemCapability.Global.ResourceManager 951 952**Parameters** 953 954| Name | Type | Mandatory | Description | 955| -------- | ---------------------- | ---- | ---- | 956| resource | [Resource](#resource9) | Yes | Resource object.| 957 958**Return value** 959 960| Type | Description | 961| --------------------- | ------------------------- | 962| Promise<string> | Promise used to return the result.| 963 964For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 965 966**Error codes** 967 968| ID| Error Message| 969| -------- | ---------------------------------------- | 970| 9001001 | If the resId invalid. | 971| 9001002 | If the resource not found by resId. | 972 973**Example** 974 ```ts 975 let resource = { 976 bundleName: "com.example.myapplication", 977 moduleName: "entry", 978 id: $r('app.media.test').id 979 }; 980 try { 981 this.context.resourceManager.getMediaContentBase64(resource).then(value => { 982 let media = value; 983 }).catch(error => { 984 console.log("getMediaContentBase64 promise error is " + error); 985 }); 986 } catch (error) { 987 console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`) 988 } 989 ``` 990 991 992### getConfiguration 993 994getConfiguration(callback: AsyncCallback<Configuration>): void 995 996Obtains the device configuration. This API uses an asynchronous callback to return the result. 997 998**System capability**: SystemCapability.Global.ResourceManager 999 1000**Parameters** 1001 1002| Name | Type | Mandatory | Description | 1003| -------- | ---------------------------------------- | ---- | ------------------------- | 1004| callback | AsyncCallback<[Configuration](#configuration)> | Yes | Callback used to return the result.| 1005 1006**Example** 1007 ```ts 1008 resourceManager.getResourceManager((error, mgr) => { 1009 mgr.getConfiguration((error, value) => { 1010 if (error != null) { 1011 console.log("error is " + error); 1012 } else { 1013 let direction = value.direction; 1014 let locale = value.locale; 1015 } 1016 }); 1017 }); 1018 ``` 1019 1020 1021### getConfiguration 1022 1023getConfiguration(): Promise<Configuration> 1024 1025Obtains the device configuration. This API uses a promise to return the result. 1026 1027**System capability**: SystemCapability.Global.ResourceManager 1028 1029**Return value** 1030 1031| Type | Description | 1032| ---------------------------------------- | ---------------- | 1033| Promise<[Configuration](#configuration)> | Promise used to return the result.| 1034 1035**Example** 1036 ```ts 1037 resourceManager.getResourceManager((error, mgr) => { 1038 mgr.getConfiguration().then(value => { 1039 let direction = value.direction; 1040 let locale = value.locale; 1041 }).catch(error => { 1042 console.log("getConfiguration promise error is " + error); 1043 }); 1044 }); 1045 ``` 1046 1047 1048### getDeviceCapability 1049 1050getDeviceCapability(callback: AsyncCallback<DeviceCapability>): void 1051 1052Obtains the device capability. This API uses an asynchronous callback to return the result. 1053 1054**System capability**: SystemCapability.Global.ResourceManager 1055 1056**Parameters** 1057 1058| Name | Type | Mandatory | Description | 1059| -------- | ---------------------------------------- | ---- | ---------------------------- | 1060| callback | AsyncCallback<[DeviceCapability](#devicecapability)> | Yes | Callback used to return the result.| 1061 1062**Example** 1063 ```ts 1064 resourceManager.getResourceManager((error, mgr) => { 1065 mgr.getDeviceCapability((error, value) => { 1066 if (error != null) { 1067 console.log("error is " + error); 1068 } else { 1069 let screenDensity = value.screenDensity; 1070 let deviceType = value.deviceType; 1071 } 1072 }); 1073 }); 1074 ``` 1075 1076 1077### getDeviceCapability 1078 1079getDeviceCapability(): Promise<DeviceCapability> 1080 1081Obtains the device capability. This API uses a promise to return the result. 1082 1083**System capability**: SystemCapability.Global.ResourceManager 1084 1085**Return value** 1086 1087| Type | Description | 1088| ---------------------------------------- | ------------------- | 1089| Promise<[DeviceCapability](#devicecapability)> | Promise used to return the result.| 1090 1091**Example** 1092 ```ts 1093 resourceManager.getResourceManager((error, mgr) => { 1094 mgr.getDeviceCapability().then(value => { 1095 let screenDensity = value.screenDensity; 1096 let deviceType = value.deviceType; 1097 }).catch(error => { 1098 console.log("getDeviceCapability promise error is " + error); 1099 }); 1100 }); 1101 ``` 1102 1103 1104### getPluralStringValue<sup>9+</sup> 1105 1106getPluralStringValue(resId: number, num: number, callback: AsyncCallback<string>): void 1107 1108Obtains the singular-plural string corresponding to the specified resource ID based on the specified number. This API uses an asynchronous callback to return the result. 1109 1110**System capability**: SystemCapability.Global.ResourceManager 1111 1112**Parameters** 1113 1114| Name | Type | Mandatory | Description | 1115| -------- | --------------------------- | ---- | ------------------------------- | 1116| resId | number | Yes | Resource ID. | 1117| num | number | Yes | Number. | 1118| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 1119 1120For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1121 1122**Error codes** 1123 1124| ID| Error Message| 1125| -------- | ---------------------------------------- | 1126| 9001001 | If the resId invalid. | 1127| 9001002 | If the resource not found by resId. | 1128| 9001006 | If the resource re-ref too much. | 1129 1130**Example** 1131 ```ts 1132 try { 1133 this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1, (error, value) => { 1134 if (error != null) { 1135 console.log("error is " + error); 1136 } else { 1137 let str = value; 1138 } 1139 }); 1140 } catch (error) { 1141 console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`) 1142 } 1143 ``` 1144 1145 1146### getPluralStringValue<sup>9+</sup> 1147 1148getPluralStringValue(resId: number, num: number): Promise<string> 1149 1150Obtains the singular-plural string corresponding to the specified resource ID based on the specified number. This API uses a promise to return the result. 1151 1152**System capability**: SystemCapability.Global.ResourceManager 1153 1154**Parameters** 1155 1156| Name | Type | Mandatory | Description | 1157| ----- | ------ | ---- | ----- | 1158| resId | number | Yes | Resource ID.| 1159| num | number | Yes | Number. | 1160 1161**Return value** 1162 1163| Type | Description | 1164| --------------------- | ------------------------- | 1165| Promise<string> | Promise used to return the result.| 1166 1167For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1168 1169**Error codes** 1170 1171| ID| Error Message| 1172| -------- | ---------------------------------------- | 1173| 9001001 | If the resId invalid. | 1174| 9001002 | If the resource not found by resId. | 1175| 9001006 | If the resource re-ref too much. | 1176 1177**Example** 1178 ```ts 1179 try { 1180 this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1).then(value => { 1181 let str = value; 1182 }).catch(error => { 1183 console.log("getPluralStringValue promise error is " + error); 1184 }); 1185 } catch (error) { 1186 console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`) 1187 } 1188 ``` 1189 1190### getPluralStringValue<sup>9+</sup> 1191 1192getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback<string>): void 1193 1194Obtains the singular-plural string corresponding to the specified resource object based on the specified number. This API uses an asynchronous callback to return the result. 1195 1196**System capability**: SystemCapability.Global.ResourceManager 1197 1198**Parameters** 1199 1200| Name | Type | Mandatory | Description | 1201| -------- | --------------------------- | ---- | ------------------------------------ | 1202| resource | [Resource](#resource9) | Yes | Resource object. | 1203| num | number | Yes | Number. | 1204| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 1205 1206For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1207 1208**Error codes** 1209 1210| ID| Error Message| 1211| -------- | ---------------------------------------- | 1212| 9001001 | If the resId invalid. | 1213| 9001002 | If the resource not found by resId. | 1214| 9001006 | If the resource re-ref too much. | 1215 1216**Example** 1217 ```ts 1218 let resource = { 1219 bundleName: "com.example.myapplication", 1220 moduleName: "entry", 1221 id: $r('app.plural.test').id 1222 }; 1223 try { 1224 this.context.resourceManager.getPluralStringValue(resource, 1, (error, value) => { 1225 if (error != null) { 1226 console.log("error is " + error); 1227 } else { 1228 let str = value; 1229 } 1230 }); 1231 } catch (error) { 1232 console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`) 1233 } 1234 1235 ``` 1236 1237### getPluralStringValue<sup>9+</sup> 1238 1239getPluralStringValue(resource: Resource, num: number): Promise<string> 1240 1241Obtains the singular-plural string corresponding to the specified resource object based on the specified number. This API uses a promise to return the result. 1242 1243**System capability**: SystemCapability.Global.ResourceManager 1244 1245**Parameters** 1246 1247| Name | Type | Mandatory | Description | 1248| -------- | ---------------------- | ---- | ---- | 1249| resource | [Resource](#resource9) | Yes | Resource object.| 1250| num | number | Yes | Number. | 1251 1252**Return value** 1253 1254| Type | Description | 1255| --------------------- | ------------------------------ | 1256| Promise<string> | Promise used to return the result.| 1257 1258For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1259 1260**Error codes** 1261 1262| ID| Error Message| 1263| -------- | ---------------------------------------- | 1264| 9001001 | If the resId invalid. | 1265| 9001002 | If the resource not found by resId. | 1266| 9001006 | If the resource re-ref too much. | 1267 1268**Example** 1269 ```ts 1270 let resource = { 1271 bundleName: "com.example.myapplication", 1272 moduleName: "entry", 1273 id: $r('app.plural.test').id 1274 }; 1275 try { 1276 this.context.resourceManager.getPluralStringValue(resource, 1).then(value => { 1277 let str = value; 1278 }).catch(error => { 1279 console.log("getPluralStringValue promise error is " + error); 1280 }); 1281 } catch (error) { 1282 console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`) 1283 } 1284 ``` 1285 1286 1287### getRawFileContent<sup>9+</sup> 1288 1289getRawFileContent(path: string, callback: AsyncCallback<Uint8Array>): void 1290 1291Obtains the content of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. 1292 1293**System capability**: SystemCapability.Global.ResourceManager 1294 1295**Parameters** 1296 1297| Name | Type | Mandatory | Description | 1298| -------- | ------------------------------- | ---- | ----------------------- | 1299| path | string | Yes | Path of the raw file. | 1300| callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| 1301 1302For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1303 1304**Error codes** 1305 1306| ID| Error Message| 1307| -------- | ---------------------------------------- | 1308| 9001005 | If the resource not found by path. | 1309 1310**Example** 1311 ```ts 1312 try { 1313 this.context.resourceManager.getRawFileContent("test.xml", (error, value) => { 1314 if (error != null) { 1315 console.log("error is " + error); 1316 } else { 1317 let rawFile = value; 1318 } 1319 }); 1320 } catch (error) { 1321 console.error(`callback getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`) 1322 } 1323 1324 ``` 1325 1326### getRawFileContent<sup>9+</sup> 1327 1328getRawFileContent(path: string): Promise<Uint8Array> 1329 1330Obtains the content of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. 1331 1332**System capability**: SystemCapability.Global.ResourceManager 1333 1334**Parameters** 1335 1336| Name | Type | Mandatory | Description | 1337| ---- | ------ | ---- | ----------- | 1338| path | string | Yes | Path of the raw file.| 1339 1340**Return value** 1341 1342| Type | Description | 1343| ------------------------- | ----------- | 1344| Promise<Uint8Array> | Promise used to return the result.| 1345 1346For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1347 1348**Error codes** 1349 1350| ID| Error Message| 1351| -------- | ---------------------------------------- | 1352| 9001005 | If the resource not found by path. | 1353 1354**Example** 1355 ```ts 1356 try { 1357 this.context.resourceManager.getRawFileContent("test.xml").then(value => { 1358 let rawFile = value; 1359 }).catch(error => { 1360 console.log("getRawFileContent promise error is " + error); 1361 }); 1362 } catch (error) { 1363 console.error(`promise getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`) 1364 } 1365 ``` 1366 1367 1368### getRawFd<sup>9+</sup> 1369 1370getRawFd(path: string, callback: AsyncCallback<RawFileDescriptor>): void 1371 1372Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. 1373 1374**System capability**: SystemCapability.Global.ResourceManager 1375 1376**Parameters** 1377 1378| Name | Type | Mandatory | Description | 1379| -------- | ---------------------------------------- | ---- | -------------------------------- | 1380| path | string | Yes | Path of the raw file. | 1381| callback | AsyncCallback<[RawFileDescriptor](#rawfiledescriptor8)> | Yes | Callback used to return the result.| 1382 1383For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1384 1385**Error codes** 1386 1387| ID| Error Message| 1388| -------- | ---------------------------------------- | 1389| 9001005 | If the resource not found by path. | 1390 1391**Example** 1392 ```ts 1393 try { 1394 this.context.resourceManager.getRawFd("test.xml", (error, value) => { 1395 if (error != null) { 1396 console.log(`callback getRawFd failed error code: ${error.code}, message: ${error.message}.`); 1397 } else { 1398 let fd = value.fd; 1399 let offset = value.offset; 1400 let length = value.length; 1401 } 1402 }); 1403 } catch (error) { 1404 console.error(`callback getRawFd failed, error code: ${error.code}, message: ${error.message}.`) 1405 }; 1406 ``` 1407 1408### getRawFd<sup>9+</sup> 1409 1410getRawFd(path: string): Promise<RawFileDescriptor> 1411 1412Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. 1413 1414**System capability**: SystemCapability.Global.ResourceManager 1415 1416**Parameters** 1417 1418| Name | Type | Mandatory | Description | 1419| ---- | ------ | ---- | ----------- | 1420| path | string | Yes | Path of the raw file.| 1421 1422**Return value** 1423 1424| Type | Description | 1425| ---------------------------------------- | ------------------- | 1426| Promise<[RawFileDescriptor](#rawfiledescriptor8)> | Promise used to return the result.| 1427 1428For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1429 1430**Error codes** 1431 1432| ID| Error Message| 1433| -------- | ---------------------------------------- | 1434| 9001005 | If the resource not found by path. | 1435 1436**Example** 1437 ```ts 1438 try { 1439 this.context.resourceManager.getRawFd("test.xml").then(value => { 1440 let fd = value.fd; 1441 let offset = value.offset; 1442 let length = value.length; 1443 }).catch(error => { 1444 console.log(`promise getRawFd error error code: ${error.code}, message: ${error.message}.`); 1445 }); 1446 } catch (error) { 1447 console.error(`promise getRawFd failed, error code: ${error.code}, message: ${error.message}.`); 1448 }; 1449 ``` 1450 1451### closeRawFileDescriptor<sup>8+</sup> 1452 1453closeRawFileDescriptor(path: string, callback: AsyncCallback<void>): void 1454 1455Closes the descriptor of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. 1456 1457**System capability**: SystemCapability.Global.ResourceManager 1458 1459**Parameters** 1460 1461| Name | Type | Mandatory | Description | 1462| -------- | ------------------------- | ---- | ----------- | 1463| path | string | Yes | Path of the raw file.| 1464| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 1465 1466**Example** 1467 ```ts 1468 resourceManager.getResourceManager((error, mgr) => { 1469 mgr.closeRawFileDescriptor("test.xml", (error, value) => { 1470 if (error != null) { 1471 console.log("error is " + error); 1472 } 1473 }); 1474 }); 1475 ``` 1476 1477### closeRawFileDescriptor<sup>8+</sup> 1478 1479closeRawFileDescriptor(path: string): Promise<void> 1480 1481Closes the descriptor of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. 1482 1483**System capability**: SystemCapability.Global.ResourceManager 1484 1485**Parameters** 1486 1487| Name | Type | Mandatory | Description | 1488| ---- | ------ | ---- | ----------- | 1489| path | string | Yes | Path of the raw file.| 1490 1491**Return value** 1492 1493| Type | Description | 1494| ------------------- | ---- | 1495| Promise<void> | Promise that returns no value.| 1496 1497**Example** 1498 ```ts 1499 resourceManager.getResourceManager((error, mgr) => { 1500 mgr.closeRawFileDescriptor("test.xml").then(value => { 1501 let result = value; 1502 }).catch(error => { 1503 console.log("closeRawFileDescriptor promise error is " + error); 1504 }); 1505 }); 1506 ``` 1507 1508 1509### closeRawFd<sup>9+</sup> 1510 1511closeRawFd(path: string, callback: AsyncCallback<void>): void 1512 1513Closes the descriptor of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. 1514 1515**System capability**: SystemCapability.Global.ResourceManager 1516 1517**Parameters** 1518 1519| Name | Type | Mandatory | Description | 1520| -------- | ------------------------- | ---- | ----------- | 1521| path | string | Yes | Path of the raw file.| 1522| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 1523 1524For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1525 1526**Error codes** 1527 1528| ID| Error Message| 1529| -------- | ---------------------------------------- | 1530| 9001005 | The resource not found by path. | 1531 1532**Example** 1533 ```ts 1534 try { 1535 this.context.resourceManager.closeRawFd("test.xml", (error, value) => { 1536 if (error != null) { 1537 console.log("error is " + error); 1538 } 1539 }); 1540 } catch (error) { 1541 console.error(`callback closeRawFd failed, error code: ${error.code}, message: ${error.message}.`) 1542 } 1543 1544 ``` 1545 1546### closeRawFd<sup>9+</sup> 1547 1548closeRawFd(path: string): Promise<void> 1549 1550Closes the descriptor of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. 1551 1552**System capability**: SystemCapability.Global.ResourceManager 1553 1554**Parameters** 1555 1556| Name | Type | Mandatory | Description | 1557| ---- | ------ | ---- | ----------- | 1558| path | string | Yes | Path of the raw file.| 1559 1560**Return value** 1561 1562| Type | Description | 1563| ------------------- | ---- | 1564| Promise<void> | Promise that returns no value.| 1565 1566For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1567 1568**Error codes** 1569 1570| ID| Error Message| 1571| -------- | ---------------------------------------- | 1572| 9001005 | If the resource not found by path. | 1573 1574**Example** 1575 ```ts 1576 try { 1577 this.context.resourceManager.closeRawFd("test.xml").then(value => { 1578 let result = value; 1579 }).catch(error => { 1580 console.log("closeRawFd promise error is " + error); 1581 }); 1582 } catch (error) { 1583 console.error(`promise closeRawFd failed, error code: ${error.code}, message: ${error.message}.`) 1584 } 1585 ``` 1586 1587### release<sup>7+</sup> 1588 1589release() 1590 1591Releases a created **resourceManager** object. 1592 1593**System capability**: SystemCapability.Global.ResourceManager 1594 1595**Example** 1596 ```ts 1597 resourceManager.getResourceManager((error, mgr) => { 1598 mgr.release(); 1599 }); 1600 ``` 1601 1602### getStringByName<sup>9+</sup> 1603 1604getStringByName(resName: string, callback: AsyncCallback<string>): void 1605 1606Obtains the string corresponding to the specified resource name. This API uses an asynchronous callback to return the result. 1607 1608**System capability**: SystemCapability.Global.ResourceManager 1609 1610**Parameters** 1611 1612| Name | Type | Mandatory | Description | 1613| -------- | --------------------------- | ---- | --------------- | 1614| resName | string | Yes | Resource name. | 1615| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 1616 1617For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1618 1619**Error codes** 1620 1621| ID| Error Message| 1622| -------- | ---------------------------------------- | 1623| 9001003 | If the resName invalid. | 1624| 9001004 | If the resource not found by resName. | 1625| 9001006 | If the resource re-ref too much. | 1626 1627**Example** 1628 ```ts 1629 try { 1630 this.context.resourceManager.getStringByName("test", (error, value) => { 1631 if (error != null) { 1632 console.log("error is " + error); 1633 } else { 1634 let string = value; 1635 } 1636 }); 1637 } catch (error) { 1638 console.error(`callback getStringByName failed, error code: ${error.code}, message: ${error.message}.`) 1639 } 1640 1641 ``` 1642 1643### getStringByName<sup>9+</sup> 1644 1645getStringByName(resName: string): Promise<string> 1646 1647Obtains the string corresponding to the specified resource name. This API uses a promise to return the result. 1648 1649**System capability**: SystemCapability.Global.ResourceManager 1650 1651**Parameters** 1652 1653| Name | Type | Mandatory | Description | 1654| ------- | ------ | ---- | ---- | 1655| resName | string | Yes | Resource name.| 1656 1657**Return value** 1658 1659| Type | Description | 1660| --------------------- | ---------- | 1661| Promise<string> | String corresponding to the resource name.| 1662 1663For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1664 1665**Error codes** 1666 1667| ID| Error Message| 1668| -------- | ---------------------------------------- | 1669| 9001003 | If the resName invalid. | 1670| 9001004 | If the resource not found by resName. | 1671| 9001006 | If the resource re-ref too much. | 1672 1673**Example** 1674 ```ts 1675 try { 1676 this.context.resourceManager.getStringByName("test").then(value => { 1677 let string = value; 1678 }).catch(error => { 1679 console.log("getStringByName promise error is " + error); 1680 }); 1681 } catch (error) { 1682 console.error(`promise getStringByName failed, error code: ${error.code}, message: ${error.message}.`) 1683 } 1684 ``` 1685 1686### getStringArrayByName<sup>9+</sup> 1687 1688getStringArrayByName(resName: string, callback: AsyncCallback<Array<string>>): void 1689 1690Obtains the string array corresponding to the specified resource name. This API uses an asynchronous callback to return the result. 1691 1692**System capability**: SystemCapability.Global.ResourceManager 1693 1694**Parameters** 1695 1696| Name | Type | Mandatory | Description | 1697| -------- | ---------------------------------------- | ---- | ----------------- | 1698| resName | string | Yes | Resource name. | 1699| callback | AsyncCallback<Array<string>> | Yes | Callback used to return the result.| 1700 1701For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1702 1703**Error codes** 1704 1705| ID| Error Message| 1706| -------- | ---------------------------------------- | 1707| 9001003 | If the resName invalid. | 1708| 9001004 | If the resource not found by resName. | 1709| 9001006 | If the resource re-ref too much. | 1710 1711**Example** 1712 ```ts 1713 try { 1714 this.context.resourceManager.getStringArrayByName("test", (error, value) => { 1715 if (error != null) { 1716 console.log("error is " + error); 1717 } else { 1718 let strArray = value; 1719 } 1720 }); 1721 } catch (error) { 1722 console.error(`callback getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`) 1723 } 1724 ``` 1725 1726### getStringArrayByName<sup>9+</sup> 1727 1728getStringArrayByName(resName: string): Promise<Array<string>> 1729 1730Obtains the string array corresponding to the specified resource name. This API uses a promise to return the result. 1731 1732**System capability**: SystemCapability.Global.ResourceManager 1733 1734**Parameters** 1735 1736| Name | Type | Mandatory | Description | 1737| ------- | ------ | ---- | ---- | 1738| resName | string | Yes | Resource name.| 1739 1740**Return value** 1741 1742| Type | Description | 1743| ---------------------------------- | ------------ | 1744| Promise<Array<string>> | Promise used to return the result.| 1745 1746For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1747 1748**Error codes** 1749 1750| ID| Error Message| 1751| -------- | ---------------------------------------- | 1752| 9001003 | If the resName invalid. | 1753| 9001004 | If the resource not found by resName. | 1754| 9001006 | If the resource re-ref too much. | 1755 1756**Example** 1757 ```ts 1758 try { 1759 this.context.resourceManager.getStringArrayByName("test").then(value => { 1760 let strArray = value; 1761 }).catch(error => { 1762 console.log("getStringArrayByName promise error is " + error); 1763 }); 1764 } catch (error) { 1765 console.error(`promise getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`) 1766 } 1767 ``` 1768 1769### getMediaByName<sup>9+</sup> 1770 1771getMediaByName(resName: string, callback: AsyncCallback<Uint8Array>): void 1772 1773Obtains the content of the media file corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 1774 1775**System capability**: SystemCapability.Global.ResourceManager 1776 1777**Parameters** 1778 1779| Name | Type | Mandatory | Description | 1780| -------- | ------------------------------- | ---- | ------------------ | 1781| resName | string | Yes | Resource name. | 1782| callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| 1783 1784For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1785 1786**Error codes** 1787 1788| ID| Error Message| 1789| -------- | ---------------------------------------- | 1790| 9001003 | If the resName invalid. | 1791| 9001004 | If the resource not found by resName. | 1792| 9001006 | If the resource re-ref too much. | 1793 1794**Example** 1795 ```ts 1796 try { 1797 this.context.resourceManager.getMediaByName("test", (error, value) => { 1798 if (error != null) { 1799 console.log("error is " + error); 1800 } else { 1801 let media = value; 1802 } 1803 }); 1804 } catch (error) { 1805 console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`) 1806 } 1807 ``` 1808 1809### getMediaByName<sup>9+</sup> 1810 1811getMediaByName(resName: string): Promise<Uint8Array> 1812 1813Obtains the content of the media file corresponding to the specified resource name. This API uses a promise to return the result. 1814 1815**System capability**: SystemCapability.Global.ResourceManager 1816 1817**Parameters** 1818 1819| Name | Type | Mandatory | Description | 1820| ------- | ------ | ---- | ---- | 1821| resName | string | Yes | Resource name.| 1822 1823**Return value** 1824 1825| Type | Description | 1826| ------------------------- | ------------- | 1827| Promise<Uint8Array> | Promise used to return the result.| 1828 1829For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1830 1831**Error codes** 1832 1833| ID| Error Message| 1834| -------- | ---------------------------------------- | 1835| 9001003 | If the resName invalid. | 1836| 9001004 | If the resource not found by resName. | 1837| 9001006 | If the resource re-ref too much. | 1838 1839**Example** 1840 ```ts 1841 try { 1842 this.context.resourceManager.getMediaByName("test").then(value => { 1843 let media = value; 1844 }).catch(error => { 1845 console.log("getMediaByName promise error is " + error); 1846 }); 1847 } catch (error) { 1848 console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`) 1849 } 1850 ``` 1851 1852### getMediaBase64ByName<sup>9+</sup> 1853 1854getMediaBase64ByName(resName: string, callback: AsyncCallback<string>): void 1855 1856Obtains the Base64 code of the image corresponding to the specified resource name. This API uses an asynchronous callback to return the result. 1857 1858**System capability**: SystemCapability.Global.ResourceManager 1859 1860**Parameters** 1861 1862| Name | Type | Mandatory | Description | 1863| -------- | --------------------------- | ---- | ------------------------ | 1864| resName | string | Yes | Resource name. | 1865| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 1866 1867For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1868 1869**Error codes** 1870 1871| ID| Error Message| 1872| -------- | ---------------------------------------- | 1873| 9001003 | If the resName invalid. | 1874| 9001004 | If the resource not found by resName. | 1875| 9001006 | If the resource re-ref too much. | 1876 1877**Example** 1878 ```ts 1879 try { 1880 this.context.resourceManager.getMediaBase64ByName("test", (error, value) => { 1881 if (error != null) { 1882 console.log("error is " + error); 1883 } else { 1884 let media = value; 1885 } 1886 }); 1887 } catch (error) { 1888 console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`) 1889 } 1890 ``` 1891 1892### getMediaBase64ByName<sup>9+</sup> 1893 1894getMediaBase64ByName(resName: string): Promise<string> 1895 1896Obtains the Base64 code of the image corresponding to the specified resource name. This API uses a promise to return the result. 1897 1898**System capability**: SystemCapability.Global.ResourceManager 1899 1900**Parameters** 1901 1902| Name | Type | Mandatory | Description | 1903| ------- | ------ | ---- | ---- | 1904| resName | string | Yes | Resource name.| 1905 1906**Return value** 1907 1908| Type | Description | 1909| --------------------- | ------------------- | 1910| Promise<string> | Promise used to return the result.| 1911 1912For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1913 1914**Error codes** 1915 1916| ID| Error Message| 1917| -------- | ---------------------------------------- | 1918| 9001003 | If the resName invalid. | 1919| 9001004 | If the resource not found by resName. | 1920| 9001006 | If the resource re-ref too much. | 1921 1922**Example** 1923 ```ts 1924 try { 1925 this.context.resourceManager.getMediaBase64ByName("test").then(value => { 1926 let media = value; 1927 }).catch(error => { 1928 console.log("getMediaBase64ByName promise error is " + error); 1929 }); 1930 } catch (error) { 1931 console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`) 1932 } 1933 ``` 1934 1935### getPluralStringByName<sup>9+</sup> 1936 1937getPluralStringByName(resName: string, num: number, callback: AsyncCallback<string>): void 1938 1939Obtains the plural string corresponding to the specified resource name based on the specified number. This API uses an asynchronous callback to return the result. 1940 1941**System capability**: SystemCapability.Global.ResourceManager 1942 1943**Parameters** 1944 1945| Name | Type | Mandatory | Description | 1946| -------- | --------------------------- | ---- | ----------------------------- | 1947| resName | string | Yes | Resource name. | 1948| num | number | Yes | Number. | 1949| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 1950 1951For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1952 1953**Error codes** 1954 1955| ID| Error Message| 1956| -------- | ---------------------------------------- | 1957| 9001003 | If the resName invalid. | 1958| 9001004 | If the resource not found by resName. | 1959| 9001006 | If the resource re-ref too much. | 1960 1961**Example** 1962 ```ts 1963 try { 1964 this.context.resourceManager.getPluralStringByName("test", 1, (error, value) => { 1965 if (error != null) { 1966 console.log("error is " + error); 1967 } else { 1968 let str = value; 1969 } 1970 }); 1971 } catch (error) { 1972 console.error(`callback getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`) 1973 } 1974 1975 ``` 1976 1977### getPluralStringByName<sup>9+</sup> 1978 1979getPluralStringByName(resName: string, num: number): Promise<string> 1980 1981Obtains the plural string corresponding to the specified resource name based on the specified number. This API uses a promise to return the result. 1982 1983**System capability**: SystemCapability.Global.ResourceManager 1984 1985**Parameters** 1986 1987| Name | Type | Mandatory | Description | 1988| ------- | ------ | ---- | ---- | 1989| resName | string | Yes | Resource name.| 1990| num | number | Yes | Number. | 1991 1992**Return value** 1993 1994| Type | Description | 1995| --------------------- | ---------------------- | 1996| Promise<string> | Promise used to return the result.| 1997 1998For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1999 2000**Error codes** 2001 2002| ID| Error Message| 2003| -------- | ---------------------------------------- | 2004| 9001003 | If the resName invalid. | 2005| 9001004 | If the resource not found by resName. | 2006| 9001006 | If the resource re-ref too much. | 2007 2008**Example** 2009 ```ts 2010 try { 2011 this.context.resourceManager.getPluralStringByName("test", 1).then(value => { 2012 let str = value; 2013 }).catch(error => { 2014 console.log("getPluralStringByName promise error is " + error); 2015 }); 2016 } catch (error) { 2017 console.error(`promise getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`) 2018 } 2019 ``` 2020 2021### getStringSync<sup>9+</sup> 2022 2023getStringSync(resId: number): string 2024 2025Obtains the string corresponding to the specified resource ID. This API returns the result synchronously. 2026 2027**System capability**: SystemCapability.Global.ResourceManager 2028 2029**Parameters** 2030 2031| Name | Type | Mandatory | Description | 2032| ----- | ------ | ---- | ----- | 2033| resId | number | Yes | Resource ID.| 2034 2035**Return value** 2036 2037| Type | Description | 2038| ------ | ----------- | 2039| string | Promise used to return the result.| 2040 2041For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2042 2043**Error codes** 2044 2045| ID| Error Message| 2046| -------- | ---------------------------------------- | 2047| 9001001 | If the resId invalid. | 2048| 9001002 | If the resource not found by resId. | 2049| 9001006 | If the resource re-ref too much. | 2050 2051**Example** 2052 ```ts 2053 try { 2054 this.context.resourceManager.getStringSync($r('app.string.test').id); 2055 } catch (error) { 2056 console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`) 2057 } 2058 ``` 2059 2060### getStringSync<sup>9+</sup> 2061 2062getStringSync(resource: Resource): string 2063 2064Obtains the string corresponding to the specified resource object. This API returns the result synchronously. 2065 2066**System capability**: SystemCapability.Global.ResourceManager 2067 2068**Parameters** 2069 2070| Name | Type | Mandatory | Description | 2071| -------- | ---------------------- | ---- | ---- | 2072| resource | [Resource](#resource9) | Yes | Resource object.| 2073 2074**Return value** 2075 2076| Type | Description | 2077| ------ | ---------------- | 2078| string | Promise used to return the result.| 2079 2080For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2081 2082**Error codes** 2083 2084| ID| Error Message| 2085| -------- | ---------------------------------------- | 2086| 9001001 | If the resId invalid. | 2087| 9001002 | If the resource not found by resId. | 2088| 9001006 | If the resource re-ref too much. | 2089 2090**Example** 2091 ```ts 2092 let resource = { 2093 bundleName: "com.example.myapplication", 2094 moduleName: "entry", 2095 id: $r('app.string.test').id 2096 }; 2097 try { 2098 this.context.resourceManager.getStringSync(resource); 2099 } catch (error) { 2100 console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`) 2101 } 2102 ``` 2103 2104### getStringByNameSync<sup>9+</sup> 2105 2106getStringByNameSync(resName: string): string 2107 2108Obtains the string corresponding to the specified resource name. This API returns the result synchronously. 2109 2110**System capability**: SystemCapability.Global.ResourceManager 2111 2112**Parameters** 2113 2114| Name | Type | Mandatory | Description | 2115| ------- | ------ | ---- | ---- | 2116| resName | string | Yes | Resource name.| 2117 2118**Return value** 2119 2120| Type | Description | 2121| ------ | ---------- | 2122| string | String corresponding to the specified resource name.| 2123 2124For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2125 2126**Error codes** 2127 2128| ID| Error Message| 2129| -------- | ---------------------------------------- | 2130| 9001003 | If the resName invalid. | 2131| 9001004 | If the resource not found by resName. | 2132| 9001006 | If the resource re-ref too much. | 2133 2134**Example** 2135 ```ts 2136 try { 2137 this.context.resourceManager.getStringByNameSync("test"); 2138 } catch (error) { 2139 console.error(`getStringByNameSync failed, error code: ${error.code}, message: ${error.message}.`) 2140 } 2141 ``` 2142 2143### getBoolean<sup>9+</sup> 2144 2145getBoolean(resId: number): boolean 2146 2147Obtains the Boolean result corresponding to the specified resource ID. This API returns the result synchronously. 2148 2149**System capability**: SystemCapability.Global.ResourceManager 2150 2151**Parameters** 2152 2153| Name | Type | Mandatory | Description | 2154| ----- | ------ | ---- | ----- | 2155| resId | number | Yes | Resource ID.| 2156 2157**Return value** 2158 2159| Type | Description | 2160| ------- | ------------ | 2161| boolean | Boolean result corresponding to the specified resource ID.| 2162 2163For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2164 2165**Error codes** 2166 2167| ID| Error Message| 2168| -------- | ---------------------------------------- | 2169| 9001001 | If the resId invalid. | 2170| 9001002 | If the resource not found by resId. | 2171| 9001006 | If the resource re-ref too much. | 2172 2173**Example** 2174 ```ts 2175 try { 2176 this.context.resourceManager.getBoolean($r('app.boolean.boolean_test').id); 2177 } catch (error) { 2178 console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`) 2179 } 2180 ``` 2181### getBoolean<sup>9+</sup> 2182 2183getBoolean(resource: Resource): boolean 2184 2185Obtains the Boolean result corresponding to the specified resource object. This API returns the result synchronously. 2186 2187**System capability**: SystemCapability.Global.ResourceManager 2188 2189**Parameters** 2190 2191| Name | Type | Mandatory | Description | 2192| -------- | ---------------------- | ---- | ---- | 2193| resource | [Resource](#resource9) | Yes | Resource object.| 2194 2195**Return value** 2196 2197| Type | Description | 2198| ------- | ----------------- | 2199| boolean | Boolean result corresponding to the specified resource object.| 2200 2201For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2202 2203**Error codes** 2204 2205| ID| Error Message| 2206| -------- | ---------------------------------------- | 2207| 9001001 | If the resId invalid. | 2208| 9001002 | If the resource not found by resId. | 2209| 9001006 | If the resource re-ref too much. | 2210 2211**Example** 2212 ```ts 2213 let resource = { 2214 bundleName: "com.example.myapplication", 2215 moduleName: "entry", 2216 id: $r('app.boolean.boolean_test').id 2217 }; 2218 try { 2219 this.context.resourceManager.getBoolean(resource); 2220 } catch (error) { 2221 console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`) 2222 } 2223 ``` 2224 2225### getBooleanByName<sup>9+</sup> 2226 2227getBooleanByName(resName: string): boolean 2228 2229Obtains the Boolean result corresponding to the specified resource name. This API returns the result synchronously. 2230 2231**System capability**: SystemCapability.Global.ResourceManager 2232 2233**Parameters** 2234 2235| Name | Type | Mandatory | Description | 2236| ------- | ------ | ---- | ---- | 2237| resName | string | Yes | Resource name.| 2238 2239**Return value** 2240 2241| Type | Description | 2242| ------- | ----------- | 2243| boolean | Boolean result corresponding to the specified resource name.| 2244 2245For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2246 2247**Error codes** 2248 2249| ID| Error Message| 2250| -------- | ---------------------------------------- | 2251| 9001003 | If the resName invalid. | 2252| 9001004 | If the resource not found by resName. | 2253| 9001006 | If the resource re-ref too much. | 2254 2255**Example** 2256 ```ts 2257 try { 2258 this.context.resourceManager.getBooleanByName("boolean_test"); 2259 } catch (error) { 2260 console.error(`getBooleanByName failed, error code: ${error.code}, message: ${error.message}.`) 2261 } 2262 ``` 2263 2264### getNumber<sup>9+</sup> 2265 2266getNumber(resId: number): number 2267 2268Obtains the integer or float value corresponding to the specified resource ID. This API returns the result synchronously. 2269 2270**System capability**: SystemCapability.Global.ResourceManager 2271 2272**Parameters** 2273 2274| Name | Type | Mandatory | Description | 2275| ----- | ------ | ---- | ----- | 2276| resId | number | Yes | Resource ID.| 2277 2278**Return value** 2279 2280| Type | Description | 2281| ------ | ---------- | 2282| number | Integer or float value corresponding to the specified resource ID. Wherein, the integer value is the original value, and the float value is the actual pixel value. For details, see the sample code.| 2283 2284For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2285 2286**Error codes** 2287 2288| ID| Error Message| 2289| -------- | ---------------------------------------- | 2290| 9001001 | If the resId invalid. | 2291| 9001002 | If the resource not found by resId. | 2292| 9001006 | If the resource re-ref too much. | 2293 2294**Example** 2295 ```ts 2296 try { 2297 this.context.resourceManager.getNumber($r('app.integer.integer_test').id); // integer refers to the original value. 2298 } catch (error) { 2299 console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`) 2300 } 2301 2302 try { 2303 this.context.resourceManager.getNumber($r('app.float.float_test').id); // float refers to the actual pixel value. 2304 } catch (error) { 2305 console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`) 2306 } 2307 ``` 2308 2309### getNumber<sup>9+</sup> 2310 2311getNumber(resource: Resource): number 2312 2313Obtains the integer or float value corresponding to the specified resource object. This API returns the result synchronously. 2314 2315**System capability**: SystemCapability.Global.ResourceManager 2316 2317**Parameters** 2318 2319| Name | Type | Mandatory | Description | 2320| -------- | ---------------------- | ---- | ---- | 2321| resource | [Resource](#resource9) | Yes | Resource object.| 2322 2323**Return value** 2324 2325| Type | Description | 2326| ------ | --------------- | 2327| number | Integer or float value corresponding to the specified resource object. Wherein, the integer value is the original value, and the float value is the actual pixel value. For details, see the sample code.| 2328 2329For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2330 2331**Error codes** 2332 2333| ID| Error Message| 2334| -------- | ---------------------------------------- | 2335| 9001001 | If the resId invalid. | 2336| 9001002 | If the resource not found by resId. | 2337| 9001006 | If the resource re-ref too much. | 2338 2339**Example** 2340 ```ts 2341 let resource = { 2342 bundleName: "com.example.myapplication", 2343 moduleName: "entry", 2344 id: $r('app.integer.integer_test').id 2345 }; 2346 try { 2347 this.context.resourceManager.getNumber(resource);// integer refers to the original value; float refers to the actual pixel value. 2348 } catch (error) { 2349 console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`) 2350 } 2351 ``` 2352 2353### getNumberByName<sup>9+</sup> 2354 2355getNumberByName(resName: string): number 2356 2357Obtains the integer or float value corresponding to the specified resource name. This API returns the result synchronously. 2358 2359**System capability**: SystemCapability.Global.ResourceManager 2360 2361**Parameters** 2362 2363| Name | Type | Mandatory | Description | 2364| ------- | ------ | ---- | ---- | 2365| resName | string | Yes | Resource name.| 2366 2367**Return value** 2368 2369| Type | Description | 2370| ------ | --------- | 2371| number | Integer or float value corresponding to the specified resource name.| 2372 2373For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2374 2375**Error codes** 2376 2377| ID| Error Message| 2378| -------- | ---------------------------------------- | 2379| 9001003 | If the resName invalid. | 2380| 9001004 | If the resource not found by resName. | 2381| 9001006 | If the resource re-ref too much. | 2382 2383**Example** 2384 ```ts 2385 try { 2386 this.context.resourceManager.getNumberByName("integer_test"); 2387 } catch (error) { 2388 console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`) 2389 } 2390 2391 try { 2392 this.context.resourceManager.getNumberByName("float_test"); 2393 } catch (error) { 2394 console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`) 2395 } 2396 ``` 2397 2398 2399### getString<sup>(deprecated)</sup> 2400 2401getString(resId: number, callback: AsyncCallback<string>): void 2402 2403Obtains the string corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 2404 2405This API is deprecated since API version 9. You are advised to use [getStringValue](#getstringvalue9) instead. 2406 2407**System capability**: SystemCapability.Global.ResourceManager 2408 2409**Parameters** 2410 2411| Name | Type | Mandatory | Description | 2412| -------- | --------------------------- | ---- | --------------- | 2413| resId | number | Yes | Resource ID. | 2414| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 2415 2416**Example** 2417 ```ts 2418 resourceManager.getResourceManager((error, mgr) => { 2419 mgr.getString($r('app.string.test').id, (error, value) => { 2420 if (error != null) { 2421 console.log("error is " + error); 2422 } else { 2423 let str = value; 2424 } 2425 }); 2426 }); 2427 ``` 2428 2429 2430### getString<sup>(deprecated)</sup> 2431 2432getString(resId: number): Promise<string> 2433 2434Obtains the string corresponding to the specified resource ID. This API uses a promise to return the result. 2435 2436This API is deprecated since API version 9. You are advised to use [getStringValue](#getstringvalue9-1) instead. 2437 2438**System capability**: SystemCapability.Global.ResourceManager 2439 2440**Parameters** 2441 2442| Name | Type | Mandatory | Description | 2443| ----- | ------ | ---- | ----- | 2444| resId | number | Yes | Resource ID.| 2445 2446**Return value** 2447 2448| Type | Description | 2449| --------------------- | ----------- | 2450| Promise<string> | Promise used to return the result.| 2451 2452**Example** 2453 ```ts 2454 resourceManager.getResourceManager((error, mgr) => { 2455 mgr.getString($r('app.string.test').id).then(value => { 2456 let str = value; 2457 }).catch(error => { 2458 console.log("getstring promise error is " + error); 2459 }); 2460 }); 2461 ``` 2462 2463 2464### getStringArray<sup>(deprecated)</sup> 2465 2466getStringArray(resId: number, callback: AsyncCallback<Array<string>>): void 2467 2468Obtains the string array corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 2469 2470This API is deprecated since API version 9. You are advised to use [getStringArrayValue](#getstringarrayvalue9) instead. 2471 2472**System capability**: SystemCapability.Global.ResourceManager 2473 2474**Parameters** 2475 2476| Name | Type | Mandatory | Description | 2477| -------- | ---------------------------------------- | ---- | ----------------- | 2478| resId | number | Yes | Resource ID. | 2479| callback | AsyncCallback<Array<string>> | Yes | Callback used to return the result.| 2480 2481**Example** 2482 ```ts 2483 resourceManager.getResourceManager((error, mgr) => { 2484 mgr.getStringArray($r('app.strarray.test').id, (error, value) => { 2485 if (error != null) { 2486 console.log("error is " + error); 2487 } else { 2488 let strArray = value; 2489 } 2490 }); 2491 }); 2492 ``` 2493 2494 2495### getStringArray<sup>(deprecated)</sup> 2496 2497getStringArray(resId: number): Promise<Array<string>> 2498 2499Obtains the string array corresponding to the specified resource ID. This API uses a promise to return the result. 2500 2501This API is deprecated since API version 9. You are advised to use [getStringArrayValue](#getstringarrayvalue9-1) instead. 2502 2503**System capability**: SystemCapability.Global.ResourceManager 2504 2505**Parameters** 2506 2507| Name | Type | Mandatory | Description | 2508| ----- | ------ | ---- | ----- | 2509| resId | number | Yes | Resource ID.| 2510 2511**Return value** 2512 2513| Type | Description | 2514| ---------------------------------- | ------------- | 2515| Promise<Array<string>> | Promise used to return the result.| 2516 2517**Example** 2518 ```ts 2519 resourceManager.getResourceManager((error, mgr) => { 2520 mgr.getStringArray($r('app.strarray.test').id).then(value => { 2521 let strArray = value; 2522 }).catch(error => { 2523 console.log("getStringArray promise error is " + error); 2524 }); 2525 }); 2526 ``` 2527 2528 2529### getMedia<sup>(deprecated)</sup> 2530 2531getMedia(resId: number, callback: AsyncCallback<Uint8Array>): void 2532 2533Obtains the content of the media file corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 2534 2535This API is deprecated since API version 9. You are advised to use [getMediaContent](#getmediacontent9) instead. 2536 2537**System capability**: SystemCapability.Global.ResourceManager 2538 2539**Parameters** 2540 2541| Name | Type | Mandatory | Description | 2542| -------- | ------------------------------- | ---- | ------------------ | 2543| resId | number | Yes | Resource ID. | 2544| callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| 2545 2546**Example** 2547 ```ts 2548 resourceManager.getResourceManager((error, mgr) => { 2549 mgr.getMedia($r('app.media.test').id, (error, value) => { 2550 if (error != null) { 2551 console.log("error is " + error); 2552 } else { 2553 let media = value; 2554 } 2555 }); 2556 }); 2557 ``` 2558 2559 2560### getMedia<sup>(deprecated)</sup> 2561 2562getMedia(resId: number): Promise<Uint8Array> 2563 2564Obtains the content of the media file corresponding to the specified resource ID. This API uses a promise to return the result. 2565 2566This API is deprecated since API version 9. You are advised to use [getMediaContent](#getmediacontent9-1) instead. 2567 2568**System capability**: SystemCapability.Global.ResourceManager 2569 2570**Parameters** 2571 2572| Name | Type | Mandatory | Description | 2573| ----- | ------ | ---- | ----- | 2574| resId | number | Yes | Resource ID.| 2575 2576**Return value** 2577 2578| Type | Description | 2579| ------------------------- | -------------- | 2580| Promise<Uint8Array> | Promise used to return the result.| 2581 2582**Example** 2583 ```ts 2584 resourceManager.getResourceManager((error, mgr) => { 2585 mgr.getMedia($r('app.media.test').id).then(value => { 2586 let media = value; 2587 }).catch(error => { 2588 console.log("getMedia promise error is " + error); 2589 }); 2590 }); 2591 ``` 2592 2593 2594### getMediaBase64<sup>(deprecated)</sup> 2595 2596getMediaBase64(resId: number, callback: AsyncCallback<string>): void 2597 2598Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 2599 2600This API is deprecated since API version 9. You are advised to use [getMediaContentBase64](#getmediacontentbase649) instead. 2601 2602**System capability**: SystemCapability.Global.ResourceManager 2603 2604**Parameters** 2605 2606| Name | Type | Mandatory | Description | 2607| -------- | --------------------------- | ---- | ------------------------ | 2608| resId | number | Yes | Resource ID. | 2609| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 2610 2611**Example** 2612 ```ts 2613 resourceManager.getResourceManager((error, mgr) => { 2614 mgr.getMediaBase64($r('app.media.test').id, (error, value) => { 2615 if (error != null) { 2616 console.log("error is " + error); 2617 } else { 2618 let media = value; 2619 } 2620 }); 2621 }); 2622 ``` 2623 2624 2625### getMediaBase64<sup>(deprecated)</sup> 2626 2627getMediaBase64(resId: number): Promise<string> 2628 2629Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses a promise to return the result. 2630 2631This API is deprecated since API version 9. You are advised to use [getMediaContentBase64](#getmediacontentbase649-1) instead. 2632 2633**System capability**: SystemCapability.Global.ResourceManager 2634 2635**Parameters** 2636 2637| Name | Type | Mandatory | Description | 2638| ----- | ------ | ---- | ----- | 2639| resId | number | Yes | Resource ID.| 2640 2641**Return value** 2642 2643| Type | Description | 2644| --------------------- | -------------------- | 2645| Promise<string> | Promise used to return the result.| 2646 2647**Example** 2648 ```ts 2649 resourceManager.getResourceManager((error, mgr) => { 2650 mgr.getMediaBase64($r('app.media.test').id).then(value => { 2651 let media = value; 2652 }).catch(error => { 2653 console.log("getMediaBase64 promise error is " + error); 2654 }); 2655 }); 2656 ``` 2657 2658 2659### getPluralString<sup>(deprecated)</sup> 2660 2661getPluralString(resId: number, num: number): Promise<string> 2662 2663Obtains the singular-plural string corresponding to the specified resource ID based on the specified number. This API uses a promise to return the result. 2664 2665This API is deprecated since API version 9. You are advised to use [getPluralStringValue](#getpluralstringvalue9) instead. 2666 2667**System capability**: SystemCapability.Global.ResourceManager 2668 2669**Parameters** 2670 2671| Name | Type | Mandatory | Description | 2672| ----- | ------ | ---- | ----- | 2673| resId | number | Yes | Resource ID.| 2674| num | number | Yes | Number. | 2675 2676**Return value** 2677 2678| Type | Description | 2679| --------------------- | ------------------------- | 2680| Promise<string> | Promise used to return the result.| 2681 2682**Example** 2683 ```ts 2684 resourceManager.getResourceManager((error, mgr) => { 2685 mgr.getPluralString($r("app.plural.test").id, 1).then(value => { 2686 let str = value; 2687 }).catch(error => { 2688 console.log("getPluralString promise error is " + error); 2689 }); 2690 }); 2691 ``` 2692 2693 2694### getPluralString<sup>(deprecated)</sup> 2695 2696getPluralString(resId: number, num: number, callback: AsyncCallback<string>): void 2697 2698Obtains the singular-plural string corresponding to the specified resource ID based on the specified number. This API uses an asynchronous callback to return the result. 2699 2700This API is deprecated since API version 9. You are advised to use [getPluralStringValue](#getpluralstringvalue9-1) instead. 2701 2702**System capability**: SystemCapability.Global.ResourceManager 2703 2704**Parameters** 2705 2706| Name | Type | Mandatory | Description | 2707| -------- | --------------------------- | ---- | ------------------------------- | 2708| resId | number | Yes | Resource ID. | 2709| num | number | Yes | Number. | 2710| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 2711 2712**Example** 2713 ```ts 2714 resourceManager.getResourceManager((error, mgr) => { 2715 mgr.getPluralString($r("app.plural.test").id, 1, (error, value) => { 2716 if (error != null) { 2717 console.log("error is " + error); 2718 } else { 2719 let str = value; 2720 } 2721 }); 2722 }); 2723 ``` 2724 2725 2726### getRawFile<sup>(deprecated)</sup> 2727 2728getRawFile(path: string, callback: AsyncCallback<Uint8Array>): void 2729 2730Obtains the content of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. 2731 2732This API is deprecated since API version 9. You are advised to use [getRawFileContent](#getrawfilecontent9) instead. 2733 2734**System capability**: SystemCapability.Global.ResourceManager 2735 2736**Parameters** 2737 2738| Name | Type | Mandatory | Description | 2739| -------- | ------------------------------- | ---- | ----------------------- | 2740| path | string | Yes | Path of the raw file. | 2741| callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| 2742 2743**Example** 2744 ```ts 2745 resourceManager.getResourceManager((error, mgr) => { 2746 mgr.getRawFile("test.xml", (error, value) => { 2747 if (error != null) { 2748 console.log("error is " + error); 2749 } else { 2750 let rawFile = value; 2751 } 2752 }); 2753 }); 2754 ``` 2755 2756 2757### getRawFile<sup>(deprecated)</sup> 2758 2759getRawFile(path: string): Promise<Uint8Array> 2760 2761Obtains the content of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. 2762 2763This API is deprecated since API version 9. You are advised to use [getRawFileContent](#getrawfilecontent9-1) instead. 2764 2765**System capability**: SystemCapability.Global.ResourceManager 2766 2767**Parameters** 2768 2769| Name | Type | Mandatory | Description | 2770| ---- | ------ | ---- | ----------- | 2771| path | string | Yes | Path of the raw file.| 2772 2773**Return value** 2774 2775| Type | Description | 2776| ------------------------- | ----------- | 2777| Promise<Uint8Array> | Promise used to return the result.| 2778 2779**Example** 2780 ```ts 2781 resourceManager.getResourceManager((error, mgr) => { 2782 mgr.getRawFile("test.xml").then(value => { 2783 let rawFile = value; 2784 }).catch(error => { 2785 console.log("getRawFile promise error is " + error); 2786 }); 2787 }); 2788 ``` 2789 2790 2791### getRawFileDescriptor<sup>(deprecated)</sup> 2792 2793getRawFileDescriptor(path: string, callback: AsyncCallback<RawFileDescriptor>): void 2794 2795Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. 2796 2797This API is deprecated since API version 9. You are advised to use [getRawFd](#getrawfd9) instead. 2798 2799**System capability**: SystemCapability.Global.ResourceManager 2800 2801**Parameters** 2802 2803| Name | Type | Mandatory | Description | 2804| -------- | ---------------------------------------- | ---- | -------------------------------- | 2805| path | string | Yes | Path of the raw file. | 2806| callback | AsyncCallback<[RawFileDescriptor](#rawfiledescriptor8)> | Yes | Callback used to return the result.| 2807 2808**Example** 2809 ```ts 2810 resourceManager.getResourceManager((error, mgr) => { 2811 mgr.getRawFileDescriptor("test.xml", (error, value) => { 2812 if (error != null) { 2813 console.log("error is " + error); 2814 } else { 2815 let fd = value.fd; 2816 let offset = value.offset; 2817 let length = value.length; 2818 } 2819 }); 2820 }); 2821 ``` 2822 2823### getRawFileDescriptor<sup>(deprecated)</sup> 2824 2825getRawFileDescriptor(path: string): Promise<RawFileDescriptor> 2826 2827Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. 2828 2829This API is deprecated since API version 9. You are advised to use [getRawFd](#getrawfd9-1) instead. 2830 2831**System capability**: SystemCapability.Global.ResourceManager 2832 2833**Parameters** 2834 2835| Name | Type | Mandatory | Description | 2836| ---- | ------ | ---- | ----------- | 2837| path | string | Yes | Path of the raw file.| 2838 2839**Return value** 2840 2841| Type | Description | 2842| ---------------------------------------- | ------------------- | 2843| Promise<[RawFileDescriptor](#rawfiledescriptor8)> | Promise used to return the result.| 2844 2845**Example** 2846 ```ts 2847 resourceManager.getResourceManager((error, mgr) => { 2848 mgr.getRawFileDescriptor("test.xml").then(value => { 2849 let fd = value.fd; 2850 let offset = value.offset; 2851 let length = value.length; 2852 }).catch(error => { 2853 console.log("getRawFileDescriptor promise error is " + error); 2854 }); 2855 }); 2856 ``` 2857