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