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