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