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