1# @ohos.resourceManager (Resource Management) 2 3The **resourceManager** 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## How to Use 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'; 23import window from '@ohos.window'; 24 25export default class EntryAbility extends UIAbility { 26 onWindowStageCreate(windowStage: window.WindowStage) { 27 let context = this.context; 28 let resourceManager = context.resourceManager; 29 } 30} 31``` 32 33## resourceManager.getResourceManager 34 35getResourceManager(callback: AsyncCallback<ResourceManager>): void 36 37Obtains the **ResourceManager** object of this application. This API uses an asynchronous callback to return the result. 38 39**System capability**: SystemCapability.Global.ResourceManager 40 41**Model restriction**: This API can be used only in the FA model. 42 43**Parameters** 44 45| Name | Type | Mandatory | Description | 46| -------- | ---------------------------------------- | ---- | ----------------------------- | 47| callback | AsyncCallback<[ResourceManager](#resourcemanager)> | Yes | Callback used to return the result.| 48 49**Example** 50 ```js 51 resourceManager.getResourceManager((error, mgr) => { 52 if (error != null) { 53 console.log("error is " + error); 54 return; 55 } 56 mgr.getStringValue(0x1000000, (error, value) => { 57 if (error != null) { 58 console.log("error is " + error); 59 } else { 60 let str = value; 61 } 62 }); 63 }); 64 ``` 65> **NOTE**<br>In the sample code, **0x1000000** indicates the resource ID, which can be found in the compiled **ResourceTable.txt** file. 66 67 68## resourceManager.getResourceManager 69 70getResourceManager(bundleName: string, callback: AsyncCallback<ResourceManager>): void 71 72Obtains the **ResourceManager** object of an application based on the specified bundle name. This API uses an asynchronous callback to return the result. 73 74**System capability**: SystemCapability.Global.ResourceManager 75 76**Model restriction**: This API can be used only in the FA model. 77 78**Parameters** 79 80| Name | Type | Mandatory | Description | 81| ---------- | ---------------------------------------- | ---- | ----------------------------- | 82| bundleName | string | Yes | Bundle name of the application. | 83| callback | AsyncCallback<[ResourceManager](#resourcemanager)> | Yes | Callback used to return the result.| 84 85**Example** 86 ```js 87 resourceManager.getResourceManager("com.example.myapplication", (error, mgr) => { 88 }); 89 ``` 90 91 92## resourceManager.getResourceManager 93 94getResourceManager(): Promise<ResourceManager> 95 96Obtains the **ResourceManager** object of this application. This API uses a promise to return the result. 97 98**System capability**: SystemCapability.Global.ResourceManager 99 100**Model restriction**: This API can be used only in the FA model. 101 102**Return value** 103 104| Type | Description | 105| ---------------------------------------- | ----------------- | 106| Promise<[ResourceManager](#resourcemanager)> | Promise used to return the result.| 107 108**Example** 109 ```js 110 import resourceManager from '@ohos.resourceManager'; 111 import { BusinessError } from '@ohos.base'; 112 113 resourceManager.getResourceManager().then((mgr: resourceManager.ResourceManager) => { 114 mgr.getStringValue(0x1000000, (error, value) => { 115 if (error != null) { 116 console.log("error is " + error); 117 } else { 118 let str = value; 119 } 120 }); 121 }).catch((error: BusinessError) => { 122 console.log("error is " + error); 123 }); 124 ``` 125> **NOTE**<br>In the sample code, **0x1000000** indicates the resource ID, which can be found in the compiled **ResourceTable.txt** file. 126 127 128## resourceManager.getResourceManager 129 130getResourceManager(bundleName: string): Promise<ResourceManager> 131 132Obtains the **ResourceManager** object of an application based on the specified bundle name. This API uses a promise to return the result. 133 134**System capability**: SystemCapability.Global.ResourceManager 135 136**Model restriction**: This API can be used only in the FA model. 137 138**Parameters** 139 140| Name | Type | Mandatory | Description | 141| ---------- | ------ | ---- | ------------- | 142| bundleName | string | Yes | Bundle name of the application.| 143 144**Return value** 145 146| Type | Description | 147| ---------------------------------------- | ------------------ | 148| Promise<[ResourceManager](#resourcemanager)> | Promise used to return the result.| 149 150**Example** 151 ```js 152 import resourceManager from '@ohos.resourceManager'; 153 import { BusinessError } from '@ohos.base'; 154 155 resourceManager.getResourceManager("com.example.myapplication").then((mgr: resourceManager.ResourceManager) => { 156 }).catch((error: BusinessError) => { 157 }); 158 ``` 159 160## resourceManager.getSystemResourceManager<sup>10+</sup> 161 162getSystemResourceManager(): ResourceManager 163 164Obtains a **ResourceManager** object. 165 166**System capability**: SystemCapability.Global.ResourceManager 167 168**Return value** 169 170| Type | Description | 171| ---------------------------------------- | ------------------ | 172| [Resourcemanager](#resourcemanager) | **ResourceManager** object.| 173 174**Error codes** 175 176For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 177 178| ID| Error Message| 179| -------- | ---------------------------------------- | 180| 9001009 | If application can't access system resource. | 181 182**Example** 183 ```js 184import resourceManager from '@ohos.resourceManager'; 185import { BusinessError } from '@ohos.base'; 186 187 try { 188 let systemResourceManager = resourceManager.getSystemResourceManager(); 189 systemResourceManager.getStringValue($r('sys.string.ohos_lab_vibrate').id).then((value: string) => { 190 let str = value; 191 }).catch((error: BusinessError) => { 192 console.log("systemResourceManager getStringValue promise error is " + error); 193 }); 194 } catch (error) { 195 let code = (error as BusinessError).code; 196 let message = (error as BusinessError).message; 197 console.error(`systemResourceManager getStringValue failed, error code: ${code}, message: ${message}.`); 198 } 199 ``` 200 201 202## Direction 203 204Enumerates the screen directions. 205 206**System capability**: SystemCapability.Global.ResourceManager 207 208| Name | Value | Description | 209| -------------------- | ---- | ---- | 210| DIRECTION_VERTICAL | 0 | Portrait. | 211| DIRECTION_HORIZONTAL | 1 | Landscape. | 212 213 214## DeviceType 215 216Enumerates the device types. 217 218**System capability**: SystemCapability.Global.ResourceManager 219 220| Name | Value | Description | 221| -------------------- | ---- | ---- | 222| DEVICE_TYPE_TABLET | 0x01 | Tablet. | 223| DEVICE_TYPE_CAR | 0x02 | Head unit. | 224| DEVICE_TYPE_TV | 0x04 | TV. | 225| DEVICE_TYPE_WEARABLE | 0x06 | Wearable. | 226 227 228## ScreenDensity 229 230Enumerates the screen density types. 231 232**System capability**: SystemCapability.Global.ResourceManager 233 234| Name | Value | Description | 235| -------------- | ---- | ---------- | 236| SCREEN_SDPI | 120 | Screen density with small-scale dots per inch (SDPI). | 237| SCREEN_MDPI | 160 | Screen density with medium-scale dots per inch (MDPI). | 238| SCREEN_LDPI | 240 | Screen density with large-scale dots per inch (LDPI). | 239| SCREEN_XLDPI | 320 | Screen density with extra-large-scale dots per inch (XLDPI). | 240| SCREEN_XXLDPI | 480 | Screen density with extra-extra-large-scale dots per inch (XXLDPI). | 241| SCREEN_XXXLDPI | 640 | Screen density with extra-extra-extra-large-scale dots per inch (XXXLDPI).| 242 243 244## Configuration 245 246Defines the device configuration. 247 248**System capability**: SystemCapability.Global.ResourceManager 249 250**Parameters** 251 252| Name | Type | Readable | Writable | Description | 253| --------- | ----------------------- | ---- | ---- | -------- | 254| direction | [Direction](#direction) | Yes | No | Screen direction of the device.| 255| locale | string | Yes | No | Current system language. | 256 257 258## DeviceCapability 259 260Defines the device capability. 261 262**System capability**: SystemCapability.Global.ResourceManager 263 264**Parameters** 265 266| Name | Type | Readable | Writable | Description | 267| ------------- | ------------------------------- | ---- | ---- | -------- | 268| screenDensity | [ScreenDensity](#screendensity) | Yes | No | Screen density of the device.| 269| deviceType | [DeviceType](#devicetype) | Yes | No | Device type. | 270 271 272## RawFileDescriptor<sup>8+</sup> 273 274Defines the descriptor of the raw file. 275 276**System capability**: SystemCapability.Global.ResourceManager 277 278**Parameters** 279 280| Name | Type | Readable | Writable | Description | 281| ------ | ------ | ---- | ---- | ------------------ | 282| fd | number | Yes | No| File descriptor of the HAP where the raw file is located.| 283| offset | number | Yes | No| Start offset of the raw file. | 284| length | number | Yes | No| Length of the raw file. | 285 286## Resource<sup>9+</sup> 287 288Defines the resource information of an application. 289 290**System capability**: SystemCapability.Global.ResourceManager 291 292**Parameters** 293 294| Name | Type | Readable | Writable |Description | 295| ---------- | ------ | ----- | ---- | ---------------| 296| bundleName | string | Yes | No| Bundle name of the application.| 297| moduleName | string | Yes | No| Module name of the application.| 298| id | number | Yes | No| Resource ID. | 299| params | any[] | Yes | No| Other resource parameters, which are optional. | 300| type | number | Yes | No| Resource type, which is optional. | 301 302 303## ResourceManager 304 305Defines the capability of accessing application resources. 306 307> **NOTE** 308> 309> - The methods involved in **ResourceManager** are applicable only to the TypeScript-based declarative development paradigm. 310> 311> - 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**. 312 313### getStringSync<sup>9+</sup> 314 315getStringSync(resId: number): string 316 317Obtains the string corresponding to the specified resource ID. This API returns the result synchronously. 318 319**System capability**: SystemCapability.Global.ResourceManager 320 321**Parameters** 322 323| Name | Type | Mandatory | Description | 324| ----- | ------ | ---- | ----- | 325| resId | number | Yes | Resource ID.| 326 327**Return value** 328 329| Type | Description | 330| ------ | ----------- | 331| string | String corresponding to the specified resource ID.| 332 333**Error codes** 334 335For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 336 337| ID| Error Message| 338| -------- | ---------------------------------------- | 339| 9001001 | If the resId invalid. | 340| 9001002 | If the resource not found by resId. | 341| 9001006 | If the resource re-ref too much. | 342 343**Example** 344 ```ts 345 import { BusinessError } from '@ohos.base'; 346 347 try { 348 this.context.resourceManager.getStringSync($r('app.string.test').id); 349 } catch (error) { 350 let code = (error as BusinessError).code; 351 let message = (error as BusinessError).message; 352 console.error(`getStringSync failed, error code: ${code}, message: ${message}.`); 353 } 354 ``` 355 356### getStringSync<sup>10+</sup> 357 358getStringSync(resId: number, ...args: Array<string | number>): string 359 360Obtains the string corresponding to the specified resource ID and formats the string based on **args**. This API returns the result synchronously. 361 362**System capability**: SystemCapability.Global.ResourceManager 363 364**Parameters** 365 366| Name | Type | Mandatory | Description | 367| ----- | ------ | ---- | ----- | 368| resId | number | Yes | Resource ID.| 369| args | Array<string \| number> | No | Arguments for formatting strings.<br> Supported arguments:<br> %d, %f, %s, and %%<br> Note: **%%** is used to translate **%**.<br>Example: **%%d** is translated into the **%d** string.| 370 371**Return value** 372 373| Type | Description | 374| ------ | ---------------------------- | 375| string | Formatted string.| 376 377**Error codes** 378 379For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 380 381| ID| Error Message| 382| -------- | ----------------------------------------------- | 383| 9001001 | If the resId invalid. | 384| 9001002 | If the resource not found by resId. | 385| 9001006 | If the resource re-ref too much. | 386| 9001007 | If the resource obtained by resId formatting error. | 387 388**Example** 389 ```ts 390 import { BusinessError } from '@ohos.base'; 391 392 try { 393 this.context.resourceManager.getStringSync($r('app.string.test').id, "format string", 10, 98.78); 394 } catch (error) { 395 let code = (error as BusinessError).code; 396 let message = (error as BusinessError).message; 397 console.error(`getStringSync failed, error code: ${code}, message: ${message}.`); 398 } 399 ``` 400 401### getStringSync<sup>9+</sup> 402 403getStringSync(resource: Resource): string 404 405Obtains the string corresponding to the specified resource object. This API returns the result synchronously. 406 407**System capability**: SystemCapability.Global.ResourceManager 408 409**Model restriction**: This API can be used only in the stage model. 410 411**Parameters** 412 413| Name | Type | Mandatory | Description | 414| -------- | ---------------------- | ---- | ---- | 415| resource | [Resource](#resource9) | Yes | Resource object.| 416 417**Return value** 418 419| Type | Description | 420| ------ | ---------------- | 421| string | String corresponding to the specified resource object.| 422 423**Error codes** 424 425For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 426 427| ID| Error Message| 428| -------- | ---------------------------------------- | 429| 9001001 | If the resId invalid. | 430| 9001002 | If the resource not found by resId. | 431| 9001006 | If the resource re-ref too much. | 432 433**Example** 434 ```ts 435 import resourceManager from '@ohos.resourceManager'; 436 import { BusinessError } from '@ohos.base'; 437 438 let resource: resourceManager.Resource = { 439 bundleName: "com.example.myapplication", 440 moduleName: "entry", 441 id: $r('app.string.test').id 442 }; 443 try { 444 this.context.resourceManager.getStringSync(resource); 445 } catch (error) { 446 let code = (error as BusinessError).code; 447 let message = (error as BusinessError).message; 448 console.error(`getStringSync failed, error code: ${code}, message: ${message}.`); 449 } 450 ``` 451 452### getStringSync<sup>10+</sup> 453 454getStringSync(resource: Resource, ...args: Array<string | number>): string 455 456Obtains the string corresponding to the specified resource object and formats the string based on **args**. This API returns the result synchronously. 457 458**System capability**: SystemCapability.Global.ResourceManager 459 460**Model restriction**: This API can be used only in the stage model. 461 462**Parameters** 463 464| Name | Type | Mandatory | Description | 465| -------- | ---------------------- | ---- | ---- | 466| resource | [Resource](#resource9) | Yes | Resource object.| 467| args | Array<string \| number> | No | Arguments for formatting strings.<br> Supported arguments:<br> %d, %f, %s, and %%<br> Note: **%%** is used to translate **%**.<br>Example: **%%d** is translated into the **%d** string.| 468 469**Return value** 470 471| Type | Description | 472| ------ | ---------------------------- | 473| string | Formatted string.| 474 475**Error codes** 476 477For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 478 479| ID| Error Message| 480| -------- | ---------------------------------------- | 481| 9001001 | If the resId invalid. | 482| 9001002 | If the resource not found by resId. | 483| 9001006 | If the resource re-ref too much. | 484| 9001007 | If the resource obtained by resId formatting error. | 485 486**Example** 487 ```ts 488 import resourceManager from '@ohos.resourceManager'; 489 import { BusinessError } from '@ohos.base'; 490 491 let resource: resourceManager.Resource = { 492 bundleName: "com.example.myapplication", 493 moduleName: "entry", 494 id: $r('app.string.test').id 495 }; 496 try { 497 this.context.resourceManager.getStringSync(resource, "format string", 10, 98.78); 498 } catch (error) { 499 let code = (error as BusinessError).code; 500 let message = (error as BusinessError).message; 501 console.error(`getStringSync failed, error code: ${code}, message: ${message}.`); 502 } 503 ``` 504 505### getStringByNameSync<sup>9+</sup> 506 507getStringByNameSync(resName: string): string 508 509Obtains the string corresponding to the specified resource name. This API returns the result synchronously. 510 511**System capability**: SystemCapability.Global.ResourceManager 512 513**Parameters** 514 515| Name | Type | Mandatory | Description | 516| ------- | ------ | ---- | ---- | 517| resName | string | Yes | Resource name.| 518 519**Return value** 520 521| Type | Description | 522| ------ | ---------- | 523| string | String corresponding to the specified resource name.| 524 525**Error codes** 526 527For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 528 529| ID| Error Message| 530| -------- | ---------------------------------------- | 531| 9001003 | If the resName invalid. | 532| 9001004 | If the resource not found by resName. | 533| 9001006 | If the resource re-ref too much. | 534 535**Example** 536 ```ts 537 import { BusinessError } from '@ohos.base'; 538 539 try { 540 this.context.resourceManager.getStringByNameSync("test"); 541 } catch (error) { 542 let code = (error as BusinessError).code; 543 let message = (error as BusinessError).message; 544 console.error(`getStringByNameSync failed, error code: ${code}, message: ${message}.`); 545 } 546 ``` 547 548### getStringByNameSync<sup>10+</sup> 549 550getStringByNameSync(resName: string, ...args: Array<string | number>): string 551 552Obtains the string corresponding to the specified resource name and formats the string based on **args**. This API returns the result synchronously. 553 554**System capability**: SystemCapability.Global.ResourceManager 555 556**Parameters** 557 558| Name | Type | Mandatory | Description | 559| ------- | ------ | ---- | ---- | 560| resName | string | Yes | Resource name.| 561| args | Array<string \| number> | No | Arguments for formatting strings.<br> Supported arguments:<br> %d, %f, %s, and %%<br> Note: **%%** is used to translate **%**.<br>Example: **%%d** is translated into the **%d** string.| 562 563**Return value** 564 565| Type | Description | 566| ------ | ---------------------------- | 567| string | Formatted string.| 568 569**Error codes** 570 571For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 572 573| ID| Error Message| 574| -------- | ---------------------------------------- | 575| 9001003 | If the resName invalid. | 576| 9001004 | If the resource not found by resName. | 577| 9001006 | If the resource re-ref too much. | 578| 9001008 | If the resource obtained by resName formatting error. | 579 580**Example** 581 ```ts 582 import { BusinessError } from '@ohos.base'; 583 584 try { 585 this.context.resourceManager.getStringByNameSync("test", "format string", 10, 98.78); 586 } catch (error) { 587 let code = (error as BusinessError).code; 588 let message = (error as BusinessError).message; 589 console.error(`getStringByNameSync failed, error code: ${code}, message: ${message}.`); 590 } 591 ``` 592 593### getStringValue<sup>9+</sup> 594 595getStringValue(resId: number, callback: AsyncCallback<string>): void 596 597Obtains the string corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 598 599**System capability**: SystemCapability.Global.ResourceManager 600 601**Parameters** 602 603| Name | Type | Mandatory | Description | 604| -------- | --------------------------- | ---- | --------------- | 605| resId | number | Yes | Resource ID. | 606| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 607 608**Error codes** 609 610For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 611 612| ID| Error Message| 613| -------- | ---------------------------------------- | 614| 9001001 | If the module resId invalid. | 615| 9001002 | If the resource not found by resId. | 616| 9001006 | If the resource re-ref too much. | 617 618**Example (stage)** 619 ```ts 620 import { BusinessError } from '@ohos.base'; 621 622 try { 623 this.context.resourceManager.getStringValue($r('app.string.test').id, (error, value) => { 624 if (error != null) { 625 console.log("error is " + error); 626 } else { 627 let str = value; 628 } 629 }); 630 } catch (error) { 631 let code = (error as BusinessError).code; 632 let message = (error as BusinessError).message; 633 console.error(`callback getStringValue failed, error code: ${code}, message: ${message}.`); 634 } 635 ``` 636 637### getStringValue<sup>9+</sup> 638 639getStringValue(resId: number): Promise<string> 640 641Obtains the string corresponding to the specified resource ID. This API uses a promise to return the result. 642 643**System capability**: SystemCapability.Global.ResourceManager 644 645**Parameters** 646 647| Name | Type | Mandatory | Description | 648| ----- | ------ | ---- | ----- | 649| resId | number | Yes | Resource ID.| 650 651**Return value** 652 653| Type | Description | 654| --------------------- | ----------- | 655| Promise<string> | Promise used to return the result.| 656 657**Error codes** 658 659For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 660 661| ID| Error Message| 662| -------- | ---------------------------------------- | 663| 9001001 | If the resId invalid. | 664| 9001002 | If the resource not found by resId. | 665| 9001006 | If the resource re-ref too much. | 666 667**Example** 668 ```ts 669 import { BusinessError } from '@ohos.base'; 670 671 try { 672 this.context.resourceManager.getStringValue($r('app.string.test').id).then((value: string) => { 673 let str = value; 674 }).catch((error: BusinessError) => { 675 console.log("getStringValue promise error is " + error); 676 }); 677 } catch (error) { 678 let code = (error as BusinessError).code; 679 let message = (error as BusinessError).message; 680 console.error(`promise getStringValue failed, error code: ${code}, message: ${message}.`); 681 } 682 ``` 683 684### getStringValue<sup>9+</sup> 685 686getStringValue(resource: Resource, callback: AsyncCallback<string>): void 687 688Obtains the string corresponding to the specified resource object. This API uses an asynchronous callback to return the result. 689 690**System capability**: SystemCapability.Global.ResourceManager 691 692**Model restriction**: This API can be used only in the stage model. 693 694**Parameters** 695 696| Name | Type | Mandatory | Description | 697| -------- | --------------------------- | ---- | --------------- | 698| resource | [Resource](#resource9) | Yes | Resource object. | 699| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 700 701**Error codes** 702 703For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 704 705| ID| Error Message| 706| -------- | ---------------------------------------- | 707| 9001001 | If the resId invalid. | 708| 9001002 | If the resource not found by resId. | 709| 9001006 | If the resource re-ref too much. | 710 711**Example** 712 ```ts 713 import resourceManager from '@ohos.resourceManager'; 714 import { BusinessError } from '@ohos.base'; 715 716 let resource: resourceManager.Resource = { 717 bundleName: "com.example.myapplication", 718 moduleName: "entry", 719 id: $r('app.string.test').id 720 }; 721 try { 722 this.context.resourceManager.getStringValue(resource, (error, value) => { 723 if (error != null) { 724 console.log("error is " + error); 725 } else { 726 let str = value; 727 } 728 }); 729 } catch (error) { 730 let code = (error as BusinessError).code; 731 let message = (error as BusinessError).message; 732 console.error(`callback getStringValue failed, error code: ${code}, message: ${message}.`); 733 } 734 ``` 735 736### getStringValue<sup>9+</sup> 737 738getStringValue(resource: Resource): Promise<string> 739 740Obtains the string corresponding to the specified resource object. This API uses a promise to return the result. 741 742**System capability**: SystemCapability.Global.ResourceManager 743 744**Model restriction**: This API can be used only in the stage model. 745 746**Parameters** 747 748| Name | Type | Mandatory | Description | 749| -------- | ---------------------- | ---- | ---- | 750| resource | [Resource](#resource9) | Yes | Resource object.| 751 752**Return value** 753 754| Type | Description | 755| --------------------- | ---------------- | 756| Promise<string> | Promise used to return the result.| 757 758**Error codes** 759 760For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 761 762| ID| Error Message| 763| -------- | ---------------------------------------- | 764| 9001001 | If the resId invalid. | 765| 9001002 | If the resource not found by resId. | 766| 9001006 | If the resource re-ref too much. | 767 768**Example** 769 ```ts 770 import resourceManager from '@ohos.resourceManager'; 771 import { BusinessError } from '@ohos.base'; 772 773 let resource: resourceManager.Resource = { 774 bundleName: "com.example.myapplication", 775 moduleName: "entry", 776 id: $r('app.string.test').id 777 }; 778 try { 779 this.context.resourceManager.getStringValue(resource).then((value: string) => { 780 let str = value; 781 }).catch((error: BusinessError) => { 782 console.log("getStringValue promise error is " + error); 783 }); 784 } catch (error) { 785 let code = (error as BusinessError).code; 786 let message = (error as BusinessError).message; 787 console.error(`promise getStringValue failed, error code: ${code}, message: ${message}.`); 788 } 789 ``` 790 791### getStringByName<sup>9+</sup> 792 793getStringByName(resName: string, callback: AsyncCallback<string>): void 794 795Obtains the string corresponding to the specified resource name. This API uses an asynchronous callback to return the result. 796 797**System capability**: SystemCapability.Global.ResourceManager 798 799**Parameters** 800 801| Name | Type | Mandatory | Description | 802| -------- | --------------------------- | ---- | --------------- | 803| resName | string | Yes | Resource name. | 804| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 805 806**Error codes** 807 808For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 809 810| ID| Error Message| 811| -------- | ---------------------------------------- | 812| 9001003 | If the resName invalid. | 813| 9001004 | If the resource not found by resName. | 814| 9001006 | If the resource re-ref too much. | 815 816**Example** 817 ```ts 818 import { BusinessError } from '@ohos.base'; 819 820 try { 821 this.context.resourceManager.getStringByName("test", (error, value) => { 822 if (error != null) { 823 console.log("error is " + error); 824 } else { 825 let str = value; 826 } 827 }); 828 } catch (error) { 829 let code = (error as BusinessError).code; 830 let message = (error as BusinessError).message; 831 console.error(`callback getStringByName failed, error code: ${code}, message: ${message}.`); 832 } 833 ``` 834 835### getStringByName<sup>9+</sup> 836 837getStringByName(resName: string): Promise<string> 838 839Obtains the string corresponding to the specified resource name. This API uses a promise to return the result. 840 841**System capability**: SystemCapability.Global.ResourceManager 842 843**Parameters** 844 845| Name | Type | Mandatory | Description | 846| ------- | ------ | ---- | ---- | 847| resName | string | Yes | Resource name.| 848 849**Return value** 850 851| Type | Description | 852| --------------------- | ---------- | 853| Promise<string> | Promise used to return the result.| 854 855**Error codes** 856 857For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 858 859| ID| Error Message| 860| -------- | ---------------------------------------- | 861| 9001003 | If the resName invalid. | 862| 9001004 | If the resource not found by resName. | 863| 9001006 | If the resource re-ref too much. | 864 865**Example** 866 ```ts 867 import { BusinessError } from '@ohos.base'; 868 869 try { 870 this.context.resourceManager.getStringByName("test").then((value: string) => { 871 let str = value; 872 }).catch((error: BusinessError) => { 873 console.log("getStringByName promise error is " + error); 874 }); 875 } catch (error) { 876 let code = (error as BusinessError).code; 877 let message = (error as BusinessError).message; 878 console.error(`promise getStringByName failed, error code: ${code}, message: ${message}.`); 879 } 880 ``` 881 882### getStringArrayValueSync<sup>10+</sup> 883 884getStringArrayValueSync(resId: number): Array<string> 885 886Obtains the string array corresponding to the specified resource ID. This API returns the result synchronously. 887 888**System capability**: SystemCapability.Global.ResourceManager 889 890**Parameters** 891 892| Name | Type | Mandatory | Description | 893| ----- | ------ | ---- | ----- | 894| resId | number | Yes | Resource ID.| 895 896**Return value** 897 898| Type | Description | 899| --------------------- | ----------- | 900| Array<string> | Promise used to return the result.| 901 902**Error codes** 903 904For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 905 906| ID| Error Message| 907| -------- | ---------------------------------------- | 908| 9001001 | If the resId invalid. | 909| 9001002 | If the resource not found by resId. | 910| 9001006 | If the resource re-ref too much. | 911 912**Example** 913 ```ts 914 import { BusinessError } from '@ohos.base'; 915 916 try { 917 this.context.resourceManager.getStringArrayValueSync($r('app.strarray.test').id); 918 } catch (error) { 919 let code = (error as BusinessError).code; 920 let message = (error as BusinessError).message; 921 console.error(`getStringArrayValueSync failed, error code: ${code}, message: ${message}.`); 922 } 923 ``` 924 925### getStringArrayValueSync<sup>10+</sup> 926 927getStringArrayValueSync(resource: Resource): Array<string> 928 929Obtains the string array corresponding to the specified resource object. This API returns the result synchronously. 930 931**System capability**: SystemCapability.Global.ResourceManager 932 933**Model restriction**: This API can be used only in the stage model. 934 935**Parameters** 936 937| Name | Type | Mandatory | Description | 938| ----- | ------ | ---- | ----- | 939| resource | [Resource](#resource9) | Yes | Resource object.| 940 941**Return value** 942 943| Type | Description | 944| --------------------- | ----------- | 945| Array<string> | Promise used to return the result.| 946 947**Error codes** 948 949For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 950 951| ID| Error Message| 952| -------- | ---------------------------------------- | 953| 9001001 | If the resId invalid. | 954| 9001002 | If the resource not found by resId. | 955| 9001006 | If the resource re-ref too much. | 956 957**Example** 958 ```ts 959 import resourceManager from '@ohos.resourceManager'; 960 import { BusinessError } from '@ohos.base'; 961 962 let resource: resourceManager.Resource = { 963 bundleName: "com.example.myapplication", 964 moduleName: "entry", 965 id: $r('app.strarray.test').id 966 }; 967 try { 968 this.context.resourceManager.getStringArrayValueSync(resource); 969 } catch (error) { 970 let code = (error as BusinessError).code; 971 let message = (error as BusinessError).message; 972 console.error(`getStringArrayValueSync failed, error code: ${code}, message: ${message}.`); 973 } 974 ``` 975 976### getStringArrayByNameSync<sup>10+</sup> 977 978getStringArrayByNameSync(resName: string): Array<string> 979 980Obtains the string array corresponding to the specified resource name. This API returns the result synchronously. 981 982**System capability**: SystemCapability.Global.ResourceManager 983 984**Parameters** 985 986| Name | Type | Mandatory | Description | 987| ----- | ------ | ---- | ----- | 988| resName | string | Yes | Resource name.| 989 990**Return value** 991 992| Type | Description | 993| --------------------- | ----------- | 994| Array<string> | String array corresponding to the specified resource name.| 995 996**Error codes** 997 998For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 999 1000| ID| Error Message| 1001| -------- | ---------------------------------------- | 1002| 9001003 | If the resName invalid. | 1003| 9001004 | If the resource not found by resName. | 1004| 9001006 | If the resource re-ref too much. | 1005 1006**Example** 1007 ```ts 1008 try { 1009 this.context.resourceManager.getStringArrayByNameSync("test"); 1010 } catch (error) { 1011 let code = (error as BusinessError).code; 1012 let message = (error as BusinessError).message; 1013 console.error(`getStringArrayByNameSync failed, error code: ${code}, message: ${message}.`); 1014 } 1015 ``` 1016 1017### getStringArrayValue<sup>9+</sup> 1018 1019getStringArrayValue(resId: number, callback: AsyncCallback<Array<string>>): void 1020 1021Obtains the string array corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 1022 1023**System capability**: SystemCapability.Global.ResourceManager 1024 1025**Parameters** 1026 1027| Name | Type | Mandatory | Description | 1028| -------- | ---------------------------------------- | ---- | ----------------- | 1029| resId | number | Yes | Resource ID. | 1030| callback | AsyncCallback<Array<string>> | Yes | Callback used to return the result.| 1031 1032**Error codes** 1033 1034For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1035 1036| ID| Error Message| 1037| -------- | ---------------------------------------- | 1038| 9001001 | If the resId invalid. | 1039| 9001002 | If the resource not found by resId. | 1040| 9001006 | If the resource re-ref too much. | 1041 1042**Example** 1043 ```ts 1044 import { BusinessError } from '@ohos.base'; 1045 1046 try { 1047 this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id, (error, value) => { 1048 if (error != null) { 1049 console.log("error is " + error); 1050 } else { 1051 let strArray = value; 1052 } 1053 }); 1054 } catch (error) { 1055 let code = (error as BusinessError).code; 1056 let message = (error as BusinessError).message; 1057 console.error(`callback getStringArrayValue failed, error code: ${code}, message: ${message}.`); 1058 } 1059 ``` 1060 1061### getStringArrayValue<sup>9+</sup> 1062 1063getStringArrayValue(resId: number): Promise<Array<string>> 1064 1065Obtains the string array corresponding to the specified resource ID. This API uses a promise to return the result. 1066 1067**System capability**: SystemCapability.Global.ResourceManager 1068 1069**Parameters** 1070 1071| Name | Type | Mandatory | Description | 1072| ----- | ------ | ---- | ----- | 1073| resId | number | Yes | Resource ID.| 1074 1075**Return value** 1076 1077| Type | Description | 1078| ---------------------------------- | ------------- | 1079| Promise<Array<string>> | Promise used to return the result.| 1080 1081**Error codes** 1082 1083For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1084 1085| ID| Error Message| 1086| -------- | ---------------------------------------- | 1087| 9001001 | If the resId invalid. | 1088| 9001002 | If the resource not found by resId. | 1089| 9001006 | If the resource re-ref too much. | 1090 1091**Example** 1092 ```ts 1093 import { BusinessError } from '@ohos.base'; 1094 1095 try { 1096 this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id).then((value: Array<string>) => { 1097 let strArray = value; 1098 }).catch((error: BusinessError) => { 1099 console.log("getStringArrayValue promise error is " + error); 1100 }); 1101 } catch (error) { 1102 let code = (error as BusinessError).code; 1103 let message = (error as BusinessError).message; 1104 console.error(`promise getStringArrayValue failed, error code: ${code}, message: ${message}.`); 1105 } 1106 ``` 1107 1108### getStringArrayValue<sup>9+</sup> 1109 1110getStringArrayValue(resource: Resource, callback: AsyncCallback<Array<string>>): void 1111 1112Obtains the string array corresponding to the specified resource object. This API uses an asynchronous callback to return the result. 1113 1114**System capability**: SystemCapability.Global.ResourceManager 1115 1116**Model restriction**: This API can be used only in the stage model. 1117 1118**Parameters** 1119 1120| Name | Type | Mandatory | Description | 1121| -------- | ---------------------------------------- | ---- | ----------------- | 1122| resource | [Resource](#resource9) | Yes | Resource object. | 1123| callback | AsyncCallback<Array<string>> | Yes | Callback used to return the result.| 1124 1125**Error codes** 1126 1127For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1128 1129| ID| Error Message| 1130| -------- | ---------------------------------------- | 1131| 9001001 | If the resId invalid. | 1132| 9001002 | If the resource not found by resId. | 1133| 9001006 | If the resource re-ref too much. | 1134 1135**Example** 1136 ```ts 1137 import resourceManager from '@ohos.resourceManager'; 1138 import { BusinessError } from '@ohos.base'; 1139 1140 let resource: resourceManager.Resource = { 1141 bundleName: "com.example.myapplication", 1142 moduleName: "entry", 1143 id: $r('app.strarray.test').id 1144 }; 1145 try { 1146 this.context.resourceManager.getStringArrayValue(resource, (error, value) => { 1147 if (error != null) { 1148 console.log("error is " + error); 1149 } else { 1150 let strArray = value; 1151 } 1152 }); 1153 } catch (error) { 1154 let code = (error as BusinessError).code; 1155 let message = (error as BusinessError).message; 1156 console.error(`callback getStringArrayValue failed, error code: ${code}, message: ${message}.`); 1157 } 1158 ``` 1159 1160### getStringArrayValue<sup>9+</sup> 1161 1162getStringArrayValue(resource: Resource): Promise<Array<string>> 1163 1164Obtains the string array corresponding to the specified resource object. This API uses a promise to return the result. 1165 1166**System capability**: SystemCapability.Global.ResourceManager 1167 1168**Model restriction**: This API can be used only in the stage model. 1169 1170**Parameters** 1171 1172| Name | Type | Mandatory | Description | 1173| -------- | ---------------------- | ---- | ---- | 1174| resource | [Resource](#resource9) | Yes | Resource object.| 1175 1176**Return value** 1177 1178| Type | Description | 1179| ---------------------------------- | ------------------ | 1180| Promise<Array<string>> | Promise used to return the result.| 1181 1182**Error codes** 1183 1184For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1185 1186| ID| Error Message| 1187| -------- | ---------------------------------------- | 1188| 9001001 | If the resId invalid. | 1189| 9001002 | If the resource not found by resId. | 1190| 9001006 | If the resource re-ref too much. | 1191 1192**Example** 1193 ```ts 1194 import resourceManager from '@ohos.resourceManager'; 1195 import { BusinessError } from '@ohos.base'; 1196 1197 let resource: resourceManager.Resource = { 1198 bundleName: "com.example.myapplication", 1199 moduleName: "entry", 1200 id: $r('app.strarray.test').id 1201 }; 1202 try { 1203 this.context.resourceManager.getStringArrayValue(resource).then((value: Array<string>) => { 1204 let strArray = value; 1205 }).catch((error: BusinessError) => { 1206 console.log("getStringArray promise error is " + error); 1207 }); 1208 } catch (error) { 1209 let code = (error as BusinessError).code; 1210 let message = (error as BusinessError).message; 1211 console.error(`promise getStringArrayValue failed, error code: ${code}, message: ${message}.`); 1212 } 1213 ``` 1214 1215### getStringArrayByName<sup>9+</sup> 1216 1217getStringArrayByName(resName: string, callback: AsyncCallback<Array<string>>): void 1218 1219Obtains the string array corresponding to the specified resource name. This API uses an asynchronous callback to return the result. 1220 1221**System capability**: SystemCapability.Global.ResourceManager 1222 1223**Parameters** 1224 1225| Name | Type | Mandatory | Description | 1226| -------- | ---------------------------------------- | ---- | ----------------- | 1227| resName | string | Yes | Resource name. | 1228| callback | AsyncCallback<Array<string>> | Yes | Callback used to return the result.| 1229 1230**Error codes** 1231 1232For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1233 1234| ID| Error Message| 1235| -------- | ---------------------------------------- | 1236| 9001003 | If the resName invalid. | 1237| 9001004 | If the resource not found by resName. | 1238| 9001006 | If the resource re-ref too much. | 1239 1240**Example** 1241 ```ts 1242 import { BusinessError } from '@ohos.base'; 1243 1244 try { 1245 this.context.resourceManager.getStringArrayByName("test", (error, value) => { 1246 if (error != null) { 1247 console.log("error is " + error); 1248 } else { 1249 let strArray = value; 1250 } 1251 }); 1252 } catch (error) { 1253 let code = (error as BusinessError).code; 1254 let message = (error as BusinessError).message; 1255 console.error(`callback getStringArrayByName failed, error code: ${code}, message: ${message}.`); 1256 } 1257 ``` 1258 1259### getStringArrayByName<sup>9+</sup> 1260 1261getStringArrayByName(resName: string): Promise<Array<string>> 1262 1263Obtains the string array corresponding to the specified resource name. This API uses a promise to return the result. 1264 1265**System capability**: SystemCapability.Global.ResourceManager 1266 1267**Parameters** 1268 1269| Name | Type | Mandatory | Description | 1270| ------- | ------ | ---- | ---- | 1271| resName | string | Yes | Resource name.| 1272 1273**Return value** 1274 1275| Type | Description | 1276| ---------------------------------- | ------------ | 1277| Promise<Array<string>> | Promise used to return the result.| 1278 1279**Error codes** 1280 1281For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1282 1283| ID| Error Message| 1284| -------- | ---------------------------------------- | 1285| 9001003 | If the resName invalid. | 1286| 9001004 | If the resource not found by resName. | 1287| 9001006 | If the resource re-ref too much. | 1288 1289**Example** 1290 ```ts 1291 import { BusinessError } from '@ohos.base'; 1292 1293 try { 1294 this.context.resourceManager.getStringArrayByName("test").then((value: Array<string>) => { 1295 let strArray = value; 1296 }).catch((error: BusinessError) => { 1297 console.log("getStringArrayByName promise error is " + error); 1298 }); 1299 } catch (error) { 1300 let code = (error as BusinessError).code; 1301 let message = (error as BusinessError).message; 1302 console.error(`promise getStringArrayByName failed, error code: ${code}, message: ${message}.`); 1303 } 1304 ``` 1305 1306### getPluralStringValueSync<sup>10+</sup> 1307 1308getPluralStringValueSync(resId: number, num: number): string 1309 1310Obtains the singular-plural string corresponding to the specified resource ID based on the specified number. This API returns the result synchronously. 1311 1312**System capability**: SystemCapability.Global.ResourceManager 1313 1314**Parameters** 1315 1316| Name | Type | Mandatory | Description | 1317| ----- | ------ | ---- | ----- | 1318| resId | number | Yes | Resource ID.| 1319| num | number | Yes | Number. | 1320 1321**Return value** 1322 1323| Type | Description | 1324| -------- | ----------- | 1325| string | Singular-plural string corresponding to the specified resource ID.| 1326 1327**Error codes** 1328 1329For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1330 1331| ID| Error Message| 1332| -------- | ---------------------------------------- | 1333| 9001001 | If the resId invalid. | 1334| 9001002 | If the resource not found by resId. | 1335| 9001006 | If the resource re-ref too much. | 1336 1337**Example** 1338 ```ts 1339 import { BusinessError } from '@ohos.base'; 1340 1341 try { 1342 this.context.resourceManager.getPluralStringValueSync($r('app.plural.test').id, 1); 1343 } catch (error) { 1344 let code = (error as BusinessError).code; 1345 let message = (error as BusinessError).message; 1346 console.error(`getPluralStringValueSync failed, error code: ${code}, message: ${message}.`); 1347 } 1348 ``` 1349 1350### getPluralStringValueSync<sup>10+</sup> 1351 1352getPluralStringValueSync(resource: Resource, num: number): string 1353 1354Obtains the singular-plural string corresponding to the specified resource object based on the specified number. This API returns the result synchronously. 1355 1356**System capability**: SystemCapability.Global.ResourceManager 1357 1358**Model restriction**: This API can be used only in the stage model. 1359 1360**Parameters** 1361 1362| Name | Type | Mandatory | Description | 1363| ----- | ------ | ---- | ----- | 1364| resource | [Resource](#resource9) | Yes | Resource object.| 1365| num | number | Yes | Number. | 1366 1367**Return value** 1368 1369| Type | Description | 1370| --------------------- | ----------- | 1371| string | Singular-plural string corresponding to the specified resource object.| 1372 1373**Error codes** 1374 1375For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1376 1377| ID| Error Message| 1378| -------- | ---------------------------------------- | 1379| 9001001 | If the resId invalid. | 1380| 9001002 | If the resource not found by resId. | 1381| 9001006 | If the resource re-ref too much. | 1382 1383**Example** 1384 ```ts 1385 import resourceManager from '@ohos.resourceManager'; 1386 import { BusinessError } from '@ohos.base'; 1387 1388 let resource: resourceManager.Resource = { 1389 bundleName: "com.example.myapplication", 1390 moduleName: "entry", 1391 id: $r('app.plural.test').id 1392 }; 1393 try { 1394 this.context.resourceManager.getPluralStringValueSync(resource, 1); 1395 } catch (error) { 1396 let code = (error as BusinessError).code; 1397 let message = (error as BusinessError).message; 1398 console.error(`getPluralStringValueSync failed, error code: ${code}, message: ${message}.`); 1399 } 1400 ``` 1401 1402### getPluralStringByNameSync<sup>10+</sup> 1403 1404getPluralStringByNameSync(resName: string, num: number): string 1405 1406Obtains the singular-plural string corresponding to the specified resource name based on the specified number. This API returns the result synchronously. 1407 1408**System capability**: SystemCapability.Global.ResourceManager 1409 1410**Parameters** 1411 1412| Name | Type | Mandatory | Description | 1413| ----- | ------ | ---- | ----- | 1414| resName | string | Yes | Resource name.| 1415| num | number | Yes | Number. | 1416 1417**Return value** 1418 1419| Type | Description | 1420| --------------------- | ----------- | 1421| string | Singular-plural string corresponding to the specified resource name.| 1422 1423**Error codes** 1424 1425For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1426 1427| ID| Error Message| 1428| -------- | ---------------------------------------- | 1429| 9001003 | If the resName invalid. | 1430| 9001004 | If the resource not found by resName. | 1431| 9001006 | If the resource re-ref too much. | 1432 1433**Example** 1434 ```ts 1435 import { BusinessError } from '@ohos.base'; 1436 1437 try { 1438 this.context.resourceManager.getPluralStringByNameSync("test", 1); 1439 } catch (error) { 1440 let code = (error as BusinessError).code; 1441 let message = (error as BusinessError).message; 1442 console.error(`getPluralStringByNameSync failed, error code: ${code}, message: ${message}.`); 1443 } 1444 ``` 1445 1446### getPluralStringValue<sup>9+</sup> 1447 1448getPluralStringValue(resId: number, num: number, callback: AsyncCallback<string>): void 1449 1450Obtains 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. 1451 1452**System capability**: SystemCapability.Global.ResourceManager 1453 1454**Parameters** 1455 1456| Name | Type | Mandatory | Description | 1457| -------- | --------------------------- | ---- | ------------------------------- | 1458| resId | number | Yes | Resource ID. | 1459| num | number | Yes | Number. | 1460| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 1461 1462**Error codes** 1463 1464For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1465 1466| ID| Error Message| 1467| -------- | ---------------------------------------- | 1468| 9001001 | If the resId invalid. | 1469| 9001002 | If the resource not found by resId. | 1470| 9001006 | If the resource re-ref too much. | 1471 1472**Example** 1473 ```ts 1474 import { BusinessError } from '@ohos.base'; 1475 1476 try { 1477 this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1, (error, value) => { 1478 if (error != null) { 1479 console.log("error is " + error); 1480 } else { 1481 let str = value; 1482 } 1483 }); 1484 } catch (error) { 1485 let code = (error as BusinessError).code; 1486 let message = (error as BusinessError).message; 1487 console.error(`callback getPluralStringValue failed, error code: ${code}, message: ${message}.`); 1488 } 1489 ``` 1490 1491### getPluralStringValue<sup>9+</sup> 1492 1493getPluralStringValue(resId: number, num: number): Promise<string> 1494 1495Obtains the singular-plural string corresponding to the specified resource ID based on the specified number. This API uses a promise to return the result. 1496 1497**System capability**: SystemCapability.Global.ResourceManager 1498 1499**Parameters** 1500 1501| Name | Type | Mandatory | Description | 1502| ----- | ------ | ---- | ----- | 1503| resId | number | Yes | Resource ID.| 1504| num | number | Yes | Number. | 1505 1506**Return value** 1507 1508| Type | Description | 1509| --------------------- | ------------------------- | 1510| Promise<string> | Promise used to return the result.| 1511 1512**Error codes** 1513 1514For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1515 1516| ID| Error Message| 1517| -------- | ---------------------------------------- | 1518| 9001001 | If the resId invalid. | 1519| 9001002 | If the resource not found by resId. | 1520| 9001006 | If the resource re-ref too much. | 1521 1522**Example** 1523 ```ts 1524 import { BusinessError } from '@ohos.base'; 1525 1526 try { 1527 this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1).then((value: string) => { 1528 let str = value; 1529 }).catch((error: BusinessError) => { 1530 console.log("getPluralStringValue promise error is " + error); 1531 }); 1532 } catch (error) { 1533 let code = (error as BusinessError).code; 1534 let message = (error as BusinessError).message; 1535 console.error(`promise getPluralStringValue failed, error code: ${code}, message: ${message}.`); 1536 } 1537 ``` 1538 1539### getPluralStringValue<sup>9+</sup> 1540 1541getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback<string>): void 1542 1543Obtains 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. 1544 1545**System capability**: SystemCapability.Global.ResourceManager 1546 1547**Model restriction**: This API can be used only in the stage model. 1548 1549**Parameters** 1550 1551| Name | Type | Mandatory | Description | 1552| -------- | --------------------------- | ---- | ------------------------------------ | 1553| resource | [Resource](#resource9) | Yes | Resource object. | 1554| num | number | Yes | Number. | 1555| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 1556 1557**Error codes** 1558 1559For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1560 1561| ID| Error Message| 1562| -------- | ---------------------------------------- | 1563| 9001001 | If the resId invalid. | 1564| 9001002 | If the resource not found by resId. | 1565| 9001006 | If the resource re-ref too much. | 1566 1567**Example** 1568 ```ts 1569 import resourceManager from '@ohos.resourceManager'; 1570 import { BusinessError } from '@ohos.base'; 1571 1572 let resource: resourceManager.Resource = { 1573 bundleName: "com.example.myapplication", 1574 moduleName: "entry", 1575 id: $r('app.plural.test').id 1576 }; 1577 try { 1578 this.context.resourceManager.getPluralStringValue(resource, 1, (error, value) => { 1579 if (error != null) { 1580 console.log("error is " + error); 1581 } else { 1582 let str = value; 1583 } 1584 }); 1585 } catch (error) { 1586 let code = (error as BusinessError).code; 1587 let message = (error as BusinessError).message; 1588 console.error(`callback getPluralStringValue failed, error code: ${code}, message: ${message}.`); 1589 } 1590 ``` 1591 1592### getPluralStringValue<sup>9+</sup> 1593 1594getPluralStringValue(resource: Resource, num: number): Promise<string> 1595 1596Obtains the singular-plural string corresponding to the specified resource object based on the specified number. This API uses a promise to return the result. 1597 1598**System capability**: SystemCapability.Global.ResourceManager 1599 1600**Model restriction**: This API can be used only in the stage model. 1601 1602**Parameters** 1603 1604| Name | Type | Mandatory | Description | 1605| -------- | ---------------------- | ---- | ---- | 1606| resource | [Resource](#resource9) | Yes | Resource object.| 1607| num | number | Yes | Number. | 1608 1609**Return value** 1610 1611| Type | Description | 1612| --------------------- | ------------------------------ | 1613| Promise<string> | Promise used to return the result.| 1614 1615**Error codes** 1616 1617For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1618 1619| ID| Error Message| 1620| -------- | ---------------------------------------- | 1621| 9001001 | If the resId invalid. | 1622| 9001002 | If the resource not found by resId. | 1623| 9001006 | If the resource re-ref too much. | 1624 1625**Example** 1626 ```ts 1627 import resourceManager from '@ohos.resourceManager'; 1628 import { BusinessError } from '@ohos.base'; 1629 1630 let resource: resourceManager.Resource = { 1631 bundleName: "com.example.myapplication", 1632 moduleName: "entry", 1633 id: $r('app.plural.test').id 1634 }; 1635 try { 1636 this.context.resourceManager.getPluralStringValue(resource, 1).then((value: string) => { 1637 let str = value; 1638 }).catch((error: BusinessError) => { 1639 console.log("getPluralStringValue promise error is " + error); 1640 }); 1641 } catch (error) { 1642 let code = (error as BusinessError).code; 1643 let message = (error as BusinessError).message; 1644 console.error(`promise getPluralStringValue failed, error code: ${code}, message: ${message}.`); 1645 } 1646 ``` 1647 1648### getPluralStringByName<sup>9+</sup> 1649 1650getPluralStringByName(resName: string, num: number, callback: AsyncCallback<string>): void 1651 1652Obtains the plural string corresponding to the specified resource name based on the specified number. This API uses an asynchronous callback to return the result. 1653 1654**System capability**: SystemCapability.Global.ResourceManager 1655 1656**Parameters** 1657 1658| Name | Type | Mandatory | Description | 1659| -------- | --------------------------- | ---- | ----------------------------- | 1660| resName | string | Yes | Resource name. | 1661| num | number | Yes | Number. | 1662| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 1663 1664**Error codes** 1665 1666For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1667 1668| ID| Error Message| 1669| -------- | ---------------------------------------- | 1670| 9001003 | If the resName invalid. | 1671| 9001004 | If the resource not found by resName. | 1672| 9001006 | If the resource re-ref too much. | 1673 1674**Example** 1675 ```ts 1676 import { BusinessError } from '@ohos.base'; 1677 1678 try { 1679 this.context.resourceManager.getPluralStringByName("test", 1, (error, value) => { 1680 if (error != null) { 1681 console.log("error is " + error); 1682 } else { 1683 let str = value; 1684 } 1685 }); 1686 } catch (error) { 1687 let code = (error as BusinessError).code; 1688 let message = (error as BusinessError).message; 1689 console.error(`callback getPluralStringByName failed, error code: ${code}, message: ${message}.`); 1690 } 1691 ``` 1692 1693### getPluralStringByName<sup>9+</sup> 1694 1695getPluralStringByName(resName: string, num: number): Promise<string> 1696 1697Obtains the plural string corresponding to the specified resource name based on the specified number. This API uses a promise to return the result. 1698 1699**System capability**: SystemCapability.Global.ResourceManager 1700 1701**Parameters** 1702 1703| Name | Type | Mandatory | Description | 1704| ------- | ------ | ---- | ---- | 1705| resName | string | Yes | Resource name.| 1706| num | number | Yes | Number. | 1707 1708**Return value** 1709 1710| Type | Description | 1711| --------------------- | ---------------------- | 1712| Promise<string> | Promise used to return the result.| 1713 1714**Error codes** 1715 1716For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1717 1718| ID| Error Message| 1719| -------- | ---------------------------------------- | 1720| 9001003 | If the resName invalid. | 1721| 9001004 | If the resource not found by resName. | 1722| 9001006 | If the resource re-ref too much. | 1723 1724**Example** 1725 ```ts 1726 import { BusinessError } from '@ohos.base'; 1727 1728 try { 1729 this.context.resourceManager.getPluralStringByName("test", 1).then((value: string) => { 1730 let str = value; 1731 }).catch((error: BusinessError) => { 1732 console.log("getPluralStringByName promise error is " + error); 1733 }); 1734 } catch (error) { 1735 let code = (error as BusinessError).code; 1736 let message = (error as BusinessError).message; 1737 console.error(`promise getPluralStringByName failed, error code: ${code}, message: ${message}.`); 1738 } 1739 ``` 1740 1741### getMediaContentSync<sup>10+</sup> 1742 1743getMediaContentSync(resId: number, density?: number): Uint8Array 1744 1745Obtains the media file content (with the default or specified screen density) corresponding to the specified resource ID. This API returns the result synchronously. 1746 1747**System capability**: SystemCapability.Global.ResourceManager 1748 1749**Parameters** 1750 1751| Name | Type | Mandatory | Description | 1752| ----- | ------ | ---- | ----- | 1753| resId | number | Yes | Resource ID.| 1754| [density](#screendensity) | number | No | Screen density. The default value or value **0** indicates the default screen density.| 1755 1756**Return value** 1757 1758| Type | Description | 1759| -------- | ----------- | 1760| Uint8Array | Media file content corresponding to the specified resource ID.| 1761 1762**Error codes** 1763 1764For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1765 1766| ID| Error Message| 1767| -------- | ---------------------------------------- | 1768| 9001001 | If the resId invalid. | 1769| 9001002 | If the resource not found by resId. | 1770 1771**Example** 1772 ```ts 1773 import { BusinessError } from '@ohos.base'; 1774 1775 try { 1776 this.context.resourceManager.getMediaContentSync($r('app.media.test').id); // Default screen density 1777 } catch (error) { 1778 let code = (error as BusinessError).code; 1779 let message = (error as BusinessError).message; 1780 console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`); 1781 } 1782 1783 try { 1784 this.context.resourceManager.getMediaContentSync($r('app.media.test').id, 120); // Specified screen density 1785 } catch (error) { 1786 let code = (error as BusinessError).code; 1787 let message = (error as BusinessError).message; 1788 console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`); 1789 } 1790 ``` 1791 1792### getMediaContentSync<sup>10+</sup> 1793 1794getMediaContentSync(resource: Resource, density?: number): Uint8Array 1795 1796Obtains the media file content (with the default or specified screen density) corresponding to the specified resource object. This API returns the result synchronously. 1797 1798**System capability**: SystemCapability.Global.ResourceManager 1799 1800**Model restriction**: This API can be used only in the stage model. 1801 1802**Parameters** 1803 1804| Name | Type | Mandatory | Description | 1805| ----- | ------ | ---- | ----- | 1806| resource | [Resource](#resource9) | Yes | Resource object.| 1807| [density](#screendensity) | number | No | Screen density. The default value or value **0** indicates the default screen density.| 1808 1809**Return value** 1810 1811| Type | Description | 1812| --------------------- | ----------- | 1813| Uint8Array | Promise used to return the result.| 1814 1815**Error codes** 1816 1817For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1818 1819| ID| Error Message| 1820| -------- | ---------------------------------------- | 1821| 9001001 | If the resId invalid. | 1822| 9001002 | If the resource not found by resId. | 1823 1824**Example** 1825 ```ts 1826 import resourceManager from '@ohos.resourceManager'; 1827 import { BusinessError } from '@ohos.base'; 1828 1829 let resource: resourceManager.Resource = { 1830 bundleName: "com.example.myapplication", 1831 moduleName: "entry", 1832 id: $r('app.media.test').id 1833 }; 1834 try { 1835 this.context.resourceManager.getMediaContentSync(resource); // Default screen density 1836 } catch (error) { 1837 let code = (error as BusinessError).code; 1838 let message = (error as BusinessError).message; 1839 console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`); 1840 } 1841 1842 try { 1843 this.context.resourceManager.getMediaContentSync(resource, 120); // Specified screen density 1844 } catch (error) { 1845 let code = (error as BusinessError).code; 1846 let message = (error as BusinessError).message; 1847 console.error(`getMediaContentSync failed, error code: ${code}, message: ${message}.`); 1848 } 1849 ``` 1850 1851### getMediaByNameSync<sup>10+</sup> 1852 1853getMediaByNameSync(resName: string, density?: number): Uint8Array 1854 1855Obtains the media file content (with the default or specified screen density) corresponding to the specified resource name. This API returns the result synchronously. 1856 1857**System capability**: SystemCapability.Global.ResourceManager 1858 1859**Parameters** 1860 1861| Name | Type | Mandatory | Description | 1862| ----- | ------ | ---- | ----- | 1863| resName | string | Yes | Resource name.| 1864| [density](#screendensity) | number | No | Screen density. The default value or value **0** indicates the default screen density.| 1865 1866**Return value** 1867 1868| Type | Description | 1869| --------------------- | ----------- | 1870| Uint8Array | Media file content corresponding to the resource name.| 1871 1872**Error codes** 1873 1874For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1875 1876| ID| Error Message| 1877| -------- | ---------------------------------------- | 1878| 9001003 | If the resName invalid. | 1879| 9001004 | If the resource not found by resName. | 1880 1881**Example** 1882 ```ts 1883 import { BusinessError } from '@ohos.base'; 1884 1885 try { 1886 this.context.resourceManager.getMediaByNameSync("test"); // Default screen density 1887 } catch (error) { 1888 let code = (error as BusinessError).code; 1889 let message = (error as BusinessError).message; 1890 console.error(`getMediaByNameSync failed, error code: ${code}, message: ${message}.`); 1891 } 1892 1893 try { 1894 this.context.resourceManager.getMediaByNameSync("test", 120); // Specified screen density 1895 } catch (error) { 1896 let code = (error as BusinessError).code; 1897 let message = (error as BusinessError).message; 1898 console.error(`getMediaByNameSync failed, error code: ${code}, message: ${message}.`); 1899 } 1900 ``` 1901 1902### getMediaContent<sup>9+</sup> 1903 1904getMediaContent(resId: number, callback: AsyncCallback<Uint8Array>): void 1905 1906Obtains the media file content corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 1907 1908**System capability**: SystemCapability.Global.ResourceManager 1909 1910**Parameters** 1911 1912| Name | Type | Mandatory | Description | 1913| -------- | ------------------------------- | ---- | ------------------ | 1914| resId | number | Yes | Resource ID. | 1915| callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| 1916 1917**Error codes** 1918 1919For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1920 1921| ID| Error Message| 1922| -------- | ---------------------------------------- | 1923| 9001001 | If the resId invalid. | 1924| 9001002 | If the resource not found by resId. | 1925 1926**Example** 1927 ```ts 1928 import { BusinessError } from '@ohos.base'; 1929 1930 try { 1931 this.context.resourceManager.getMediaContent($r('app.media.test').id, (error, value) => { 1932 if (error != null) { 1933 console.log("error is " + error); 1934 } else { 1935 let media = value; 1936 } 1937 }); 1938 } catch (error) { 1939 let code = (error as BusinessError).code; 1940 let message = (error as BusinessError).message; 1941 console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`); 1942 } 1943 ``` 1944 1945### getMediaContent<sup>10+</sup> 1946 1947getMediaContent(resId: number, density: number, callback: AsyncCallback<Uint8Array>): void 1948 1949Obtains the media file content (with the specified screen density) corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 1950 1951**System capability**: SystemCapability.Global.ResourceManager 1952 1953**Parameters** 1954 1955| Name | Type | Mandatory | Description | 1956| -------- | ------------------------------- | ---- | ------------------ | 1957| resId | number | Yes | Resource ID. | 1958| [density](#screendensity) | number | Yes | Screen density. The value **0** indicates the default screen density. | 1959| callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| 1960 1961**Error codes** 1962 1963For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 1964 1965| ID| Error Message| 1966| -------- | ---------------------------------------- | 1967| 9001001 | If the resId invalid. | 1968| 9001002 | If the resource not found by resId. | 1969 1970**Example** 1971 ```ts 1972 import { BusinessError } from '@ohos.base'; 1973 1974 try { 1975 this.context.resourceManager.getMediaContent($r('app.media.test').id, 120, (error, value) => { 1976 if (error != null) { 1977 console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); 1978 } else { 1979 let media = value; 1980 } 1981 }); 1982 } catch (error) { 1983 let code = (error as BusinessError).code; 1984 let message = (error as BusinessError).message; 1985 console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`); 1986 } 1987 ``` 1988 1989### getMediaContent<sup>9+</sup> 1990 1991getMediaContent(resId: number): Promise<Uint8Array> 1992 1993Obtains the media file content corresponding to the specified resource ID. This API uses a promise to return the result. 1994 1995**System capability**: SystemCapability.Global.ResourceManager 1996 1997**Parameters** 1998 1999| Name | Type | Mandatory | Description | 2000| ----- | ------ | ---- | ----- | 2001| resId | number | Yes | Resource ID.| 2002 2003**Return value** 2004 2005| Type | Description | 2006| ------------------------- | -------------- | 2007| Promise<Uint8Array> | Promise used to return the result.| 2008 2009**Error codes** 2010 2011For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2012 2013| ID| Error Message| 2014| -------- | ---------------------------------------- | 2015| 9001001 | If the resId invalid. | 2016| 9001002 | If the resource not found by resId. | 2017 2018**Example** 2019 ```ts 2020 import { BusinessError } from '@ohos.base'; 2021 2022 try { 2023 this.context.resourceManager.getMediaContent($r('app.media.test').id).then((value: Uint8Array) => { 2024 let media = value; 2025 }).catch((error: BusinessError) => { 2026 console.log("getMediaContent promise error is " + error); 2027 }); 2028 } catch (error) { 2029 let code = (error as BusinessError).code; 2030 let message = (error as BusinessError).message; 2031 console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`); 2032 } 2033 ``` 2034 2035### getMediaContent<sup>10+</sup> 2036 2037getMediaContent(resId: number, density: number): Promise<Uint8Array> 2038 2039Obtains the media file content (with the specified screen density) corresponding to the specified resource ID. This API uses a promise to return the result. 2040 2041**System capability**: SystemCapability.Global.ResourceManager 2042 2043**Parameters** 2044 2045| Name | Type | Mandatory | Description | 2046| ----- | ------ | ---- | ----- | 2047| resId | number | Yes | Resource ID.| 2048| [density](#screendensity) | number | Yes | Screen density. The value **0** indicates the default screen density. | 2049 2050**Return value** 2051 2052| Type | Description | 2053| ------------------------- | -------------- | 2054| Promise<Uint8Array> | Promise used to return the result.| 2055 2056**Error codes** 2057 2058For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2059 2060| ID| Error Message| 2061| -------- | ---------------------------------------- | 2062| 9001001 | If the resId invalid. | 2063| 9001002 | If the resource not found by resId. | 2064 2065**Example** 2066 ```ts 2067 import { BusinessError } from '@ohos.base'; 2068 2069 try { 2070 this.context.resourceManager.getMediaContent($r('app.media.test').id, 120).then((value: Uint8Array) => { 2071 let media = value; 2072 }).catch((error: BusinessError) => { 2073 console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); 2074 }); 2075 } catch (error) { 2076 let code = (error as BusinessError).code; 2077 let message = (error as BusinessError).message; 2078 console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`); 2079 } 2080 ``` 2081 2082### getMediaContent<sup>9+</sup> 2083 2084getMediaContent(resource: Resource, callback: AsyncCallback<Uint8Array>): void 2085 2086Obtains the media file content corresponding to the specified resource object. This API uses an asynchronous callback to return the result. 2087 2088**System capability**: SystemCapability.Global.ResourceManager 2089 2090**Model restriction**: This API can be used only in the stage model. 2091 2092**Parameters** 2093 2094| Name | Type | Mandatory | Description | 2095| -------- | ------------------------------- | ---- | ------------------ | 2096| resource | [Resource](#resource9) | Yes | Resource object. | 2097| callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| 2098 2099**Error codes** 2100 2101For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2102 2103| ID| Error Message| 2104| -------- | ---------------------------------------- | 2105| 9001001 | If the resId invalid. | 2106| 9001002 | If the resource not found by resId. | 2107 2108**Example** 2109 ```ts 2110 import resourceManager from '@ohos.resourceManager'; 2111 import { BusinessError } from '@ohos.base'; 2112 2113 let resource: resourceManager.Resource = { 2114 bundleName: "com.example.myapplication", 2115 moduleName: "entry", 2116 id: $r('app.media.test').id 2117 }; 2118 try { 2119 this.context.resourceManager.getMediaContent(resource, (error, value) => { 2120 if (error != null) { 2121 console.log("error is " + error); 2122 } else { 2123 let media = value; 2124 } 2125 }); 2126 } catch (error) { 2127 let code = (error as BusinessError).code; 2128 let message = (error as BusinessError).message; 2129 console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`); 2130 } 2131 ``` 2132 2133### getMediaContent<sup>10+</sup> 2134 2135getMediaContent(resource: Resource, density: number, callback: AsyncCallback<Uint8Array>): void 2136 2137Obtains the media file content (with the specified screen density) corresponding to the specified resource object. This API uses an asynchronous callback to return the result. 2138 2139**System capability**: SystemCapability.Global.ResourceManager 2140 2141**Model restriction**: This API can be used only in the stage model. 2142 2143**Parameters** 2144 2145| Name | Type | Mandatory | Description | 2146| -------- | ------------------------------- | ---- | ------------------ | 2147| resource | [Resource](#resource9) | Yes | Resource object. | 2148| [density](#screendensity) | number | Yes | Screen density. The value **0** indicates the default screen density. | 2149| callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| 2150 2151**Error codes** 2152 2153For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2154 2155| ID| Error Message| 2156| -------- | ---------------------------------------- | 2157| 9001001 | If the resId invalid. | 2158| 9001002 | If the resource not found by resId. | 2159 2160**Example** 2161 ```ts 2162 import resourceManager from '@ohos.resourceManager'; 2163 import { BusinessError } from '@ohos.base'; 2164 2165 let resource: resourceManager.Resource = { 2166 bundleName: "com.example.myapplication", 2167 moduleName: "entry", 2168 id: $r('app.media.test').id 2169 }; 2170 try { 2171 this.context.resourceManager.getMediaContent(resource, 120, (error, value) => { 2172 if (error != null) { 2173 console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); 2174 } else { 2175 let media = value; 2176 } 2177 }); 2178 } catch (error) { 2179 let code = (error as BusinessError).code; 2180 let message = (error as BusinessError).message; 2181 console.error(`callback getMediaContent failed, error code: ${code}, message: ${message}.`); 2182 } 2183 ``` 2184 2185### getMediaContent<sup>9+</sup> 2186 2187getMediaContent(resource: Resource): Promise<Uint8Array> 2188 2189Obtains the media file content corresponding to the specified resource object. This API uses a promise to return the result. 2190 2191**System capability**: SystemCapability.Global.ResourceManager 2192 2193**Model restriction**: This API can be used only in the stage model. 2194 2195**Parameters** 2196 2197| Name | Type | Mandatory | Description | 2198| -------- | ---------------------- | ---- | ---- | 2199| resource | [Resource](#resource9) | Yes | Resource object.| 2200 2201**Return value** 2202 2203| Type | Description | 2204| ------------------------- | ------------------- | 2205| Promise<Uint8Array> | Promise used to return the result.| 2206 2207**Error codes** 2208 2209For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2210 2211| ID| Error Message| 2212| -------- | ---------------------------------------- | 2213| 9001001 | If the resId invalid. | 2214| 9001002 | If the resource not found by resId. | 2215 2216**Example** 2217 ```ts 2218 import resourceManager from '@ohos.resourceManager'; 2219 import { BusinessError } from '@ohos.base'; 2220 2221 let resource: resourceManager.Resource = { 2222 bundleName: "com.example.myapplication", 2223 moduleName: "entry", 2224 id: $r('app.media.test').id 2225 }; 2226 try { 2227 this.context.resourceManager.getMediaContent(resource).then((value: Uint8Array) => { 2228 let media = value; 2229 }).catch((error: BusinessError) => { 2230 console.log("getMediaContent promise error is " + error); 2231 }); 2232 } catch (error) { 2233 let code = (error as BusinessError).code; 2234 let message = (error as BusinessError).message; 2235 console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`); 2236 } 2237 ``` 2238 2239### getMediaContent<sup>10+</sup> 2240 2241getMediaContent(resource: Resource, density: number): Promise<Uint8Array> 2242 2243Obtains the media file content (with the specified screen density) corresponding to the specified resource object. This API uses a promise to return the result. 2244 2245**System capability**: SystemCapability.Global.ResourceManager 2246 2247**Model restriction**: This API can be used only in the stage model. 2248 2249**Parameters** 2250 2251| Name | Type | Mandatory | Description | 2252| -------- | ---------------------- | ---- | ---- | 2253| resource | [Resource](#resource9) | Yes | Resource object.| 2254| [density](#screendensity) | number | Yes | Screen density. The value **0** indicates the default screen density. | 2255 2256**Return value** 2257 2258| Type | Description | 2259| ------------------------- | ------------------- | 2260| Promise<Uint8Array> | Promise used to return the result.| 2261 2262**Error codes** 2263 2264For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2265 2266| ID| Error Message| 2267| -------- | ---------------------------------------- | 2268| 9001001 | If the resId invalid. | 2269| 9001002 | If the resource not found by resId. | 2270 2271**Example** 2272 ```ts 2273 import resourceManager from '@ohos.resourceManager'; 2274 import { BusinessError } from '@ohos.base'; 2275 2276 let resource: resourceManager.Resource = { 2277 bundleName: "com.example.myapplication", 2278 moduleName: "entry", 2279 id: $r('app.media.test').id 2280 }; 2281 try { 2282 this.context.resourceManager.getMediaContent(resource, 120).then((value: Uint8Array) => { 2283 let media = value; 2284 }).catch((error: BusinessError) => { 2285 console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`); 2286 }); 2287 } catch (error) { 2288 let code = (error as BusinessError).code; 2289 let message = (error as BusinessError).message; 2290 console.error(`promise getMediaContent failed, error code: ${code}, message: ${message}.`); 2291 } 2292 ``` 2293 2294### getMediaByName<sup>9+</sup> 2295 2296getMediaByName(resName: string, callback: AsyncCallback<Uint8Array>): void 2297 2298Obtains the media file content corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 2299 2300**System capability**: SystemCapability.Global.ResourceManager 2301 2302**Parameters** 2303 2304| Name | Type | Mandatory | Description | 2305| -------- | ------------------------------- | ---- | ------------------ | 2306| resName | string | Yes | Resource name. | 2307| callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| 2308 2309**Error codes** 2310 2311For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2312 2313| ID| Error Message| 2314| -------- | ---------------------------------------- | 2315| 9001003 | If the resName invalid. | 2316| 9001004 | If the resource not found by resName. | 2317 2318**Example** 2319 ```ts 2320 import { BusinessError } from '@ohos.base'; 2321 2322 try { 2323 this.context.resourceManager.getMediaByName("test", (error, value) => { 2324 if (error != null) { 2325 console.log("error is " + error); 2326 } else { 2327 let media = value; 2328 } 2329 }); 2330 } catch (error) { 2331 let code = (error as BusinessError).code; 2332 let message = (error as BusinessError).message; 2333 console.error(`callback getMediaByName failed, error code: ${code}, message: ${message}.`); 2334 } 2335 ``` 2336 2337### getMediaByName<sup>10+</sup> 2338 2339getMediaByName(resName: string, density: number, callback: AsyncCallback<Uint8Array>): void 2340 2341Obtains the media file content (with the specified screen density) corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 2342 2343**System capability**: SystemCapability.Global.ResourceManager 2344 2345**Parameters** 2346 2347| Name | Type | Mandatory | Description | 2348| -------- | ------------------------------- | ---- | ------------------ | 2349| resName | string | Yes | Resource name. | 2350| [density](#screendensity) | number | Yes | Screen density. The value **0** indicates the default screen density. | 2351| callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| 2352 2353**Error codes** 2354 2355For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2356 2357| ID| Error Message| 2358| -------- | ---------------------------------------- | 2359| 9001003 | If the resName invalid. | 2360| 9001004 | If the resource not found by resName. | 2361 2362**Example** 2363 ```ts 2364 import { BusinessError } from '@ohos.base'; 2365 2366 try { 2367 this.context.resourceManager.getMediaByName("test", 120, (error, value) => { 2368 if (error != null) { 2369 console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); 2370 } else { 2371 let media = value; 2372 } 2373 }); 2374 } catch (error) { 2375 let code = (error as BusinessError).code; 2376 let message = (error as BusinessError).message; 2377 console.error(`callback getMediaByName failed, error code: ${code}, message: ${message}.`); 2378 } 2379 ``` 2380 2381### getMediaByName<sup>9+</sup> 2382 2383getMediaByName(resName: string): Promise<Uint8Array> 2384 2385Obtains the media file content corresponding to the specified resource name. This API uses a promise to return the result. 2386 2387**System capability**: SystemCapability.Global.ResourceManager 2388 2389**Parameters** 2390 2391| Name | Type | Mandatory | Description | 2392| ------- | ------ | ---- | ---- | 2393| resName | string | Yes | Resource name.| 2394 2395**Return value** 2396 2397| Type | Description | 2398| ------------------------- | ------------- | 2399| Promise<Uint8Array> | Promise used to return the result.| 2400 2401**Error codes** 2402 2403For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2404 2405| ID| Error Message| 2406| -------- | ---------------------------------------- | 2407| 9001003 | If the resName invalid. | 2408| 9001004 | If the resource not found by resName. | 2409 2410**Example** 2411 ```ts 2412 import { BusinessError } from '@ohos.base'; 2413 2414 try { 2415 this.context.resourceManager.getMediaByName("test").then((value: Uint8Array) => { 2416 let media = value; 2417 }).catch((error: BusinessError) => { 2418 console.log("getMediaByName promise error is " + error); 2419 }); 2420 } catch (error) { 2421 let code = (error as BusinessError).code; 2422 let message = (error as BusinessError).message; 2423 console.error(`promise getMediaByName failed, error code: ${code}, message: ${message}.`); 2424 } 2425 ``` 2426 2427### getMediaByName<sup>10+</sup> 2428 2429getMediaByName(resName: string, density: number): Promise<Uint8Array> 2430 2431Obtains the media file content (with the specified screen density) corresponding to the specified resource name. This API uses a promise to return the result. 2432 2433**System capability**: SystemCapability.Global.ResourceManager 2434 2435**Parameters** 2436 2437| Name | Type | Mandatory | Description | 2438| ------- | ------ | ---- | ---- | 2439| resName | string | Yes | Resource name.| 2440| [density](#screendensity) | number | Yes | Screen density. The value **0** indicates the default screen density. | 2441 2442**Return value** 2443 2444| Type | Description | 2445| ------------------------- | ------------- | 2446| Promise<Uint8Array> | Promise used to return the result.| 2447 2448**Error codes** 2449 2450For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2451 2452| ID| Error Message| 2453| -------- | ---------------------------------------- | 2454| 9001003 | If the resName invalid. | 2455| 9001004 | If the resource not found by resName. | 2456 2457**Example** 2458 ```ts 2459 import { BusinessError } from '@ohos.base'; 2460 2461 try { 2462 this.context.resourceManager.getMediaByName("test", 120).then((value: Uint8Array) => { 2463 let media = value; 2464 }).catch((error: BusinessError) => { 2465 console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`); 2466 }); 2467 } catch (error) { 2468 let code = (error as BusinessError).code; 2469 let message = (error as BusinessError).message; 2470 console.error(`promise getMediaByName failed, error code: ${code}, message: ${message}.`); 2471 } 2472 ``` 2473 2474### getMediaContentBase64Sync<sup>10+</sup> 2475 2476getMediaContentBase64Sync(resId: number, density?: number): string 2477 2478Obtains the Base64 code of the image (with the default or specified screen density) corresponding to the specified resource ID. 2479 2480**System capability**: SystemCapability.Global.ResourceManager 2481 2482**Parameters** 2483 2484| Name | Type | Mandatory | Description | 2485| ----- | ------ | ---- | ----- | 2486| resId | number | Yes | Resource ID.| 2487| [density](#screendensity) | number | No | Screen density. The default value or value **0** indicates the default screen density.| 2488 2489**Return value** 2490 2491| Type | Description | 2492| -------- | ----------- | 2493| string | Base64 code of the image corresponding to the specified resource ID.| 2494 2495**Error codes** 2496 2497For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2498 2499| ID| Error Message| 2500| -------- | ---------------------------------------- | 2501| 9001001 | If the resId invalid. | 2502| 9001002 | If the resource not found by resId. | 2503 2504**Example** 2505 ```ts 2506 import { BusinessError } from '@ohos.base'; 2507 2508 try { 2509 this.context.resourceManager.getMediaContentBase64Sync($r('app.media.test').id); // Default screen density 2510 } catch (error) { 2511 let code = (error as BusinessError).code; 2512 let message = (error as BusinessError).message; 2513 console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`); 2514 } 2515 2516 try { 2517 this.context.resourceManager.getMediaContentBase64Sync($r('app.media.test').id, 120); // Specified screen density 2518 } catch (error) { 2519 let code = (error as BusinessError).code; 2520 let message = (error as BusinessError).message; 2521 console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`); 2522 } 2523 ``` 2524 2525### getMediaContentBase64Sync<sup>10+</sup> 2526 2527getMediaContentBase64Sync(resource: Resource, density?: number): string 2528 2529Obtains the Base64 code of the image (with the default or specified screen density) corresponding to the specified resource object. 2530 2531**System capability**: SystemCapability.Global.ResourceManager 2532 2533**Model restriction**: This API can be used only in the stage model. 2534 2535**Parameters** 2536 2537| Name | Type | Mandatory | Description | 2538| ----- | ------ | ---- | ----- | 2539| resource | [Resource](#resource9) | Yes | Resource object.| 2540| [density](#screendensity) | number | No | Screen density. The default value or value **0** indicates the default screen density.| 2541 2542**Return value** 2543 2544| Type | Description | 2545| --------------------- | ----------- | 2546| string | Promise used to return the result.| 2547 2548**Error codes** 2549 2550For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2551 2552| ID| Error Message| 2553| -------- | ---------------------------------------- | 2554| 9001001 | If the resId invalid. | 2555| 9001002 | If the resource not found by resId. | 2556 2557**Example** 2558 ```ts 2559 import resourceManager from '@ohos.resourceManager'; 2560 import { BusinessError } from '@ohos.base'; 2561 2562 let resource: resourceManager.Resource = { 2563 bundleName: "com.example.myapplication", 2564 moduleName: "entry", 2565 id: $r('app.media.test').id 2566 }; 2567 try { 2568 this.context.resourceManager.getMediaContentBase64Sync(resource); // Default screen density 2569 } catch (error) { 2570 let code = (error as BusinessError).code; 2571 let message = (error as BusinessError).message; 2572 console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`); 2573 } 2574 2575 try { 2576 this.context.resourceManager.getMediaContentBase64Sync(resource, 120); // Specified screen density 2577 } catch (error) { 2578 let code = (error as BusinessError).code; 2579 let message = (error as BusinessError).message; 2580 console.error(`getMediaContentBase64Sync failed, error code: ${code}, message: ${message}.`); 2581 } 2582 ``` 2583 2584### getMediaBase64ByNameSync<sup>10+</sup> 2585 2586getMediaBase64ByNameSync(resName: string, density?: number): string 2587 2588Obtains the Base64 code of the image (with the default or specified screen density) corresponding to the specified resource name. 2589 2590**System capability**: SystemCapability.Global.ResourceManager 2591 2592**Parameters** 2593 2594| Name | Type | Mandatory | Description | 2595| ----- | ------ | ---- | ----- | 2596| resName | string | Yes | Resource name.| 2597| [density](#screendensity) | number | No | Screen density. The default value or value **0** indicates the default screen density.| 2598 2599**Return value** 2600 2601| Type | Description | 2602| --------------------- | ----------- | 2603| string | Promise used to return the result.| 2604 2605**Error codes** 2606 2607For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2608 2609| ID| Error Message| 2610| -------- | ---------------------------------------- | 2611| 9001003 | If the resName invalid. | 2612| 9001004 | If the resource not found by resName. | 2613 2614**Example** 2615 ```ts 2616 import { BusinessError } from '@ohos.base'; 2617 2618 try { 2619 this.context.resourceManager.getMediaBase64ByNameSync("test"); // Default screen density 2620 } catch (error) { 2621 let code = (error as BusinessError).code; 2622 let message = (error as BusinessError).message; 2623 console.error(`getMediaBase64ByNameSync failed, error code: ${code}, message: ${message}.`); 2624 } 2625 2626 try { 2627 this.context.resourceManager.getMediaBase64ByNameSync("test", 120); // Specified screen density 2628 } catch (error) { 2629 let code = (error as BusinessError).code; 2630 let message = (error as BusinessError).message; 2631 console.error(`getMediaBase64ByNameSync failed, error code: ${code}, message: ${message}.`); 2632 } 2633 ``` 2634 2635### getMediaContentBase64<sup>9+</sup> 2636 2637getMediaContentBase64(resId: number, callback: AsyncCallback<string>): void 2638 2639Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 2640 2641**System capability**: SystemCapability.Global.ResourceManager 2642 2643**Parameters** 2644 2645| Name | Type | Mandatory | Description | 2646| -------- | --------------------------- | ---- | ------------------------ | 2647| resId | number | Yes | Resource ID. | 2648| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 2649 2650**Error codes** 2651 2652For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2653 2654| ID| Error Message| 2655| -------- | ---------------------------------------- | 2656| 9001001 | If the resId invalid. | 2657| 9001002 | If the resource not found by resId. | 2658 2659**Example** 2660 ```ts 2661 import { BusinessError } from '@ohos.base'; 2662 2663 try { 2664 this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, (error, value) => { 2665 if (error != null) { 2666 console.log("error is " + error); 2667 } else { 2668 let media = value; 2669 } 2670 }); 2671 } catch (error) { 2672 let code = (error as BusinessError).code; 2673 let message = (error as BusinessError).message; 2674 console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2675 } 2676 ``` 2677 2678### getMediaContentBase64<sup>10+</sup> 2679 2680getMediaContentBase64(resId: number, density: number, callback: AsyncCallback<string>): void 2681 2682Obtains the Base64 code of an image with the screen density corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 2683 2684**System capability**: SystemCapability.Global.ResourceManager 2685 2686**Parameters** 2687 2688| Name | Type | Mandatory | Description | 2689| -------- | --------------------------- | ---- | ------------------------ | 2690| resId | number | Yes | Resource ID. | 2691| [density](#screendensity) | number | Yes | Screen density. The value **0** indicates the default screen density. | 2692| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 2693 2694**Error codes** 2695 2696For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2697 2698| ID| Error Message| 2699| -------- | ---------------------------------------- | 2700| 9001001 | If the resId invalid. | 2701| 9001002 | If the resource not found by resId. | 2702 2703**Example** 2704 ```ts 2705 import { BusinessError } from '@ohos.base'; 2706 2707 try { 2708 this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120, (error, value) => { 2709 if (error != null) { 2710 console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); 2711 } else { 2712 let media = value; 2713 } 2714 }); 2715 } catch (error) { 2716 let code = (error as BusinessError).code; 2717 let message = (error as BusinessError).message; 2718 console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2719 } 2720 ``` 2721 2722### getMediaContentBase64<sup>9+</sup> 2723 2724getMediaContentBase64(resId: number): Promise<string> 2725 2726Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses a promise to return the result. 2727 2728**System capability**: SystemCapability.Global.ResourceManager 2729 2730**Parameters** 2731 2732| Name | Type | Mandatory | Description | 2733| ----- | ------ | ---- | ----- | 2734| resId | number | Yes | Resource ID.| 2735 2736**Return value** 2737 2738| Type | Description | 2739| --------------------- | -------------------- | 2740| Promise<string> | Promise used to return the result.| 2741 2742**Error codes** 2743 2744For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2745 2746| ID| Error Message| 2747| -------- | ---------------------------------------- | 2748| 9001001 | If the resId invalid. | 2749| 9001002 | If the resource not found by resId. | 2750 2751**Example** 2752 ```ts 2753 import { BusinessError } from '@ohos.base'; 2754 2755 try { 2756 this.context.resourceManager.getMediaContentBase64($r('app.media.test').id).then((value: string) => { 2757 let media = value; 2758 }).catch((error: BusinessError) => { 2759 console.log("getMediaContentBase64 promise error is " + error); 2760 }); 2761 } catch (error) { 2762 let code = (error as BusinessError).code; 2763 let message = (error as BusinessError).message; 2764 console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2765 } 2766 ``` 2767 2768### getMediaContentBase64<sup>10+</sup> 2769 2770getMediaContentBase64(resId: number, density: number): Promise<string> 2771 2772Obtains the Base64 code of an image with the screen density corresponding to the specified resource ID. This API uses a promise to return the result. 2773 2774**System capability**: SystemCapability.Global.ResourceManager 2775 2776**Parameters** 2777 2778| Name | Type | Mandatory | Description | 2779| ----- | ------ | ---- | ----- | 2780| resId | number | Yes | Resource ID.| 2781| [density](#screendensity) | number | Yes | Screen density. The value **0** indicates the default screen density. | 2782 2783**Return value** 2784 2785| Type | Description | 2786| --------------------- | -------------------- | 2787| Promise<string> | Promise used to return the result.| 2788 2789**Error codes** 2790 2791For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2792 2793| ID| Error Message| 2794| -------- | ---------------------------------------- | 2795| 9001001 | If the resId invalid. | 2796| 9001002 | If the resource not found by resId. | 2797 2798**Example** 2799 ```ts 2800 import { BusinessError } from '@ohos.base'; 2801 2802 try { 2803 this.context.resourceManager.getMediaContentBase64($r('app.media.test').id, 120).then((value: string) => { 2804 let media = value; 2805 }).catch((error: BusinessError) => { 2806 console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); 2807 }); 2808 } catch (error) { 2809 let code = (error as BusinessError).code; 2810 let message = (error as BusinessError).message; 2811 console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2812 } 2813 ``` 2814 2815### getMediaContentBase64<sup>9+</sup> 2816 2817getMediaContentBase64(resource: Resource, callback: AsyncCallback<string>): void 2818 2819Obtains the Base64 code of the image corresponding to the specified resource object. This API uses an asynchronous callback to return the result. 2820 2821**System capability**: SystemCapability.Global.ResourceManager 2822 2823**Model restriction**: This API can be used only in the stage model. 2824 2825**Parameters** 2826 2827| Name | Type | Mandatory | Description | 2828| -------- | --------------------------- | ---- | ------------------------ | 2829| resource | [Resource](#resource9) | Yes | Resource object. | 2830| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 2831 2832**Error codes** 2833 2834For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2835 2836| ID| Error Message| 2837| -------- | ---------------------------------------- | 2838| 9001001 | If the resId invalid. | 2839| 9001002 | If the resource not found by resId. | 2840 2841**Example** 2842 ```ts 2843 import resourceManager from '@ohos.resourceManager'; 2844 import { BusinessError } from '@ohos.base'; 2845 2846 let resource: resourceManager.Resource = { 2847 bundleName: "com.example.myapplication", 2848 moduleName: "entry", 2849 id: $r('app.media.test').id 2850 }; 2851 try { 2852 this.context.resourceManager.getMediaContentBase64(resource, (error, value) => { 2853 if (error != null) { 2854 console.log("error is " + error); 2855 } else { 2856 let media = value; 2857 } 2858 }); 2859 } catch (error) { 2860 let code = (error as BusinessError).code; 2861 let message = (error as BusinessError).message; 2862 console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2863 } 2864 ``` 2865 2866### getMediaContentBase64<sup>10+</sup> 2867 2868getMediaContentBase64(resource: Resource, density: number, callback: AsyncCallback<string>): void 2869 2870Obtains the Base64 code of an image with the screen density corresponding to the specified resource object. This API uses an asynchronous callback to return the result. 2871 2872**System capability**: SystemCapability.Global.ResourceManager 2873 2874**Model restriction**: This API can be used only in the stage model. 2875 2876**Parameters** 2877 2878| Name | Type | Mandatory | Description | 2879| -------- | --------------------------- | ---- | ------------------------ | 2880| resource | [Resource](#resource9) | Yes | Resource object. | 2881| [density](#screendensity) | number | Yes | Screen density. The value **0** indicates the default screen density. | 2882| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 2883 2884**Error codes** 2885 2886For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2887 2888| ID| Error Message| 2889| -------- | ---------------------------------------- | 2890| 9001001 | If the resId invalid. | 2891| 9001002 | If the resource not found by resId. | 2892 2893**Example** 2894 ```ts 2895 import resourceManager from '@ohos.resourceManager'; 2896 import { BusinessError } from '@ohos.base'; 2897 2898 let resource: resourceManager.Resource = { 2899 bundleName: "com.example.myapplication", 2900 moduleName: "entry", 2901 id: $r('app.media.test').id 2902 }; 2903 try { 2904 this.context.resourceManager.getMediaContentBase64(resource, 120, (error, value) => { 2905 if (error != null) { 2906 console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); 2907 } else { 2908 let media = value; 2909 } 2910 }); 2911 } catch (error) { 2912 let code = (error as BusinessError).code; 2913 let message = (error as BusinessError).message; 2914 console.error(`callback getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2915 } 2916 ``` 2917 2918### getMediaContentBase64<sup>9+</sup> 2919 2920getMediaContentBase64(resource: Resource): Promise<string> 2921 2922Obtains the Base64 code of the image corresponding to the specified resource object. This API uses a promise to return the result. 2923 2924**System capability**: SystemCapability.Global.ResourceManager 2925 2926**Model restriction**: This API can be used only in the stage model. 2927 2928**Parameters** 2929 2930| Name | Type | Mandatory | Description | 2931| -------- | ---------------------- | ---- | ---- | 2932| resource | [Resource](#resource9) | Yes | Resource object.| 2933 2934**Return value** 2935 2936| Type | Description | 2937| --------------------- | ------------------------- | 2938| Promise<string> | Promise used to return the result.| 2939 2940**Error codes** 2941 2942For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2943 2944| ID| Error Message| 2945| -------- | ---------------------------------------- | 2946| 9001001 | If the resId invalid. | 2947| 9001002 | If the resource not found by resId. | 2948 2949**Example** 2950 ```ts 2951 import resourceManager from '@ohos.resourceManager'; 2952 import { BusinessError } from '@ohos.base'; 2953 2954 let resource: resourceManager.Resource = { 2955 bundleName: "com.example.myapplication", 2956 moduleName: "entry", 2957 id: $r('app.media.test').id 2958 }; 2959 try { 2960 this.context.resourceManager.getMediaContentBase64(resource).then((value: string) => { 2961 let media = value; 2962 }).catch((error: BusinessError) => { 2963 console.log("getMediaContentBase64 promise error is " + error); 2964 }); 2965 } catch (error) { 2966 let code = (error as BusinessError).code; 2967 let message = (error as BusinessError).message; 2968 console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 2969 } 2970 ``` 2971 2972### getMediaContentBase64<sup>10+</sup> 2973 2974getMediaContentBase64(resource: Resource, density: number): Promise<string> 2975 2976Obtains the Base64 code of an image with the screen density corresponding to the specified resource object. This API uses a promise to return the result. 2977 2978**System capability**: SystemCapability.Global.ResourceManager 2979 2980**Model restriction**: This API can be used only in the stage model. 2981 2982**Parameters** 2983 2984| Name | Type | Mandatory | Description | 2985| -------- | ---------------------- | ---- | ---- | 2986| resource | [Resource](#resource9) | Yes | Resource object.| 2987| [density](#screendensity) | number | Yes | Screen density. The value **0** indicates the default screen density. | 2988 2989**Return value** 2990 2991| Type | Description | 2992| --------------------- | ------------------------- | 2993| Promise<string> | Promise used to return the result.| 2994 2995**Error codes** 2996 2997For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 2998 2999| ID| Error Message| 3000| -------- | ---------------------------------------- | 3001| 9001001 | If the resId invalid. | 3002| 9001002 | If the resource not found by resId. | 3003 3004**Example** 3005 ```ts 3006 import resourceManager from '@ohos.resourceManager'; 3007 import { BusinessError } from '@ohos.base'; 3008 3009 let resource: resourceManager.Resource = { 3010 bundleName: "com.example.myapplication", 3011 moduleName: "entry", 3012 id: $r('app.media.test').id 3013 }; 3014 try { 3015 this.context.resourceManager.getMediaContentBase64(resource, 120).then((value: string) => { 3016 let media = value; 3017 }).catch((error: BusinessError) => { 3018 console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`); 3019 }); 3020 } catch (error) { 3021 let code = (error as BusinessError).code; 3022 let message = (error as BusinessError).message; 3023 console.error(`promise getMediaContentBase64 failed, error code: ${code}, message: ${message}.`); 3024 } 3025 ``` 3026 3027### getMediaBase64ByName<sup>9+</sup> 3028 3029getMediaBase64ByName(resName: string, callback: AsyncCallback<string>): void 3030 3031Obtains the Base64 code of the image corresponding to the specified resource name. This API uses an asynchronous callback to return the result. 3032 3033**System capability**: SystemCapability.Global.ResourceManager 3034 3035**Parameters** 3036 3037| Name | Type | Mandatory | Description | 3038| -------- | --------------------------- | ---- | ------------------------ | 3039| resName | string | Yes | Resource name. | 3040| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 3041 3042**Error codes** 3043 3044For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3045 3046| ID| Error Message| 3047| -------- | ---------------------------------------- | 3048| 9001003 | If the resName invalid. | 3049| 9001004 | If the resource not found by resName. | 3050 3051**Example** 3052 ```ts 3053 import { BusinessError } from '@ohos.base'; 3054 3055 try { 3056 this.context.resourceManager.getMediaBase64ByName("test", (error, value) => { 3057 if (error != null) { 3058 console.log("error is " + error); 3059 } else { 3060 let media = value; 3061 } 3062 }); 3063 } catch (error) { 3064 let code = (error as BusinessError).code; 3065 let message = (error as BusinessError).message; 3066 console.error(`callback getMediaBase64ByName failed, error code: ${code}, message: ${message}.`); 3067 } 3068 ``` 3069 3070### getMediaBase64ByName<sup>10+</sup> 3071 3072getMediaBase64ByName(resName: string, density: number, callback: AsyncCallback<string>): void 3073 3074Obtains the Base64 code of an image with the screen density corresponding to the specified resource name. This API uses an asynchronous callback to return the result. 3075 3076**System capability**: SystemCapability.Global.ResourceManager 3077 3078**Parameters** 3079 3080| Name | Type | Mandatory | Description | 3081| -------- | --------------------------- | ---- | ------------------------ | 3082| resName | string | Yes | Resource name. | 3083| [density](#screendensity) | number | Yes | Screen density. The value **0** indicates the default screen density. | 3084| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 3085 3086**Error codes** 3087 3088For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3089 3090| ID| Error Message| 3091| -------- | ---------------------------------------- | 3092| 9001003 | If the resName invalid. | 3093| 9001004 | If the resource not found by resName. | 3094 3095**Example** 3096 ```ts 3097 import { BusinessError } from '@ohos.base'; 3098 3099 try { 3100 this.context.resourceManager.getMediaBase64ByName("test", 120, (error, value) => { 3101 if (error != null) { 3102 console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); 3103 } else { 3104 let media = value; 3105 } 3106 }); 3107 } catch (error) { 3108 let code = (error as BusinessError).code; 3109 let message = (error as BusinessError).message; 3110 console.error(`callback getMediaBase64ByName failed, error code: ${code}, message: ${message}.`); 3111 } 3112 ``` 3113 3114### getMediaBase64ByName<sup>9+</sup> 3115 3116getMediaBase64ByName(resName: string): Promise<string> 3117 3118Obtains the Base64 code of the image corresponding to the specified resource name. This API uses a promise to return the result. 3119 3120**System capability**: SystemCapability.Global.ResourceManager 3121 3122**Parameters** 3123 3124| Name | Type | Mandatory | Description | 3125| ------- | ------ | ---- | ---- | 3126| resName | string | Yes | Resource name.| 3127 3128**Return value** 3129 3130| Type | Description | 3131| --------------------- | ------------------- | 3132| Promise<string> | Promise used to return the result.| 3133 3134**Error codes** 3135 3136For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3137 3138| ID| Error Message| 3139| -------- | ---------------------------------------- | 3140| 9001003 | If the resName invalid. | 3141| 9001004 | If the resource not found by resName. | 3142 3143**Example** 3144 ```ts 3145 import { BusinessError } from '@ohos.base'; 3146 3147 try { 3148 this.context.resourceManager.getMediaBase64ByName("test").then((value: string) => { 3149 let media = value; 3150 }).catch((error: BusinessError) => { 3151 console.log("getMediaBase64ByName promise error is " + error); 3152 }); 3153 } catch (error) { 3154 let code = (error as BusinessError).code; 3155 let message = (error as BusinessError).message; 3156 console.error(`promise getMediaBase64ByName failed, error code: ${code}, message: ${message}.`); 3157 } 3158 ``` 3159 3160### getMediaBase64ByName<sup>10+</sup> 3161 3162getMediaBase64ByName(resName: string, density: number): Promise<string> 3163 3164Obtains the Base64 code of an image with the screen density corresponding to the specified resource name. This API uses a promise to return the result. 3165 3166**System capability**: SystemCapability.Global.ResourceManager 3167 3168**Parameters** 3169 3170| Name | Type | Mandatory | Description | 3171| ------- | ------ | ---- | ---- | 3172| resName | string | Yes | Resource name.| 3173| [density](#screendensity) | number | Yes | Screen density. The value **0** indicates the default screen density. | 3174 3175**Return value** 3176 3177| Type | Description | 3178| --------------------- | ------------------- | 3179| Promise<string> | Promise used to return the result.| 3180 3181**Error codes** 3182 3183For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3184 3185| ID| Error Message| 3186| -------- | ---------------------------------------- | 3187| 9001003 | If the resName invalid. | 3188| 9001004 | If the resource not found by resName. | 3189 3190**Example** 3191 ```ts 3192 import { BusinessError } from '@ohos.base'; 3193 3194 try { 3195 this.context.resourceManager.getMediaBase64ByName("test", 120).then((value: string) => { 3196 let media = value; 3197 }).catch((error: BusinessError) => { 3198 console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`); 3199 }); 3200 } catch (error) { 3201 let code = (error as BusinessError).code; 3202 let message = (error as BusinessError).message; 3203 console.error(`promise getMediaBase64ByName failed, error code: ${code}, message: ${message}.`); 3204 } 3205 ``` 3206 3207### getDrawableDescriptor<sup>10+</sup> 3208 3209getDrawableDescriptor(resId: number, density?: number): DrawableDescriptor; 3210 3211Obtains the **DrawableDescriptor** object corresponding to the specified resource ID. This API returns the result synchronously. 3212 3213**System capability**: SystemCapability.Global.ResourceManager 3214 3215**Parameters** 3216 3217| Name | Type | Mandatory | Description | 3218| ----- | ------ | ---- | ----- | 3219| resId | number | Yes | Resource ID.| 3220| [density](#screendensity) | number | No | Screen density. The default value or value **0** indicates the default screen density.| 3221 3222**Return value** 3223 3224| Type | Description | 3225| ------ | ---------- | 3226| DrawableDescriptor | **DrawableDescriptor** object corresponding to the specified resource ID.| 3227 3228**Error codes** 3229 3230For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3231 3232| ID| Error Message| 3233| -------- | ---------------------------------------- | 3234| 9001001 | If the resId invalid. | 3235| 9001002 | If the resource not found by resId. | 3236 3237**Example** 3238 ```ts 3239 import { BusinessError } from '@ohos.base'; 3240 3241 try { 3242 this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id); 3243 } catch (error) { 3244 let code = (error as BusinessError).code; 3245 let message = (error as BusinessError).message; 3246 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 3247 } 3248 try { 3249 this.context.resourceManager.getDrawableDescriptor($r('app.media.icon').id, 120); 3250 } catch (error) { 3251 let code = (error as BusinessError).code; 3252 let message = (error as BusinessError).message; 3253 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 3254 } 3255 ``` 3256 3257### getDrawableDescriptor<sup>10+</sup> 3258 3259getDrawableDescriptor(resource: Resource, density?: number): DrawableDescriptor; 3260 3261Obtains the **DrawableDescriptor** object corresponding to the specified resource object. This API returns the result synchronously. 3262 3263**System capability**: SystemCapability.Global.ResourceManager 3264 3265**Model restriction**: This API can be used only in the stage model. 3266 3267**Parameters** 3268 3269| Name | Type | Mandatory | Description | 3270| -------- | ---------------------- | ---- | ---- | 3271| resource | [Resource](#resource9) | Yes | Resource object.| 3272| [density](#screendensity) | number | No | Screen density. The default value or value **0** indicates the default screen density.| 3273 3274**Return value** 3275 3276| Type | Description | 3277| ------- | ----------------- | 3278| DrawableDescriptor | **DrawableDescriptor** object corresponding to the specified resource ID.| 3279 3280**Error codes** 3281 3282For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3283 3284| ID| Error Message| 3285| -------- | ---------------------------------------- | 3286| 9001001 | If the resId invalid. | 3287| 9001002 | If the resource not found by resId. | 3288 3289**Example** 3290 ```ts 3291 import resourceManager from '@ohos.resourceManager'; 3292 import { BusinessError } from '@ohos.base'; 3293 3294 let resource: resourceManager.Resource = { 3295 bundleName: "com.example.myapplication", 3296 moduleName: "entry", 3297 id: $r('app.media.icon').id 3298 }; 3299 try { 3300 this.context.resourceManager.getDrawableDescriptor(resource); 3301 } catch (error) { 3302 let code = (error as BusinessError).code; 3303 let message = (error as BusinessError).message; 3304 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 3305 } 3306 try { 3307 this.context.resourceManager.getDrawableDescriptor(resource, 120); 3308 } catch (error) { 3309 let code = (error as BusinessError).code; 3310 let message = (error as BusinessError).message; 3311 console.error(`getDrawableDescriptor failed, error code: ${code}, message: ${message}.`); 3312 } 3313 ``` 3314 3315### getDrawableDescriptorByName<sup>10+</sup> 3316 3317getDrawableDescriptorByName(resName: string, density?: number): DrawableDescriptor; 3318 3319Obtains the **DrawableDescriptor** object corresponding to the specified resource name. This API returns the result synchronously. 3320 3321**System capability**: SystemCapability.Global.ResourceManager 3322 3323**Parameters** 3324 3325| Name | Type | Mandatory | Description | 3326| ------- | ------ | ---- | ---- | 3327| resName | string | Yes | Resource name.| 3328| [density](#screendensity) | number | No | Screen density. The default value or value **0** indicates the default screen density.| 3329 3330**Return value** 3331 3332| Type | Description | 3333| ------ | --------- | 3334| DrawableDescriptor | **DrawableDescriptor** object corresponding to the specified resource ID.| 3335 3336**Error codes** 3337 3338For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3339 3340| ID| Error Message| 3341| -------- | ---------------------------------------- | 3342| 9001003 | If the resName invalid. | 3343| 9001004 | If the resource not found by resName. | 3344 3345**Example** 3346 ```ts 3347 import { BusinessError } from '@ohos.base'; 3348 3349 try { 3350 this.context.resourceManager.getDrawableDescriptorByName('icon'); 3351 } catch (error) { 3352 let code = (error as BusinessError).code; 3353 let message = (error as BusinessError).message; 3354 console.error(`getDrawableDescriptorByName failed, error code: ${code}, message: ${message}.`); 3355 } 3356 try { 3357 this.context.resourceManager.getDrawableDescriptorByName('icon', 120); 3358 } catch (error) { 3359 let code = (error as BusinessError).code; 3360 let message = (error as BusinessError).message; 3361 console.error(`getDrawableDescriptorByName failed, error code: ${code}, message: ${message}.`); 3362 } 3363 ``` 3364 3365### getBoolean<sup>9+</sup> 3366 3367getBoolean(resId: number): boolean 3368 3369Obtains the Boolean result corresponding to the specified resource ID. This API returns the result synchronously. 3370 3371**System capability**: SystemCapability.Global.ResourceManager 3372 3373**Parameters** 3374 3375| Name | Type | Mandatory | Description | 3376| ----- | ------ | ---- | ----- | 3377| resId | number | Yes | Resource ID.| 3378 3379**Return value** 3380 3381| Type | Description | 3382| ------- | ------------ | 3383| boolean | Boolean result corresponding to the specified resource ID.| 3384 3385**Error codes** 3386 3387For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3388 3389| ID| Error Message| 3390| -------- | ---------------------------------------- | 3391| 9001001 | If the resId invalid. | 3392| 9001002 | If the resource not found by resId. | 3393| 9001006 | If the resource re-ref too much. | 3394 3395**Example** 3396 ```ts 3397 import { BusinessError } from '@ohos.base'; 3398 3399 try { 3400 this.context.resourceManager.getBoolean($r('app.boolean.boolean_test').id); 3401 } catch (error) { 3402 let code = (error as BusinessError).code; 3403 let message = (error as BusinessError).message; 3404 console.error(`getBoolean failed, error code: ${code}, message: ${message}.`); 3405 } 3406 ``` 3407### getBoolean<sup>9+</sup> 3408 3409getBoolean(resource: Resource): boolean 3410 3411Obtains the Boolean result corresponding to the specified resource object. This API returns the result synchronously. 3412 3413**System capability**: SystemCapability.Global.ResourceManager 3414 3415**Model restriction**: This API can be used only in the stage model. 3416 3417**Parameters** 3418 3419| Name | Type | Mandatory | Description | 3420| -------- | ---------------------- | ---- | ---- | 3421| resource | [Resource](#resource9) | Yes | Resource object.| 3422 3423**Return value** 3424 3425| Type | Description | 3426| ------- | ----------------- | 3427| boolean | Boolean result corresponding to the specified resource object.| 3428 3429**Error codes** 3430 3431For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3432 3433| ID| Error Message| 3434| -------- | ---------------------------------------- | 3435| 9001001 | If the resId invalid. | 3436| 9001002 | If the resource not found by resId. | 3437| 9001006 | If the resource re-ref too much. | 3438 3439**Example** 3440 ```ts 3441 import resourceManager from '@ohos.resourceManager'; 3442 import { BusinessError } from '@ohos.base'; 3443 3444 let resource: resourceManager.Resource = { 3445 bundleName: "com.example.myapplication", 3446 moduleName: "entry", 3447 id: $r('app.boolean.boolean_test').id 3448 }; 3449 try { 3450 this.context.resourceManager.getBoolean(resource); 3451 } catch (error) { 3452 let code = (error as BusinessError).code; 3453 let message = (error as BusinessError).message; 3454 console.error(`getBoolean failed, error code: ${code}, message: ${message}.`); 3455 } 3456 ``` 3457 3458### getBooleanByName<sup>9+</sup> 3459 3460getBooleanByName(resName: string): boolean 3461 3462Obtains the Boolean result corresponding to the specified resource name. This API returns the result synchronously. 3463 3464**System capability**: SystemCapability.Global.ResourceManager 3465 3466**Parameters** 3467 3468| Name | Type | Mandatory | Description | 3469| ------- | ------ | ---- | ---- | 3470| resName | string | Yes | Resource name.| 3471 3472**Return value** 3473 3474| Type | Description | 3475| ------- | ----------- | 3476| boolean | Boolean result corresponding to the specified resource name.| 3477 3478**Error codes** 3479 3480For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3481 3482| ID| Error Message| 3483| -------- | ---------------------------------------- | 3484| 9001003 | If the resName invalid. | 3485| 9001004 | If the resource not found by resName. | 3486| 9001006 | If the resource re-ref too much. | 3487 3488**Example** 3489 ```ts 3490 import { BusinessError } from '@ohos.base'; 3491 3492 try { 3493 this.context.resourceManager.getBooleanByName("boolean_test"); 3494 } catch (error) { 3495 let code = (error as BusinessError).code; 3496 let message = (error as BusinessError).message; 3497 console.error(`getBooleanByName failed, error code: ${code}, message: ${message}.`); 3498 } 3499 ``` 3500 3501### getNumber<sup>9+</sup> 3502 3503getNumber(resId: number): number 3504 3505Obtains the integer or float value corresponding to the specified resource ID. This API returns the result synchronously. 3506 3507**System capability**: SystemCapability.Global.ResourceManager 3508 3509**Parameters** 3510 3511| Name | Type | Mandatory | Description | 3512| ----- | ------ | ---- | ----- | 3513| resId | number | Yes | Resource ID.| 3514 3515**Return value** 3516 3517| Type | Description | 3518| ------ | ---------- | 3519| 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.| 3520 3521**Error codes** 3522 3523For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3524 3525| ID| Error Message| 3526| -------- | ---------------------------------------- | 3527| 9001001 | If the resId invalid. | 3528| 9001002 | If the resource not found by resId. | 3529| 9001006 | If the resource re-ref too much. | 3530 3531**Example** 3532 ```ts 3533 import { BusinessError } from '@ohos.base'; 3534 3535 try { 3536 this.context.resourceManager.getNumber($r('app.integer.integer_test').id); // integer refers to the original value. 3537 } catch (error) { 3538 let code = (error as BusinessError).code; 3539 let message = (error as BusinessError).message; 3540 console.error(`getNumber failed, error code: ${code}, message: ${message}.`); 3541 } 3542 3543 try { 3544 this.context.resourceManager.getNumber($r('app.float.float_test').id); // float refers to the actual pixel value. 3545 } catch (error) { 3546 let code = (error as BusinessError).code; 3547 let message = (error as BusinessError).message; 3548 console.error(`getNumber failed, error code: ${code}, message: ${message}.`); 3549 } 3550 ``` 3551 3552### getNumber<sup>9+</sup> 3553 3554getNumber(resource: Resource): number 3555 3556Obtains the integer or float value corresponding to the specified resource object. This API returns the result synchronously. 3557 3558**System capability**: SystemCapability.Global.ResourceManager 3559 3560**Model restriction**: This API can be used only in the stage model. 3561 3562**Parameters** 3563 3564| Name | Type | Mandatory | Description | 3565| -------- | ---------------------- | ---- | ---- | 3566| resource | [Resource](#resource9) | Yes | Resource object.| 3567 3568**Return value** 3569 3570| Type | Description | 3571| ------ | --------------- | 3572| 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.| 3573 3574**Error codes** 3575 3576For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3577 3578| ID| Error Message| 3579| -------- | ---------------------------------------- | 3580| 9001001 | If the resId invalid. | 3581| 9001002 | If the resource not found by resId. | 3582| 9001006 | If the resource re-ref too much. | 3583 3584**Example** 3585 ```ts 3586 import resourceManager from '@ohos.resourceManager'; 3587 import { BusinessError } from '@ohos.base'; 3588 3589 let resource: resourceManager.Resource = { 3590 bundleName: "com.example.myapplication", 3591 moduleName: "entry", 3592 id: $r('app.integer.integer_test').id 3593 }; 3594 try { 3595 this.context.resourceManager.getNumber(resource);// integer refers to the original value; float refers to the actual pixel value. 3596 } catch (error) { 3597 let code = (error as BusinessError).code; 3598 let message = (error as BusinessError).message; 3599 console.error(`getNumber failed, error code: ${code}, message: ${message}.`); 3600 } 3601 ``` 3602 3603### getNumberByName<sup>9+</sup> 3604 3605getNumberByName(resName: string): number 3606 3607Obtains the integer or float value corresponding to the specified resource name. This API returns the result synchronously. 3608 3609**System capability**: SystemCapability.Global.ResourceManager 3610 3611**Parameters** 3612 3613| Name | Type | Mandatory | Description | 3614| ------- | ------ | ---- | ---- | 3615| resName | string | Yes | Resource name.| 3616 3617**Return value** 3618 3619| Type | Description | 3620| ------ | --------- | 3621| number | Integer or float value corresponding to the specified resource name.| 3622 3623**Error codes** 3624 3625For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3626 3627| ID| Error Message| 3628| -------- | ---------------------------------------- | 3629| 9001003 | If the resName invalid. | 3630| 9001004 | If the resource not found by resName. | 3631| 9001006 | If the resource re-ref too much. | 3632 3633**Example** 3634 ```ts 3635 import { BusinessError } from '@ohos.base'; 3636 3637 try { 3638 this.context.resourceManager.getNumberByName("integer_test"); 3639 } catch (error) { 3640 let code = (error as BusinessError).code; 3641 let message = (error as BusinessError).message; 3642 console.error(`getNumberByName failed, error code: ${code}, message: ${message}.`); 3643 } 3644 3645 try { 3646 this.context.resourceManager.getNumberByName("float_test"); 3647 } catch (error) { 3648 let code = (error as BusinessError).code; 3649 let message = (error as BusinessError).message; 3650 console.error(`getNumberByName failed, error code: ${code}, message: ${message}.`); 3651 } 3652 ``` 3653 3654### getColorSync<sup>10+</sup> 3655 3656getColorSync(resId: number) : number; 3657 3658Obtains the color value corresponding to the specified resource ID. This API returns the result synchronously. 3659 3660**System capability**: SystemCapability.Global.ResourceManager 3661 3662**Parameters** 3663 3664| Name | Type | Mandatory | Description | 3665| ----- | ------ | ---- | ----- | 3666| resId | number | Yes | Resource ID.| 3667 3668**Return value** 3669 3670| Type | Description | 3671| ------ | ----------- | 3672| number | Color value corresponding to the resource ID (decimal).| 3673 3674**Error codes** 3675 3676For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3677 3678| ID| Error Message| 3679| -------- | ---------------------------------------- | 3680| 9001001 | If the resId invalid. | 3681| 9001002 | If the resource not found by resId. | 3682| 9001006 | If the resource re-ref too much. | 3683 3684**Example** 3685 ```ts 3686 import { BusinessError } from '@ohos.base'; 3687 3688 try { 3689 this.context.resourceManager.getColorSync($r('app.color.test').id); 3690 } catch (error) { 3691 let code = (error as BusinessError).code; 3692 let message = (error as BusinessError).message; 3693 console.error(`getColorSync failed, error code: ${code}, message: ${message}.`); 3694 } 3695 ``` 3696 3697### getColorSync<sup>10+</sup> 3698 3699getColorSync(resource: Resource): number 3700 3701Obtains the color value corresponding to the specified resource object. This API returns the result synchronously. 3702 3703**System capability**: SystemCapability.Global.ResourceManager 3704 3705**Model restriction**: This API can be used only in the stage model. 3706 3707**Parameters** 3708 3709| Name | Type | Mandatory | Description | 3710| -------- | ---------------------- | ---- | ---- | 3711| resource | [Resource](#resource9) | Yes | Resource object.| 3712 3713**Return value** 3714 3715| Type | Description | 3716| ------ | ---------------- | 3717| number | Color value corresponding to the resource object (decimal).| 3718 3719**Error codes** 3720 3721For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3722 3723| ID| Error Message| 3724| -------- | ---------------------------------------- | 3725| 9001001 | If the resId invalid. | 3726| 9001002 | If the resource not found by resId. | 3727| 9001006 | If the resource re-ref too much. | 3728 3729**Example** 3730 ```ts 3731 import resourceManager from '@ohos.resourceManager'; 3732 import { BusinessError } from '@ohos.base'; 3733 3734 let resource: resourceManager.Resource = { 3735 bundleName: "com.example.myapplication", 3736 moduleName: "entry", 3737 id: $r('app.color.test').id 3738 }; 3739 try { 3740 this.context.resourceManager.getColorSync(resource); 3741 } catch (error) { 3742 let code = (error as BusinessError).code; 3743 let message = (error as BusinessError).message; 3744 console.error(`getColorSync failed, error code: ${code}, message: ${message}.`); 3745 } 3746 ``` 3747 3748### getColorByNameSync<sup>10+</sup> 3749 3750getColorByNameSync(resName: string) : number; 3751 3752Obtains the color value corresponding to the specified resource name. This API returns the result synchronously. 3753 3754**System capability**: SystemCapability.Global.ResourceManager 3755 3756**Parameters** 3757 3758| Name | Type | Mandatory | Description | 3759| ------- | ------ | ---- | ---- | 3760| resName | string | Yes | Resource name.| 3761 3762**Return value** 3763 3764| Type | Description | 3765| ------ | ---------- | 3766| number | Color value corresponding to the resource name (decimal).| 3767 3768**Error codes** 3769 3770For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3771 3772| ID| Error Message| 3773| -------- | ---------------------------------------- | 3774| 9001003 | If the resName invalid. | 3775| 9001004 | If the resource not found by resName. | 3776| 9001006 | If the resource re-ref too much. | 3777 3778**Example** 3779 ```ts 3780 import { BusinessError } from '@ohos.base'; 3781 3782 try { 3783 this.context.resourceManager.getColorByNameSync("test"); 3784 } catch (error) { 3785 let code = (error as BusinessError).code; 3786 let message = (error as BusinessError).message; 3787 console.error(`getColorByNameSync failed, error code: ${code}, message: ${message}.`); 3788 } 3789 ``` 3790 3791### getColor<sup>10+</sup> 3792 3793getColor(resId: number, callback: AsyncCallback<number>): void; 3794 3795Obtains the color value corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 3796 3797**System capability**: SystemCapability.Global.ResourceManager 3798 3799**Parameters** 3800 3801| Name | Type | Mandatory | Description | 3802| -------- | --------------------------- | ---- | --------------- | 3803| resId | number | Yes | Resource ID. | 3804| callback | AsyncCallback<number> | Yes | Callback used to return the result.| 3805 3806**Error codes** 3807 3808For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3809 3810| ID| Error Message| 3811| -------- | ---------------------------------------- | 3812| 9001001 | If the module resId invalid. | 3813| 9001002 | If the resource not found by resId. | 3814| 9001006 | If the resource re-ref too much. | 3815 3816**Example (stage)** 3817 ```ts 3818 import { BusinessError } from '@ohos.base'; 3819 3820 try { 3821 this.context.resourceManager.getColor($r('app.color.test').id, (error, value) => { 3822 if (error != null) { 3823 console.log("error is " + error); 3824 } else { 3825 let str = value; 3826 } 3827 }); 3828 } catch (error) { 3829 let code = (error as BusinessError).code; 3830 let message = (error as BusinessError).message; 3831 console.error(`callback getColor failed, error code: ${code}, message: ${message}.`); 3832 } 3833 ``` 3834 3835### getColor<sup>10+</sup> 3836 3837getColor(resId: number): Promise<number> 3838 3839Obtains the color value corresponding to the specified resource ID. This API uses a promise to return the result. 3840 3841**System capability**: SystemCapability.Global.ResourceManager 3842 3843**Parameters** 3844 3845| Name | Type | Mandatory | Description | 3846| ----- | ------ | ---- | ----- | 3847| resId | number | Yes | Resource ID.| 3848 3849**Return value** 3850 3851| Type | Description | 3852| --------------------- | ----------- | 3853| Promise<number> | Promise used to return the result.| 3854 3855**Error codes** 3856 3857For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3858 3859| ID| Error Message| 3860| -------- | ---------------------------------------- | 3861| 9001001 | If the resId invalid. | 3862| 9001002 | If the resource not found by resId. | 3863| 9001006 | If the resource re-ref too much. | 3864 3865**Example** 3866 ```ts 3867 import { BusinessError } from '@ohos.base'; 3868 3869 try { 3870 this.context.resourceManager.getColor($r('app.color.test').id).then((value: number) => { 3871 let str = value; 3872 }).catch((error: BusinessError) => { 3873 console.log("getColor promise error is " + error); 3874 }); 3875 } catch (error) { 3876 let code = (error as BusinessError).code; 3877 let message = (error as BusinessError).message; 3878 console.error(`promise getColor failed, error code: ${code}, message: ${message}.`); 3879 } 3880 ``` 3881 3882### getColor<sup>10+</sup> 3883 3884getColor(resource: Resource, callback: AsyncCallback<number>): void; 3885 3886Obtains the color value corresponding to the specified resource object. This API uses an asynchronous callback to return the result. 3887 3888**System capability**: SystemCapability.Global.ResourceManager 3889 3890**Model restriction**: This API can be used only in the stage model. 3891 3892**Parameters** 3893 3894| Name | Type | Mandatory | Description | 3895| -------- | --------------------------- | ---- | --------------- | 3896| resource | [Resource](#resource9) | Yes | Resource object. | 3897| callback | AsyncCallback<number> | Yes | Callback used to return the result.| 3898 3899**Error codes** 3900 3901For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3902 3903| ID| Error Message| 3904| -------- | ---------------------------------------- | 3905| 9001001 | If the resId invalid. | 3906| 9001002 | If the resource not found by resId. | 3907| 9001006 | If the resource re-ref too much. | 3908 3909**Example** 3910 ```ts 3911 import resourceManager from '@ohos.resourceManager'; 3912 import { BusinessError } from '@ohos.base'; 3913 3914 let resource: resourceManager.Resource = { 3915 bundleName: "com.example.myapplication", 3916 moduleName: "entry", 3917 id: $r('app.color.test').id 3918 }; 3919 try { 3920 this.context.resourceManager.getColor(resource, (error, value) => { 3921 if (error != null) { 3922 console.log("error is " + error); 3923 } else { 3924 let str = value; 3925 } 3926 }); 3927 } catch (error) { 3928 let code = (error as BusinessError).code; 3929 let message = (error as BusinessError).message; 3930 console.error(`callback getColor failed, error code: ${code}, message: ${message}.`); 3931 } 3932 ``` 3933 3934### getColor<sup>10+</sup> 3935 3936getColor(resource: Resource): Promise<number>; 3937 3938Obtains the color value corresponding to the specified resource object. This API uses a promise to return the result. 3939 3940**System capability**: SystemCapability.Global.ResourceManager 3941 3942**Model restriction**: This API can be used only in the stage model. 3943 3944**Parameters** 3945 3946| Name | Type | Mandatory | Description | 3947| -------- | ---------------------- | ---- | ---- | 3948| resource | [Resource](#resource9) | Yes | Resource object.| 3949 3950**Return value** 3951 3952| Type | Description | 3953| --------------------- | ---------------- | 3954| Promise<number> | Promise used to return the result.| 3955 3956**Error codes** 3957 3958For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 3959 3960| ID| Error Message| 3961| -------- | ---------------------------------------- | 3962| 9001001 | If the resId invalid. | 3963| 9001002 | If the resource not found by resId. | 3964| 9001006 | If the resource re-ref too much. | 3965 3966**Example** 3967 ```ts 3968 import resourceManager from '@ohos.resourceManager'; 3969 import { BusinessError } from '@ohos.base'; 3970 3971 let resource: resourceManager.Resource = { 3972 bundleName: "com.example.myapplication", 3973 moduleName: "entry", 3974 id: $r('app.color.test').id 3975 }; 3976 try { 3977 this.context.resourceManager.getColor(resource).then((value: number) => { 3978 let str = value; 3979 }).catch((error: BusinessError) => { 3980 console.log("getColor promise error is " + error); 3981 }); 3982 } catch (error) { 3983 let code = (error as BusinessError).code; 3984 let message = (error as BusinessError).message; 3985 console.error(`promise getColor failed, error code: ${code}, message: ${message}.`); 3986 } 3987 ``` 3988 3989### getColorByName<sup>10+</sup> 3990 3991getColorByName(resName: string, callback: AsyncCallback<number>): void 3992 3993Obtains the color value corresponding to the specified resource name. This API uses an asynchronous callback to return the result. 3994 3995**System capability**: SystemCapability.Global.ResourceManager 3996 3997**Parameters** 3998 3999| Name | Type | Mandatory | Description | 4000| -------- | --------------------------- | ---- | --------------- | 4001| resName | string | Yes | Resource name. | 4002| callback | AsyncCallback<number> | Yes | Callback used to return the result.| 4003 4004**Error codes** 4005 4006For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4007 4008| ID| Error Message| 4009| -------- | ---------------------------------------- | 4010| 9001003 | If the resName invalid. | 4011| 9001004 | If the resource not found by resName. | 4012| 9001006 | If the resource re-ref too much. | 4013 4014**Example** 4015 ```ts 4016 import { BusinessError } from '@ohos.base'; 4017 4018 try { 4019 this.context.resourceManager.getColorByName("test", (error, value) => { 4020 if (error != null) { 4021 console.log("error is " + error); 4022 } else { 4023 let string = value; 4024 } 4025 }); 4026 } catch (error) { 4027 let code = (error as BusinessError).code; 4028 let message = (error as BusinessError).message; 4029 console.error(`callback getColorByName failed, error code: ${code}, message: ${message}.`); 4030 } 4031 ``` 4032 4033### getColorByName<sup>10+</sup> 4034 4035getColorByName(resName: string): Promise<number> 4036 4037Obtains the color value corresponding to the specified resource name. This API uses a promise to return the result. 4038 4039**System capability**: SystemCapability.Global.ResourceManager 4040 4041**Parameters** 4042 4043| Name | Type | Mandatory | Description | 4044| ------- | ------ | ---- | ---- | 4045| resName | string | Yes | Resource name.| 4046 4047**Return value** 4048 4049| Type | Description | 4050| --------------------- | ---------- | 4051| Promise<number> | Promise used to return the result.| 4052 4053**Error codes** 4054 4055For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4056 4057| ID| Error Message| 4058| -------- | ---------------------------------------- | 4059| 9001003 | If the resName invalid. | 4060| 9001004 | If the resource not found by resName. | 4061| 9001006 | If the resource re-ref too much. | 4062 4063**Example** 4064 ```ts 4065 import { BusinessError } from '@ohos.base'; 4066 4067 try { 4068 this.context.resourceManager.getColorByName("test").then((value: number) => { 4069 let string = value; 4070 }).catch((error: BusinessError) => { 4071 console.log("getColorByName promise error is " + error); 4072 }); 4073 } catch (error) { 4074 let code = (error as BusinessError).code; 4075 let message = (error as BusinessError).message; 4076 console.error(`promise getColorByName failed, error code: ${code}, message: ${message}.`); 4077 } 4078 ``` 4079 4080### getRawFileContentSync<sup>10+</sup> 4081 4082getRawFileContentSync(path: string): Uint8Array 4083 4084Obtains the content of the raw file in the **resources/rawfile** directory. This API returns the result synchronously. 4085 4086**System capability**: SystemCapability.Global.ResourceManager 4087 4088**Parameters** 4089 4090| Name | Type | Mandatory | Description | 4091| -------- | ------------------------------- | ---- | ----------------------- | 4092| path | string | Yes | Path of the raw file. | 4093 4094**Return value** 4095 4096| Type | Description | 4097| --------------------- | ---------- | 4098| Uint8Array | Content of the raw file.| 4099 4100**Error codes** 4101 4102For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4103 4104| ID| Error Message| 4105| -------- | ---------------------------------------- | 4106| 9001005 | If the resource not found by path. | 4107 4108**Example** 4109 ```ts 4110 import { BusinessError } from '@ohos.base'; 4111 4112 try { 4113 this.context.resourceManager.getRawFileContentSync("test.txt"); 4114 } catch (error) { 4115 let code = (error as BusinessError).code; 4116 let message = (error as BusinessError).message; 4117 console.error(`getRawFileContentSync failed, error code: ${code}, message: ${message}.`); 4118 } 4119 ``` 4120 4121### getRawFileContent<sup>9+</sup> 4122 4123getRawFileContent(path: string, callback: AsyncCallback<Uint8Array>): void 4124 4125Obtains the content of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. 4126 4127**System capability**: SystemCapability.Global.ResourceManager 4128 4129**Parameters** 4130 4131| Name | Type | Mandatory | Description | 4132| -------- | ------------------------------- | ---- | ----------------------- | 4133| path | string | Yes | Path of the raw file. | 4134| callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| 4135 4136**Error codes** 4137 4138For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4139 4140| ID| Error Message| 4141| -------- | ---------------------------------------- | 4142| 9001005 | If the resource not found by path. | 4143 4144**Example** 4145 ```ts 4146 import { BusinessError } from '@ohos.base'; 4147 4148 try { 4149 this.context.resourceManager.getRawFileContent("test.txt", (error, value) => { 4150 if (error != null) { 4151 console.log("error is " + error); 4152 } else { 4153 let rawFile = value; 4154 } 4155 }); 4156 } catch (error) { 4157 let code = (error as BusinessError).code; 4158 let message = (error as BusinessError).message; 4159 console.error(`callback getRawFileContent failed, error code: ${code}, message: ${message}.`); 4160 } 4161 ``` 4162 4163### getRawFileContent<sup>9+</sup> 4164 4165getRawFileContent(path: string): Promise<Uint8Array> 4166 4167Obtains the content of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. 4168 4169**System capability**: SystemCapability.Global.ResourceManager 4170 4171**Parameters** 4172 4173| Name | Type | Mandatory | Description | 4174| ---- | ------ | ---- | ----------- | 4175| path | string | Yes | Path of the raw file.| 4176 4177**Return value** 4178 4179| Type | Description | 4180| ------------------------- | ----------- | 4181| Promise<Uint8Array> | Promise used to return the result.| 4182 4183**Error codes** 4184 4185For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4186 4187| ID| Error Message| 4188| -------- | ---------------------------------------- | 4189| 9001005 | If the resource not found by path. | 4190 4191**Example** 4192 ```ts 4193 import { BusinessError } from '@ohos.base'; 4194 4195 try { 4196 this.context.resourceManager.getRawFileContent("test.txt").then((value: Uint8Array) => { 4197 let rawFile = value; 4198 }).catch((error: BusinessError) => { 4199 console.log("getRawFileContent promise error is " + error); 4200 }); 4201 } catch (error) { 4202 let code = (error as BusinessError).code; 4203 let message = (error as BusinessError).message; 4204 console.error(`promise getRawFileContent failed, error code: ${code}, message: ${message}.`); 4205 } 4206 ``` 4207 4208### getRawFileListSync<sup>10+</sup> 4209 4210getRawFileListSync(path: string): Array\<string\> 4211 4212Obtains the list of files in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. 4213 4214**System capability**: SystemCapability.Global.ResourceManager 4215 4216**Parameters** 4217 4218| Name | Type | Mandatory | Description | 4219| -------- | ------------------------------- | ---- | ----------------------- | 4220| path | string | Yes | Path of the **rawfile** folder. | 4221 4222**Return value** 4223 4224| Type | Description | 4225| ------------------------- | ----------- | 4226| Array\<string\> | List of files in the **resources/rawfile** directory.| 4227 4228**Error codes** 4229 4230For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4231 4232| ID| Error Message| 4233| -------- | ---------------------------------------- | 4234| 9001005 | If the resource not found by path. | 4235 4236**Example** 4237 ```ts 4238 import { BusinessError } from '@ohos.base'; 4239 4240 try { // Passing "" means to obtain the list of files in the root directory of the raw file. 4241 this.context.resourceManager.getRawFileListSync("") 4242 } catch (error) { 4243 let code = (error as BusinessError).code; 4244 let message = (error as BusinessError).message; 4245 console.error(`getRawFileListSync failed, error code: ${code}, message: ${message}.`); 4246 } 4247 ``` 4248 4249### getRawFileList<sup>10+</sup> 4250 4251getRawFileList(path: string, callback: AsyncCallback<Array\<string\>>): void; 4252 4253Obtains the list of files in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. 4254 4255**System capability**: SystemCapability.Global.ResourceManager 4256 4257**Parameters** 4258 4259| Name | Type | Mandatory | Description | 4260| -------- | ------------------------------- | ---- | ----------------------- | 4261| path | string | Yes | Path of the **rawfile** folder. | 4262| callback | AsyncCallback<Array\<string\>> | Yes| Callback used to return the result.| 4263 4264**Error codes** 4265 4266For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4267 4268| ID| Error Message| 4269| -------- | ---------------------------------------- | 4270| 9001005 | If the resource not found by path. | 4271 4272**Example** 4273 ```ts 4274 import { BusinessError } from '@ohos.base'; 4275 4276 try { // Passing "" means to obtain the list of files in the root directory of the raw file. 4277 this.context.resourceManager.getRawFileList("", (error, value) => { 4278 if (error != null) { 4279 console.error(`callback getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); 4280 } else { 4281 let rawFile = value; 4282 } 4283 }); 4284 } catch (error) { 4285 let code = (error as BusinessError).code; 4286 let message = (error as BusinessError).message; 4287 console.error(`callback getRawFileList failed, error code: ${code}, message: ${message}.`); 4288 } 4289 ``` 4290 4291### getRawFileList<sup>10+</sup> 4292 4293getRawFileList(path: string): Promise<Array\<string\>> 4294 4295Obtains the list of files in the **resources/rawfile** directory. This API uses a promise to return the result. 4296 4297**System capability**: SystemCapability.Global.ResourceManager 4298 4299**Parameters** 4300 4301| Name | Type | Mandatory | Description | 4302| ---- | ------ | ---- | ----------- | 4303| path | string | Yes | Path of the **rawfile** folder.| 4304 4305**Return value** 4306 4307| Type | Description | 4308| ------------------------- | ----------- | 4309| Promise<Array\<string\>> | Promise used to return the result.| 4310 4311**Error codes** 4312 4313For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4314 4315| ID| Error Message| 4316| -------- | ---------------------------------------- | 4317| 9001005 | If the resource not found by path. | 4318 4319**Example** 4320 ```ts 4321 import { BusinessError } from '@ohos.base'; 4322 4323 try { // Passing "" means to obtain the list of files in the root directory of the raw file. 4324 this.context.resourceManager.getRawFileList("").then((value: Array<string>) => { 4325 let rawFile = value; 4326 }).catch((error: BusinessError) => { 4327 console.error(`promise getRawFileList failed, error code: ${error.code}, message: ${error.message}.`); 4328 }); 4329 } catch (error) { 4330 let code = (error as BusinessError).code; 4331 let message = (error as BusinessError).message; 4332 console.error(`promise getRawFileList failed, error code: ${code}, message: ${message}.`); 4333 } 4334 ``` 4335 4336### getRawFdSync<sup>10+</sup> 4337 4338getRawFdSync(path: string): RawFileDescriptor 4339 4340Obtains the descriptor of the raw file in the **resources/rawfile** directory. 4341 4342**System capability**: SystemCapability.Global.ResourceManager 4343 4344**Parameters** 4345 4346| Name | Type | Mandatory | Description | 4347| -------- | ---------------------------------------- | ---- | -------------------------------- | 4348| path | string | Yes | Path of the raw file. | 4349 4350**Return value** 4351 4352| Type | Description | 4353| ------------------------- | ----------- | 4354| [RawFileDescriptor](#rawfiledescriptor8) | Descriptor of the raw file.| 4355 4356**Error codes** 4357 4358For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4359 4360| ID| Error Message| 4361| -------- | ---------------------------------------- | 4362| 9001005 | If the resource not found by path. | 4363 4364**Example** 4365 ```ts 4366 import { BusinessError } from '@ohos.base'; 4367 4368 try { 4369 this.context.resourceManager.getRawFdSync("test.txt"); 4370 } catch (error) { 4371 let code = (error as BusinessError).code; 4372 let message = (error as BusinessError).message; 4373 console.error(`getRawFdSync failed, error code: ${code}, message: ${message}.`); 4374 } 4375 ``` 4376 4377### getRawFd<sup>9+</sup> 4378 4379getRawFd(path: string, callback: AsyncCallback<RawFileDescriptor>): void 4380 4381Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. 4382 4383**System capability**: SystemCapability.Global.ResourceManager 4384 4385**Parameters** 4386 4387| Name | Type | Mandatory | Description | 4388| -------- | ---------------------------------------- | ---- | -------------------------------- | 4389| path | string | Yes | Path of the raw file. | 4390| callback | AsyncCallback<[RawFileDescriptor](#rawfiledescriptor8)> | Yes | Callback used to return the result.| 4391 4392**Error codes** 4393 4394For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4395 4396| ID| Error Message| 4397| -------- | ---------------------------------------- | 4398| 9001005 | If the resource not found by path. | 4399 4400**Example** 4401 ```ts 4402 import { BusinessError } from '@ohos.base'; 4403 4404 try { 4405 this.context.resourceManager.getRawFd("test.txt", (error, value) => { 4406 if (error != null) { 4407 console.log(`callback getRawFd failed error code: ${error.code}, message: ${error.message}.`); 4408 } else { 4409 let fd = value.fd; 4410 let offset = value.offset; 4411 let length = value.length; 4412 } 4413 }); 4414 } catch (error) { 4415 let code = (error as BusinessError).code; 4416 let message = (error as BusinessError).message; 4417 console.error(`callback getRawFd failed, error code: ${code}, message: ${message}.`); 4418 } 4419 ``` 4420 4421### getRawFd<sup>9+</sup> 4422 4423getRawFd(path: string): Promise<RawFileDescriptor> 4424 4425Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. 4426 4427**System capability**: SystemCapability.Global.ResourceManager 4428 4429**Parameters** 4430 4431| Name | Type | Mandatory | Description | 4432| ---- | ------ | ---- | ----------- | 4433| path | string | Yes | Path of the raw file.| 4434 4435**Return value** 4436 4437| Type | Description | 4438| ---------------------------------------- | ------------------- | 4439| Promise<[RawFileDescriptor](#rawfiledescriptor8)> | Promise used to return the result.| 4440 4441**Error codes** 4442 4443For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4444 4445| ID| Error Message| 4446| -------- | ---------------------------------------- | 4447| 9001005 | If the resource not found by path. | 4448 4449**Example** 4450 ```ts 4451 import { BusinessError } from '@ohos.base'; 4452 4453 try { 4454 this.context.resourceManager.getRawFd("test.txt").then((value: resourceManager.RawFileDescriptor) => { 4455 let fd = value.fd; 4456 let offset = value.offset; 4457 let length = value.length; 4458 }).catch((error: BusinessError) => { 4459 console.log(`promise getRawFd error error code: ${error.code}, message: ${error.message}.`); 4460 }); 4461 } catch (error) { 4462 let code = (error as BusinessError).code; 4463 let message = (error as BusinessError).message; 4464 console.error(`promise getRawFd failed, error code: ${code}, message: ${message}.`); 4465 } 4466 ``` 4467 4468### closeRawFdSync<sup>10+</sup> 4469 4470closeRawFdSync(path: string): void 4471 4472Closes the descriptor of the raw file in the **resources/rawfile** directory. 4473 4474**System capability**: SystemCapability.Global.ResourceManager 4475 4476**Parameters** 4477 4478| Name | Type | Mandatory | Description | 4479| -------- | ------------------------- | ---- | ----------- | 4480| path | string | Yes | Path of the raw file.| 4481 4482**Error codes** 4483 4484For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4485 4486| ID| Error Message| 4487| -------- | ---------------------------------------- | 4488| 9001005 | The resource not found by path. | 4489 4490**Example** 4491 ```ts 4492 import { BusinessError } from '@ohos.base'; 4493 4494 try { 4495 this.context.resourceManager.closeRawFdSync("test.txt"); 4496 } catch (error) { 4497 let code = (error as BusinessError).code; 4498 let message = (error as BusinessError).message; 4499 console.error(`closeRawFd failed, error code: ${code}, message: ${message}.`); 4500 } 4501 ``` 4502 4503### closeRawFd<sup>9+</sup> 4504 4505closeRawFd(path: string, callback: AsyncCallback<void>): void 4506 4507Closes the descriptor of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. 4508 4509**System capability**: SystemCapability.Global.ResourceManager 4510 4511**Parameters** 4512 4513| Name | Type | Mandatory | Description | 4514| -------- | ------------------------- | ---- | ----------- | 4515| path | string | Yes | Path of the raw file.| 4516| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 4517 4518**Error codes** 4519 4520For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4521 4522| ID| Error Message| 4523| -------- | ---------------------------------------- | 4524| 9001005 | The resource not found by path. | 4525 4526**Example** 4527 ```ts 4528 import { BusinessError } from '@ohos.base'; 4529 4530 try { 4531 this.context.resourceManager.closeRawFd("test.txt", (error, value) => { 4532 if (error != null) { 4533 console.log("error is " + error); 4534 } 4535 }); 4536 } catch (error) { 4537 let code = (error as BusinessError).code; 4538 let message = (error as BusinessError).message; 4539 console.error(`callback closeRawFd failed, error code: ${code}, message: ${message}.`); 4540 } 4541 ``` 4542 4543### closeRawFd<sup>9+</sup> 4544 4545closeRawFd(path: string): Promise<void> 4546 4547Closes the descriptor of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. 4548 4549**System capability**: SystemCapability.Global.ResourceManager 4550 4551**Parameters** 4552 4553| Name | Type | Mandatory | Description | 4554| ---- | ------ | ---- | ----------- | 4555| path | string | Yes | Path of the raw file.| 4556 4557**Return value** 4558 4559| Type | Description | 4560| ------------------- | ---- | 4561| Promise<void> | Promise that returns no value.| 4562 4563**Error codes** 4564 4565For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4566 4567| ID| Error Message| 4568| -------- | ---------------------------------------- | 4569| 9001005 | If the resource not found by path. | 4570 4571**Example** 4572 ```ts 4573 import { BusinessError } from '@ohos.base'; 4574 4575 try { 4576 this.context.resourceManager.closeRawFd("test.txt"); 4577 } catch (error) { 4578 let code = (error as BusinessError).code; 4579 let message = (error as BusinessError).message; 4580 console.error(`promise closeRawFd failed, error code: ${code}, message: ${message}.`); 4581 } 4582 ``` 4583 4584### getConfigurationSync 4585 4586getConfigurationSync(): Configuration 4587 4588Obtains the device configuration. This API return the results synchronously. 4589 4590**System capability**: SystemCapability.Global.ResourceManager 4591 4592**Return value** 4593 4594| Type | Description | 4595| ---------------------------------------- | ---------------- | 4596| [Configuration](#configuration) | Promise used to return the result.| 4597 4598**Example** 4599 ```ts 4600 try { 4601 let value = this.context.resourceManager.getConfigurationSync(); 4602 let direction = value.direction; 4603 let locale = value.locale; 4604 } catch (error) { 4605 console.error("getConfigurationSync error is " + error); 4606 } 4607 ``` 4608 4609### getConfiguration 4610 4611getConfiguration(callback: AsyncCallback<Configuration>): void 4612 4613Obtains the device configuration. This API uses an asynchronous callback to return the result. 4614 4615**System capability**: SystemCapability.Global.ResourceManager 4616 4617**Parameters** 4618 4619| Name | Type | Mandatory | Description | 4620| -------- | ---------------------------------------- | ---- | ------------------------- | 4621| callback | AsyncCallback<[Configuration](#configuration)> | Yes | Callback used to return the result.| 4622 4623**Example** 4624 ```ts 4625 try { 4626 this.context.resourceManager.getConfiguration((error, value) => { 4627 if (error != null) { 4628 console.error("getConfiguration callback error is " + error); 4629 } else { 4630 let direction = value.direction; 4631 let locale = value.locale; 4632 } 4633 }); 4634 } catch (error) { 4635 console.error("getConfiguration callback error is " + error); 4636 } 4637 ``` 4638 4639### getConfiguration 4640 4641getConfiguration(): Promise<Configuration> 4642 4643Obtains the device configuration. This API uses a promise to return the result. 4644 4645**System capability**: SystemCapability.Global.ResourceManager 4646 4647**Return value** 4648 4649| Type | Description | 4650| ---------------------------------------- | ---------------- | 4651| Promise<[Configuration](#configuration)> | Promise used to return the result.| 4652 4653**Example** 4654 ```ts 4655 import { BusinessError } from '@ohos.base'; 4656 4657 try { 4658 this.context.resourceManager.getConfiguration().then((value: resourceManager.Configuration) => { 4659 let direction = value.direction; 4660 let locale = value.locale; 4661 }).catch((error: BusinessError) => { 4662 console.error("getConfiguration promise error is " + error); 4663 }); 4664 } catch (error) { 4665 console.error("getConfiguration promise error is " + error); 4666 } 4667 ``` 4668 4669### getDeviceCapabilitySync 4670 4671getDeviceCapabilitySync(): DeviceCapability 4672 4673Obtains the device capability. This API return the results synchronously. 4674 4675**System capability**: SystemCapability.Global.ResourceManager 4676 4677**Return value** 4678 4679| Type | Description | 4680| ---------------------------------------- | ------------------- | 4681| [DeviceCapability](#devicecapability) | Promise used to return the result.| 4682 4683**Example** 4684 ```ts 4685 try { 4686 let value = this.context.resourceManager.getDeviceCapabilitySync(); 4687 let screenDensity = value.screenDensity; 4688 let deviceType = value.deviceType; 4689 } catch (error) { 4690 console.error("getDeviceCapabilitySync error is " + error); 4691 } 4692 ``` 4693 4694### getDeviceCapability 4695 4696getDeviceCapability(callback: AsyncCallback<DeviceCapability>): void 4697 4698Obtains the device capability. This API uses an asynchronous callback to return the result. 4699 4700**System capability**: SystemCapability.Global.ResourceManager 4701 4702**Parameters** 4703 4704| Name | Type | Mandatory | Description | 4705| -------- | ---------------------------------------- | ---- | ---------------------------- | 4706| callback | AsyncCallback<[DeviceCapability](#devicecapability)> | Yes | Callback used to return the result.| 4707 4708**Example** 4709 ```ts 4710 try { 4711 this.context.resourceManager.getDeviceCapability((error, value) => { 4712 if (error != null) { 4713 console.error("getDeviceCapability callback error is " + error); 4714 } else { 4715 let screenDensity = value.screenDensity; 4716 let deviceType = value.deviceType; 4717 } 4718 }); 4719 } catch (error) { 4720 console.error("getDeviceCapability callback error is " + error); 4721 } 4722 ``` 4723 4724### getDeviceCapability 4725 4726getDeviceCapability(): Promise<DeviceCapability> 4727 4728Obtains the device capability. This API uses a promise to return the result. 4729 4730**System capability**: SystemCapability.Global.ResourceManager 4731 4732**Return value** 4733 4734| Type | Description | 4735| ---------------------------------------- | ------------------- | 4736| Promise<[DeviceCapability](#devicecapability)> | Promise used to return the result.| 4737 4738**Example** 4739 ```ts 4740 import { BusinessError } from '@ohos.base'; 4741 4742 try { 4743 this.context.resourceManager.getDeviceCapability().then((value: resourceManager.DeviceCapability) => { 4744 let screenDensity = value.screenDensity; 4745 let deviceType = value.deviceType; 4746 }).catch((error: BusinessError) => { 4747 console.error("getDeviceCapability promise error is " + error); 4748 }); 4749 } catch (error) { 4750 console.error("getDeviceCapability promise error is " + error); 4751 } 4752 ``` 4753 4754### release<sup>7+</sup> 4755 4756release() 4757 4758Releases a **ResourceManager** object. This API is not supported currently. 4759 4760**System capability**: SystemCapability.Global.ResourceManager 4761 4762**Example** 4763 ```ts 4764 try { 4765 this.context.resourceManager.release(); 4766 } catch (error) { 4767 console.error("release error is " + error); 4768 } 4769 ``` 4770 4771### addResource<sup>10+</sup> 4772 4773addResource(path: string) : void; 4774 4775Loads resources from the specified path. 4776 4777**System capability**: SystemCapability.Global.ResourceManager 4778 4779**Parameters** 4780 4781| Name | Type | Mandatory | Description | 4782| -------- | ---------------------- | ---- | ---- | 4783| path | string | Yes | Resource path.| 4784 4785**Error codes** 4786 4787For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4788 4789| ID| Error Message| 4790| -------- | ---------------------------------------- | 4791| 9001010 | If the overlay path is invalid. | 4792 4793**Example** 4794 ```ts 4795 import { BusinessError } from '@ohos.base'; 4796 4797 let path = getContext().bundleCodeDir + "/library1-default-signed.hsp"; 4798 try { 4799 this.context.resourceManager.addResource(path); 4800 } catch (error) { 4801 let code = (error as BusinessError).code; 4802 let message = (error as BusinessError).message; 4803 console.error(`addResource failed, error code: ${code}, message: ${message}.`); 4804 } 4805 ``` 4806 4807### removeResource<sup>10+</sup> 4808 4809removeResource(path: string) : void; 4810 4811Removes the resources loaded from the specified path to restore the original resources. 4812 4813**System capability**: SystemCapability.Global.ResourceManager 4814 4815**Parameters** 4816 4817| Name | Type | Mandatory | Description | 4818| -------- | ---------------------- | ---- | ---- | 4819| path | string | Yes | Resource path.| 4820 4821**Error codes** 4822 4823For details about the error codes, see [Resource Manager Error Codes](../errorcodes/errorcode-resource-manager.md). 4824 4825| ID| Error Message| 4826| -------- | ---------------------------------------- | 4827| 9001010 | If the overlay path is invalid. | 4828 4829**Example** 4830 ```ts 4831 import { BusinessError } from '@ohos.base'; 4832 4833 let path = getContext().bundleCodeDir + "/library1-default-signed.hsp"; 4834 try { 4835 this.context.resourceManager.removeResource(path); 4836 } catch (error) { 4837 let code = (error as BusinessError).code; 4838 let message = (error as BusinessError).message; 4839 console.error(`removeResource failed, error code: ${code}, message: ${message}.`); 4840 } 4841 ``` 4842 4843### getString<sup>(deprecated)</sup> 4844 4845getString(resId: number, callback: AsyncCallback<string>): void 4846 4847Obtains the string corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 4848 4849This API is deprecated since API version 9. You are advised to use [getStringValue](#getstringvalue9) instead. 4850 4851**System capability**: SystemCapability.Global.ResourceManager 4852 4853**Parameters** 4854 4855| Name | Type | Mandatory | Description | 4856| -------- | --------------------------- | ---- | --------------- | 4857| resId | number | Yes | Resource ID. | 4858| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 4859 4860**Example** 4861 ```ts 4862 resourceManager.getResourceManager((error, mgr) => { 4863 mgr.getString($r('app.string.test').id, (error, value) => { 4864 if (error != null) { 4865 console.log("error is " + error); 4866 } else { 4867 let str = value; 4868 } 4869 }); 4870 }); 4871 ``` 4872 4873 4874### getString<sup>(deprecated)</sup> 4875 4876getString(resId: number): Promise<string> 4877 4878Obtains the string corresponding to the specified resource ID. This API uses a promise to return the result. 4879 4880This API is deprecated since API version 9. You are advised to use [getStringValue](#getstringvalue9-1) instead. 4881 4882**System capability**: SystemCapability.Global.ResourceManager 4883 4884**Parameters** 4885 4886| Name | Type | Mandatory | Description | 4887| ----- | ------ | ---- | ----- | 4888| resId | number | Yes | Resource ID.| 4889 4890**Return value** 4891 4892| Type | Description | 4893| --------------------- | ----------- | 4894| Promise<string> | Promise used to return the result.| 4895 4896**Example** 4897 ```ts 4898 import { BusinessError } from '@ohos.base'; 4899 4900 resourceManager.getResourceManager((error, mgr) => { 4901 mgr.getString($r('app.string.test').id).then((value: string) => { 4902 let str = value; 4903 }).catch((error: BusinessError) => { 4904 console.log("getstring promise error is " + error); 4905 }); 4906 }); 4907 ``` 4908 4909 4910### getStringArray<sup>(deprecated)</sup> 4911 4912getStringArray(resId: number, callback: AsyncCallback<Array<string>>): void 4913 4914Obtains the string array corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 4915 4916This API is deprecated since API version 9. You are advised to use [getStringArrayValue](#getstringarrayvalue9) instead. 4917 4918**System capability**: SystemCapability.Global.ResourceManager 4919 4920**Parameters** 4921 4922| Name | Type | Mandatory | Description | 4923| -------- | ---------------------------------------- | ---- | ----------------- | 4924| resId | number | Yes | Resource ID. | 4925| callback | AsyncCallback<Array<string>> | Yes | Callback used to return the result.| 4926 4927**Example** 4928 ```ts 4929 resourceManager.getResourceManager((error, mgr) => { 4930 mgr.getStringArray($r('app.strarray.test').id, (error, value) => { 4931 if (error != null) { 4932 console.log("error is " + error); 4933 } else { 4934 let strArray = value; 4935 } 4936 }); 4937 }); 4938 ``` 4939 4940 4941### getStringArray<sup>(deprecated)</sup> 4942 4943getStringArray(resId: number): Promise<Array<string>> 4944 4945Obtains the string array corresponding to the specified resource ID. This API uses a promise to return the result. 4946 4947This API is deprecated since API version 9. You are advised to use [getStringArrayValue](#getstringarrayvalue9-1) instead. 4948 4949**System capability**: SystemCapability.Global.ResourceManager 4950 4951**Parameters** 4952 4953| Name | Type | Mandatory | Description | 4954| ----- | ------ | ---- | ----- | 4955| resId | number | Yes | Resource ID.| 4956 4957**Return value** 4958 4959| Type | Description | 4960| ---------------------------------- | ------------- | 4961| Promise<Array<string>> | Promise used to return the result.| 4962 4963**Example** 4964 ```ts 4965 import { BusinessError } from '@ohos.base'; 4966 4967 resourceManager.getResourceManager((error, mgr) => { 4968 mgr.getStringArray($r('app.strarray.test').id).then((value: Array<string>) => { 4969 let strArray = value; 4970 }).catch((error: BusinessError) => { 4971 console.log("getStringArray promise error is " + error); 4972 }); 4973 }); 4974 ``` 4975 4976 4977### getMedia<sup>(deprecated)</sup> 4978 4979getMedia(resId: number, callback: AsyncCallback<Uint8Array>): void 4980 4981Obtains the media file content corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 4982 4983This API is deprecated since API version 9. You are advised to use [getMediaContent](#getmediacontent9) instead. 4984 4985**System capability**: SystemCapability.Global.ResourceManager 4986 4987**Parameters** 4988 4989| Name | Type | Mandatory | Description | 4990| -------- | ------------------------------- | ---- | ------------------ | 4991| resId | number | Yes | Resource ID. | 4992| callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| 4993 4994**Example** 4995 ```ts 4996 resourceManager.getResourceManager((error, mgr) => { 4997 mgr.getMedia($r('app.media.test').id, (error, value) => { 4998 if (error != null) { 4999 console.log("error is " + error); 5000 } else { 5001 let media = value; 5002 } 5003 }); 5004 }); 5005 ``` 5006 5007 5008### getMedia<sup>(deprecated)</sup> 5009 5010getMedia(resId: number): Promise<Uint8Array> 5011 5012Obtains the media file content corresponding to the specified resource ID. This API uses a promise to return the result. 5013 5014This API is deprecated since API version 9. You are advised to use [getMediaContent](#getmediacontent9-1) instead. 5015 5016**System capability**: SystemCapability.Global.ResourceManager 5017 5018**Parameters** 5019 5020| Name | Type | Mandatory | Description | 5021| ----- | ------ | ---- | ----- | 5022| resId | number | Yes | Resource ID.| 5023 5024**Return value** 5025 5026| Type | Description | 5027| ------------------------- | -------------- | 5028| Promise<Uint8Array> | Promise used to return the result.| 5029 5030**Example** 5031 ```ts 5032 import { BusinessError } from '@ohos.base'; 5033 5034 resourceManager.getResourceManager((error, mgr) => { 5035 mgr.getMedia($r('app.media.test').id).then((value: Uint8Array) => { 5036 let media = value; 5037 }).catch((error: BusinessError) => { 5038 console.log("getMedia promise error is " + error); 5039 }); 5040 }); 5041 ``` 5042 5043 5044### getMediaBase64<sup>(deprecated)</sup> 5045 5046getMediaBase64(resId: number, callback: AsyncCallback<string>): void 5047 5048Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses an asynchronous callback to return the result. 5049 5050This API is deprecated since API version 9. You are advised to use [getMediaContentBase64](#getmediacontentbase649) instead. 5051 5052**System capability**: SystemCapability.Global.ResourceManager 5053 5054**Parameters** 5055 5056| Name | Type | Mandatory | Description | 5057| -------- | --------------------------- | ---- | ------------------------ | 5058| resId | number | Yes | Resource ID. | 5059| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 5060 5061**Example** 5062 ```ts 5063 resourceManager.getResourceManager((error, mgr) => { 5064 mgr.getMediaBase64($r('app.media.test').id, (error, value) => { 5065 if (error != null) { 5066 console.log("error is " + error); 5067 } else { 5068 let media = value; 5069 } 5070 }); 5071 }); 5072 ``` 5073 5074 5075### getMediaBase64<sup>(deprecated)</sup> 5076 5077getMediaBase64(resId: number): Promise<string> 5078 5079Obtains the Base64 code of the image corresponding to the specified resource ID. This API uses a promise to return the result. 5080 5081This API is deprecated since API version 9. You are advised to use [getMediaContentBase64](#getmediacontentbase649-1) instead. 5082 5083**System capability**: SystemCapability.Global.ResourceManager 5084 5085**Parameters** 5086 5087| Name | Type | Mandatory | Description | 5088| ----- | ------ | ---- | ----- | 5089| resId | number | Yes | Resource ID.| 5090 5091**Return value** 5092 5093| Type | Description | 5094| --------------------- | -------------------- | 5095| Promise<string> | Promise used to return the result.| 5096 5097**Example** 5098 ```ts 5099 import { BusinessError } from '@ohos.base'; 5100 5101 resourceManager.getResourceManager((error, mgr) => { 5102 mgr.getMediaBase64($r('app.media.test').id).then((value: string) => { 5103 let media = value; 5104 }).catch((error: BusinessError) => { 5105 console.log("getMediaBase64 promise error is " + error); 5106 }); 5107 }); 5108 ``` 5109 5110 5111### getPluralString<sup>(deprecated)</sup> 5112 5113getPluralString(resId: number, num: number): Promise<string> 5114 5115Obtains the singular-plural string corresponding to the specified resource ID based on the specified number. This API uses a promise to return the result. 5116 5117This API is deprecated since API version 9. You are advised to use [getPluralStringValue](#getpluralstringvalue9) instead. 5118 5119**System capability**: SystemCapability.Global.ResourceManager 5120 5121**Parameters** 5122 5123| Name | Type | Mandatory | Description | 5124| ----- | ------ | ---- | ----- | 5125| resId | number | Yes | Resource ID.| 5126| num | number | Yes | Number. | 5127 5128**Return value** 5129 5130| Type | Description | 5131| --------------------- | ------------------------- | 5132| Promise<string> | Promise used to return the result.| 5133 5134**Example** 5135 ```ts 5136 import { BusinessError } from '@ohos.base'; 5137 5138 resourceManager.getResourceManager((error, mgr) => { 5139 mgr.getPluralString($r("app.plural.test").id, 1).then((value: string) => { 5140 let str = value; 5141 }).catch((error: BusinessError) => { 5142 console.log("getPluralString promise error is " + error); 5143 }); 5144 }); 5145 ``` 5146 5147 5148### getPluralString<sup>(deprecated)</sup> 5149 5150getPluralString(resId: number, num: number, callback: AsyncCallback<string>): void 5151 5152Obtains 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. 5153 5154This API is deprecated since API version 9. You are advised to use [getPluralStringValue](#getpluralstringvalue9-1) instead. 5155 5156**System capability**: SystemCapability.Global.ResourceManager 5157 5158**Parameters** 5159 5160| Name | Type | Mandatory | Description | 5161| -------- | --------------------------- | ---- | ------------------------------- | 5162| resId | number | Yes | Resource ID. | 5163| num | number | Yes | Number. | 5164| callback | AsyncCallback<string> | Yes | Callback used to return the result.| 5165 5166**Example** 5167 ```ts 5168 resourceManager.getResourceManager((error, mgr) => { 5169 mgr.getPluralString($r("app.plural.test").id, 1, (error, value) => { 5170 if (error != null) { 5171 console.log("error is " + error); 5172 } else { 5173 let str = value; 5174 } 5175 }); 5176 }); 5177 ``` 5178 5179 5180### getRawFile<sup>(deprecated)</sup> 5181 5182getRawFile(path: string, callback: AsyncCallback<Uint8Array>): void 5183 5184Obtains the content of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. 5185 5186This API is deprecated since API version 9. You are advised to use [getRawFileContent](#getrawfilecontent9) instead. 5187 5188**System capability**: SystemCapability.Global.ResourceManager 5189 5190**Parameters** 5191 5192| Name | Type | Mandatory | Description | 5193| -------- | ------------------------------- | ---- | ----------------------- | 5194| path | string | Yes | Path of the raw file. | 5195| callback | AsyncCallback<Uint8Array> | Yes | Callback used to return the result.| 5196 5197**Example** 5198 ```ts 5199 resourceManager.getResourceManager((error, mgr) => { 5200 mgr.getRawFile("test.txt", (error, value) => { 5201 if (error != null) { 5202 console.log("error is " + error); 5203 } else { 5204 let rawFile = value; 5205 } 5206 }); 5207 }); 5208 ``` 5209 5210 5211### getRawFile<sup>(deprecated)</sup> 5212 5213getRawFile(path: string): Promise<Uint8Array> 5214 5215Obtains the content of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. 5216 5217This API is deprecated since API version 9. You are advised to use [getRawFileContent](#getrawfilecontent9-1) instead. 5218 5219**System capability**: SystemCapability.Global.ResourceManager 5220 5221**Parameters** 5222 5223| Name | Type | Mandatory | Description | 5224| ---- | ------ | ---- | ----------- | 5225| path | string | Yes | Path of the raw file.| 5226 5227**Return value** 5228 5229| Type | Description | 5230| ------------------------- | ----------- | 5231| Promise<Uint8Array> | Promise used to return the result.| 5232 5233**Example** 5234 ```ts 5235 import { BusinessError } from '@ohos.base'; 5236 5237 resourceManager.getResourceManager((error, mgr) => { 5238 mgr.getRawFile("test.txt").then((value: Uint8Array) => { 5239 let rawFile = value; 5240 }).catch((error: BusinessError) => { 5241 console.log("getRawFile promise error is " + error); 5242 }); 5243 }); 5244 ``` 5245 5246 5247### getRawFileDescriptor<sup>(deprecated)</sup> 5248 5249getRawFileDescriptor(path: string, callback: AsyncCallback<RawFileDescriptor>): void 5250 5251Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. 5252 5253This API is deprecated since API version 9. You are advised to use [getRawFd](#getrawfd9) instead. 5254 5255**System capability**: SystemCapability.Global.ResourceManager 5256 5257**Parameters** 5258 5259| Name | Type | Mandatory | Description | 5260| -------- | ---------------------------------------- | ---- | -------------------------------- | 5261| path | string | Yes | Path of the raw file. | 5262| callback | AsyncCallback<[RawFileDescriptor](#rawfiledescriptor8)> | Yes | Callback used to return the result.| 5263 5264**Example** 5265 ```ts 5266 resourceManager.getResourceManager((error, mgr) => { 5267 mgr.getRawFileDescriptor("test.txt", (error, value) => { 5268 if (error != null) { 5269 console.log("error is " + error); 5270 } else { 5271 let fd = value.fd; 5272 let offset = value.offset; 5273 let length = value.length; 5274 } 5275 }); 5276 }); 5277 ``` 5278 5279### getRawFileDescriptor<sup>(deprecated)</sup> 5280 5281getRawFileDescriptor(path: string): Promise<RawFileDescriptor> 5282 5283Obtains the descriptor of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. 5284 5285This API is deprecated since API version 9. You are advised to use [getRawFd](#getrawfd9-1) instead. 5286 5287**System capability**: SystemCapability.Global.ResourceManager 5288 5289**Parameters** 5290 5291| Name | Type | Mandatory | Description | 5292| ---- | ------ | ---- | ----------- | 5293| path | string | Yes | Path of the raw file.| 5294 5295**Return value** 5296 5297| Type | Description | 5298| ---------------------------------------- | ------------------- | 5299| Promise<[RawFileDescriptor](#rawfiledescriptor8)> | Promise used to return the result.| 5300 5301**Example** 5302 ```ts 5303 import { BusinessError } from '@ohos.base'; 5304 5305 resourceManager.getResourceManager((error, mgr) => { 5306 mgr.getRawFileDescriptor("test.txt").then((value: resourceManager.RawFileDescriptor) => { 5307 let fd = value.fd; 5308 let offset = value.offset; 5309 let length = value.length; 5310 }).catch((error: BusinessError) => { 5311 console.log("getRawFileDescriptor promise error is " + error); 5312 }); 5313 }); 5314 ``` 5315 5316### closeRawFileDescriptor<sup>(deprecated)</sup> 5317 5318closeRawFileDescriptor(path: string, callback: AsyncCallback<void>): void 5319 5320Closes the descriptor of the raw file in the **resources/rawfile** directory. This API uses an asynchronous callback to return the result. 5321 5322This API is deprecated since API version 9. You are advised to use [closeRawFd](#closerawfd9). 5323 5324**System capability**: SystemCapability.Global.ResourceManager 5325 5326**Parameters** 5327 5328| Name | Type | Mandatory | Description | 5329| -------- | ------------------------- | ---- | ----------- | 5330| path | string | Yes | Path of the raw file.| 5331| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 5332 5333**Example** 5334 ```ts 5335 resourceManager.getResourceManager((error, mgr) => { 5336 mgr.closeRawFileDescriptor("test.txt", (error, value) => { 5337 if (error != null) { 5338 console.log("error is " + error); 5339 } 5340 }); 5341 }); 5342 ``` 5343 5344### closeRawFileDescriptor<sup>(deprecated)</sup> 5345 5346closeRawFileDescriptor(path: string): Promise<void> 5347 5348Closes the descriptor of the raw file in the **resources/rawfile** directory. This API uses a promise to return the result. 5349 5350This API is deprecated since API version 9. You are advised to use [closeRawFd](#closerawfd9-1). 5351 5352**System capability**: SystemCapability.Global.ResourceManager 5353 5354**Parameters** 5355 5356| Name | Type | Mandatory | Description | 5357| ---- | ------ | ---- | ----------- | 5358| path | string | Yes | Path of the raw file.| 5359 5360**Return value** 5361 5362| Type | Description | 5363| ------------------- | ---- | 5364| Promise<void> | Promise that returns no value.| 5365 5366**Example** 5367 ```ts 5368 resourceManager.getResourceManager((error, mgr) => { 5369 mgr.closeRawFileDescriptor("test.txt"); 5370 }); 5371 ``` 5372