js-apis-resource-manager.md 88.2 KB
Newer Older
1
# 资源管理
Z
zengyawen 已提交
2

3 4
资源管理模块,根据当前configuration(语言,区域,横竖屏,mccmnc)和device capability(设备类型,分辨率)提供获取应用资源信息读取接口。

5 6
> **说明:**
>
W
wplan1 已提交
7
> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10

## 导入模块
Z
zengyawen 已提交
11

H
HelloCrease 已提交
12
```js
Z
zengyawen 已提交
13 14 15
import resourceManager from '@ohos.resourceManager';
```

H
huangjie 已提交
16 17
## 使用说明

H
huangjie 已提交
18
从API Version9开始,Stage模型支持了通过context获取resourceManager对象的方式,再调用其内部获取资源的接口,无需再导入包,此方式FA模型不适用。
H
huangjie 已提交
19

H
HelloCrease 已提交
20
```ts
H
huangjie 已提交
21 22 23
this.context.resourceManager;
```

Z
zengyawen 已提交
24 25 26
## resourceManager.getResourceManager

getResourceManager(callback: AsyncCallback<ResourceManager>): void
Z
zengyawen 已提交
27 28 29

获取当前应用的资源管理对象,使用callback形式返回ResourceManager对象。

H
huangjie 已提交
30
**模型约束**:此接口仅可在FA模型下使用。
H
huangjie 已提交
31

V
VictoriaGuo 已提交
32 33
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
34 35 36 37
**参数:** 
| 参数名      | 类型                                       | 必填   | 说明                            |
| -------- | ---------------------------------------- | ---- | ----------------------------- |
| callback | AsyncCallback<[ResourceManager](#resourcemanager)> | 是    | callback方式返回ResourceManager对象 |
Z
zengyawen 已提交
38

H
HelloCrease 已提交
39
**示例:** 
H
HelloCrease 已提交
40
  ```js
Z
zengyawen 已提交
41 42
  resourceManager.getResourceManager((error, mgr) => {
      if (error != null) {
W
wplan1 已提交
43
          console.log("error is " + error);
Z
zengyawen 已提交
44 45 46 47
          return; 
      }
      mgr.getString(0x1000000, (error, value) => {
          if (error != null) {
W
wplan1 已提交
48
              console.log("error is " + error);
Z
zengyawen 已提交
49
          } else {
W
wplan1 已提交
50
              let str = value;
Z
zengyawen 已提交
51 52 53 54 55 56 57 58 59
          }
      });
  });
  ```


## resourceManager.getResourceManager

getResourceManager(bundleName: string, callback: AsyncCallback<ResourceManager>): void
Z
zengyawen 已提交
60 61 62

获取指定应用的资源管理对象,使用callback形式返回ResourceManager对象。

H
huangjie 已提交
63 64
此接口仅可在FA模型下使用。

V
VictoriaGuo 已提交
65 66
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
67 68 69 70 71
**参数:** 
| 参数名        | 类型                                       | 必填   | 说明                            |
| ---------- | ---------------------------------------- | ---- | ----------------------------- |
| bundleName | string                                   | 是    | 指定应用的Bundle名称                 |
| callback   | AsyncCallback<[ResourceManager](#resourcemanager)> | 是    | callback方式返回ResourceManager对象 |
Z
zengyawen 已提交
72

H
HelloCrease 已提交
73
**示例:** 
H
HelloCrease 已提交
74
  ```js
Z
zengyawen 已提交
75 76 77 78 79 80 81 82
  resourceManager.getResourceManager("com.example.myapplication", (error, mgr) => {
  });
  ```


## resourceManager.getResourceManager

getResourceManager(): Promise<ResourceManager>
Z
zengyawen 已提交
83 84 85

获取当前应用的资源管理对象,使用Promise形式返回ResourceManager对象。

H
huangjie 已提交
86 87
此接口仅可在FA模型下使用。

V
VictoriaGuo 已提交
88 89
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
90 91 92 93
**返回值:** 
| 类型                                       | 说明                |
| ---------------------------------------- | ----------------- |
| Promise<[ResourceManager](#resourcemanager)> | Promise方式返回资源管理对象 |
Z
zengyawen 已提交
94

H
HelloCrease 已提交
95
**示例:** 
H
HelloCrease 已提交
96
  ```js
Z
zengyawen 已提交
97 98 99
  resourceManager.getResourceManager().then(mgr => {
      mgr.getString(0x1000000, (error, value) => {
          if (error != null) {
W
wplan1 已提交
100
              console.log("error is " + error);
Z
zengyawen 已提交
101
          } else {
W
wplan1 已提交
102
              let str = value;
Z
zengyawen 已提交
103 104 105
          }
      });
  }).catch(error => {
W
wplan1 已提交
106
      console.log("error is " + error);
Z
zengyawen 已提交
107 108 109 110 111 112 113
  });
  ```


## resourceManager.getResourceManager

getResourceManager(bundleName: string): Promise<ResourceManager>
Z
zengyawen 已提交
114 115 116

获取指定应用的资源管理对象,使用Promise形式返回ResourceManager对象。

H
huangjie 已提交
117 118
此接口仅可在FA模型下使用。

V
VictoriaGuo 已提交
119 120
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
121 122 123 124
**参数:** 
| 参数名        | 类型     | 必填   | 说明            |
| ---------- | ------ | ---- | ------------- |
| bundleName | string | 是    | 指定应用的Bundle名称 |
Z
zengyawen 已提交
125

H
HelloCrease 已提交
126 127 128 129
**返回值:** 
| 类型                                       | 说明                 |
| ---------------------------------------- | ------------------ |
| Promise<[ResourceManager](#resourcemanager)> | Promise方式返回的资源管理对象 |
Z
zengyawen 已提交
130

H
HelloCrease 已提交
131
**示例:** 
H
HelloCrease 已提交
132
  ```js
Z
zengyawen 已提交
133 134 135 136 137 138 139
  resourceManager.getResourceManager("com.example.myapplication").then(mgr => {
  }).catch(error => {
  });
  ```


## Direction
Z
zengyawen 已提交
140 141 142

用于表示设备屏幕方向。

H
HelloCrease 已提交
143
**系统能力**:SystemCapability.Global.ResourceManager
H
HelloCrease 已提交
144 145 146 147 148

| 名称                   | 默认值  | 说明   |
| -------------------- | ---- | ---- |
| DIRECTION_VERTICAL   | 0    | 竖屏   |
| DIRECTION_HORIZONTAL | 1    | 横屏   |
Z
zengyawen 已提交
149 150 151


## DeviceType
Z
zengyawen 已提交
152 153 154

用于表示当前设备类型。

H
HelloCrease 已提交
155
**系统能力**:SystemCapability.Global.ResourceManager
H
HelloCrease 已提交
156 157 158 159 160 161 162 163 164

| 名称                   | 默认值  | 说明   |
| -------------------- | ---- | ---- |
| DEVICE_TYPE_PHONE    | 0x00 | 手机   |
| DEVICE_TYPE_TABLET   | 0x01 | 平板   |
| DEVICE_TYPE_CAR      | 0x02 | 汽车   |
| DEVICE_TYPE_PC       | 0x03 | 电脑   |
| DEVICE_TYPE_TV       | 0x04 | 电视   |
| DEVICE_TYPE_WEARABLE | 0x06 | 穿戴   |
Z
zengyawen 已提交
165 166 167


## ScreenDensity
Z
zengyawen 已提交
168 169 170

用于表示当前设备屏幕密度。

H
HelloCrease 已提交
171
**系统能力**:SystemCapability.Global.ResourceManager
H
HelloCrease 已提交
172 173 174 175 176 177 178 179 180

| 名称             | 默认值  | 说明         |
| -------------- | ---- | ---------- |
| SCREEN_SDPI    | 120  | 小规模的屏幕密度   |
| SCREEN_MDPI    | 160  | 中规模的屏幕密度   |
| SCREEN_LDPI    | 240  | 大规模的屏幕密度   |
| SCREEN_XLDPI   | 320  | 特大规模的屏幕密度  |
| SCREEN_XXLDPI  | 480  | 超大规模的屏幕密度  |
| SCREEN_XXXLDPI | 640  | 超特大规模的屏幕密度 |
Z
zengyawen 已提交
181 182 183


## Configuration
Z
zengyawen 已提交
184 185 186

表示当前设备的状态。

H
HelloCrease 已提交
187
**系统能力**:SystemCapability.Global.ResourceManager
H
HelloCrease 已提交
188

Z
zengyawen 已提交
189

H
HelloCrease 已提交
190 191 192 193
| 名称        | 参数类型                    | 可读   | 可写   | 说明       |
| --------- | ----------------------- | ---- | ---- | -------- |
| direction | [Direction](#direction) | 是    | 否    | 当前设备屏幕方向 |
| locale    | string                  | 是    | 否    | 当前系统语言   |
Z
zengyawen 已提交
194

195 196
**示例:**

H
HelloCrease 已提交
197
  ```js
198 199
resourceManager.getResourceManager((error, mgr) => {
      mgr.getConfiguration((error, value) => {
W
wplan1 已提交
200 201
          let direction = value.direction;
          let locale = value.locale;
202 203 204
      });
  });
  ```
Z
zengyawen 已提交
205 206

## DeviceCapability
Z
zengyawen 已提交
207 208 209

表示设备支持的能力。

H
HelloCrease 已提交
210
**系统能力**:SystemCapability.Global.ResourceManager
H
HelloCrease 已提交
211

Z
zengyawen 已提交
212

H
HelloCrease 已提交
213 214 215 216
| 名称            | 参数类型                            | 可读   | 可写   | 说明       |
| ------------- | ------------------------------- | ---- | ---- | -------- |
| screenDensity | [ScreenDensity](#screendensity) | 是    | 否    | 当前设备屏幕密度 |
| deviceType    | [DeviceType](#devicetype)       | 是    | 否    | 当前设备类型   |
Z
zengyawen 已提交
217

218 219
**示例:**

H
HelloCrease 已提交
220
  ```js
221 222
resourceManager.getResourceManager((error, mgr) => {
      mgr.getDeviceCapability((error, value) => {
W
wplan1 已提交
223 224
          let screenDensity = value.screenDensity;
          let deviceType = value.deviceType;
225 226 227
      });
  });
  ```
Z
zengyawen 已提交
228

W
wplan1 已提交
229 230
## RawFileDescriptor<sup>8+</sup>

H
HelloCrease 已提交
231 232 233
表示rawfile的descriptor信息。

**系统能力:** SystemCapability.Global.ResourceManager
W
wplan1 已提交
234

H
HelloCrease 已提交
235 236 237 238 239
| 名称     | 类型     | 说明                 |
| ------ | ------ | ------------------ |
| fd     | number | rawfile的descriptor |
| offset | number | rawfile的起始偏移量      |
| length | number | rawfile的文件长度       |
W
wplan1 已提交
240

241
## Resource<sup>9+</sup>
242 243 244 245 246

表示的资源信息。

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
247 248
| 名称         | 类型     | 说明          |
| ---------- | ------ | ----------- |
249
| bundleName | string | 应用的bundle名称 |
H
HelloCrease 已提交
250 251
| moduleName | string | 应用的module名称 |
| id         | number | 资源的id值      |
252

W
wplan1 已提交
253

Z
zengyawen 已提交
254
## ResourceManager
Z
zengyawen 已提交
255 256 257

提供访问应用资源的能力。

258 259
> **说明:**
>
Z
zengyawen 已提交
260
> - ResourceManager涉及到的方法,仅限基于TS扩展的声明式开发范式使用。
H
HelloCrease 已提交
261
>
Z
zengyawen 已提交
262 263
> - 资源文件在工程的resources目录中定义,id可通过$r(资源地址).id的方式获取,例如$r('app.string.test').id。

H
huangjie 已提交
264
### getStringValue<sup>9+</sup>
Z
zengyawen 已提交
265

H
huangjie 已提交
266
getStringValue(resId: number, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
267 268 269

用户获取指定资源ID对应的字符串,使用callback形式返回字符串。

V
VictoriaGuo 已提交
270 271
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
272 273 274 275 276
**参数:** 
| 参数名      | 类型                          | 必填   | 说明              |
| -------- | --------------------------- | ---- | --------------- |
| resId    | number                      | 是    | 资源ID值           |
| callback | AsyncCallback&lt;string&gt; | 是    | 异步回调,用于返回获取的字符串 |
Z
zengyawen 已提交
277

H
huangjie 已提交
278 279 280 281 282 283 284 285 286 287 288
**错误码:**

以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

**示例Stage:** 
H
HelloCrease 已提交
289
  ```ts
H
huangjie 已提交
290 291
    try {
        this.context.getStringValue($r('app.string.test').id, (error, value) => {
Z
zengyawen 已提交
292
          if (error != null) {
W
wplan1 已提交
293
              console.log("error is " + error);
Z
zengyawen 已提交
294
          } else {
W
wplan1 已提交
295
              let str = value;
Z
zengyawen 已提交
296 297
          }
      });
H
huangjie 已提交
298 299 300
    } catch (error) {
        console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`)
    }
Z
zengyawen 已提交
301 302 303
  ```


H
huangjie 已提交
304
### getStringValue<sup>9+</sup>
Z
zengyawen 已提交
305

H
huangjie 已提交
306
getStringValue(resId: number): Promise&lt;string&gt;
Z
zengyawen 已提交
307 308 309

用户获取指定资源ID对应的字符串,使用Promise形式返回字符串。

V
VictoriaGuo 已提交
310 311
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
312 313 314 315
**参数:** 
| 参数名   | 类型     | 必填   | 说明    |
| ----- | ------ | ---- | ----- |
| resId | number | 是    | 资源ID值 |
Z
zengyawen 已提交
316

H
HelloCrease 已提交
317 318 319 320
**返回值:** 
| 类型                    | 说明          |
| --------------------- | ----------- |
| Promise&lt;string&gt; | 资源ID值对应的字符串 |
Z
zengyawen 已提交
321

H
huangjie 已提交
322 323 324 325 326 327 328 329
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

H
HelloCrease 已提交
330
**示例:** 
H
HelloCrease 已提交
331
  ```ts
H
huangjie 已提交
332 333 334 335 336 337 338 339 340
  try {
    this.context.resourceManager.getStringValue($r('app.string.test').id).then(value => {
        let str = value;
    }).catch(error => {
        console.log("getStringValue promise error is " + error);
    });
  } catch (error) {
    console.error(`promise getStringValue failed, error code: ${error.code}, message: ${error.message}.`)
  }
Z
zengyawen 已提交
341 342 343
  ```


H
huangjie 已提交
344
### getStringValue<sup>9+</sup>
345

H
huangjie 已提交
346
getStringValue(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
347 348 349 350 351 352 353 354

用户获取指定resource对象对应的字符串,使用callback形式返回字符串。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名      | 类型                          | 必填   | 说明              |
| -------- | --------------------------- | ---- | --------------- |
H
HelloCrease 已提交
355
| resource | [Resource](#resource9)      | 是    | 资源信息            |
356 357
| callback | AsyncCallback&lt;string&gt; | 是    | 异步回调,用于返回获取的字符串 |

H
huangjie 已提交
358 359 360 361 362 363 364 365
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

366
**示例:** 
H
HelloCrease 已提交
367
  ```ts
368 369 370 371 372
  let resource = {
      bundleName: "com.example.myapplication",
      moduleName: "entry",
      id: $r('app.string.test').id
  };
H
huangjie 已提交
373 374 375 376 377 378 379 380 381 382 383 384
  tyr {
    this.context.resourceManager.getStringValue(resource, (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let str = value;
        }
    });
  } catch (error) {
    console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`)
  }
  
385 386 387
  ```


H
huangjie 已提交
388 389 390
### getStringValue<sup>9+</sup>

getStringValue(resource: Resource): Promise&lt;string&gt;
391 392 393 394 395 396

用户获取指定resource对象对应的字符串,使用Promise形式返回字符串。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
397 398
| 参数名      | 类型                     | 必填   | 说明   |
| -------- | ---------------------- | ---- | ---- |
399
| resource | [Resource](#resource9) | 是    | 资源信息 |
400 401

**返回值:** 
H
HelloCrease 已提交
402 403
| 类型                    | 说明               |
| --------------------- | ---------------- |
404 405
| Promise&lt;string&gt; | resource对象对应的字符串 |

H
huangjie 已提交
406 407 408 409 410 411 412 413
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

414
**示例:** 
H
HelloCrease 已提交
415
  ```ts
416 417 418 419 420
  let resource = {
      bundleName: "com.example.myapplication",
      moduleName: "entry",
      id: $r('app.string.test').id
  };
H
huangjie 已提交
421 422
  try {
    this.context.resourceManager.getStringValue(resource).then(value => {
423
      let str = value;
H
huangjie 已提交
424 425 426 427 428 429
    }).catch(error => {
      console.log("getStringValue promise error is " + error);
    });
  } catch (error) {
    console.error(`callback getStringValue failed, error code: ${error.code}, message: ${error.message}.`)
  }
430 431
  ```

Z
zengyawen 已提交
432

H
huangjie 已提交
433 434 435
### getStringArrayValue<sup>9+</sup>

getStringArrayValue(resId: number, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
Z
zengyawen 已提交
436 437 438

用户获取指定资源ID对应的字符串数组,使用callback形式返回字符串数组。

V
VictoriaGuo 已提交
439 440
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
441 442 443 444 445
**参数:** 
| 参数名      | 类型                                       | 必填   | 说明                |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resId    | number                                   | 是    | 资源ID值             |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是    | 异步回调,用于返回获取的字符串数组 |
Z
zengyawen 已提交
446

H
huangjie 已提交
447 448 449 450 451 452 453 454
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

H
HelloCrease 已提交
455
**示例:** 
H
HelloCrease 已提交
456
  ```ts
H
huangjie 已提交
457 458 459 460 461 462 463 464 465 466 467
  try {
    this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id, (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let strArray = value;
        }
    });
  } catch (error) {
    console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`)
  }
Z
zengyawen 已提交
468 469 470
  ```


H
huangjie 已提交
471
### getStringArrayValue<sup>9+</sup>
Z
zengyawen 已提交
472

H
huangjie 已提交
473
getStringArrayValue(resId: number): Promise&lt;Array&lt;string&gt;&gt;
Z
zengyawen 已提交
474 475 476

用户获取指定资源ID对应的字符串数组,使用Promise形式返回字符串数组。

V
VictoriaGuo 已提交
477 478
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
479 480 481 482
**参数:** 
| 参数名   | 类型     | 必填   | 说明    |
| ----- | ------ | ---- | ----- |
| resId | number | 是    | 资源ID值 |
Z
zengyawen 已提交
483

H
HelloCrease 已提交
484 485 486 487
**返回值:** 
| 类型                                 | 说明            |
| ---------------------------------- | ------------- |
| Promise&lt;Array&lt;string&gt;&gt; | 资源ID值对应的字符串数组 |
Z
zengyawen 已提交
488

H
huangjie 已提交
489 490 491 492 493 494 495 496
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

H
HelloCrease 已提交
497
**示例:** 
H
HelloCrease 已提交
498
  ```ts
H
huangjie 已提交
499 500 501 502 503 504 505 506 507
  try {
    this.context.resourceManager.getStringArrayValue($r('app.strarray.test').id).then(value => {
        let strArray = value;
    }).catch(error => {
        console.log("getStringArrayValue promise error is " + error);
    });
  } catch (error) {
    console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`)
  }
Z
zengyawen 已提交
508 509
  ```

H
huangjie 已提交
510
### getStringArrayValue<sup>9+</sup>
511

H
huangjie 已提交
512
getStringArrayValue(resource: Resource, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void
513 514 515 516 517 518

用户获取指定resource对象对应的字符串数组,使用callback形式返回回字符串数组。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
519 520 521
| 参数名      | 类型                                       | 必填   | 说明                |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resource | [Resource](#resource9)                   | 是    | 资源信息              |
522 523
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是    | 异步回调,用于返回获取的字符串数组 |

H
huangjie 已提交
524 525 526 527 528 529 530 531
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

532
**示例:** 
H
HelloCrease 已提交
533
  ```ts
534 535 536 537 538
  let resource = {
      bundleName: "com.example.myapplication",
      moduleName: "entry",
      id: $r('app.strarray.test').id
  };
H
huangjie 已提交
539 540
  try {
    this.context.resourceManager.getStringArrayValue(resource, (error, value) => {
541 542 543 544 545
      if (error != null) {
          console.log("error is " + error);
      } else {
          let strArray = value;
      }
H
huangjie 已提交
546 547 548 549
    });
  } catch (error) {
    console.error(`callback getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`)
  }
550 551
  ```

H
huangjie 已提交
552
### getStringArrayValue<sup>9+</sup>
553

H
huangjie 已提交
554
getStringArrayValue(resource: Resource): Promise&lt;Array&lt;string&gt;&gt;
555 556 557 558 559 560

用户获取指定resource对象对应的字符串数组,使用Promise形式返回字符串数组。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
561 562
| 参数名      | 类型                     | 必填   | 说明   |
| -------- | ---------------------- | ---- | ---- |
563
| resource | [Resource](#resource9) | 是    | 资源信息 |
564 565

**返回值:** 
H
HelloCrease 已提交
566 567
| 类型                                 | 说明                 |
| ---------------------------------- | ------------------ |
568 569
| Promise&lt;Array&lt;string&gt;&gt; | resource对象对应的字符串数组 |

H
huangjie 已提交
570 571 572 573 574 575 576 577
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

578
**示例:** 
H
HelloCrease 已提交
579
  ```ts
580 581 582 583 584
  let resource = {
      bundleName: "com.example.myapplication",
      moduleName: "entry",
      id: $r('app.strarray.test').id
  };
H
huangjie 已提交
585 586
  try {
    this.context.resourceManager.getStringArrayValue(resource).then(value => {
587
      let strArray = value;
H
huangjie 已提交
588 589 590 591 592 593
    }).catch(error => {
        console.log("getStringArray promise error is " + error);
    });
  } catch (error) {
    console.error(`promise getStringArrayValue failed, error code: ${error.code}, message: ${error.message}.`)
  }
594
  ```
Z
zengyawen 已提交
595 596


H
huangjie 已提交
597 598 599
### getMediaContent

getMediaContent(resId: number, callback: AsyncCallback&lt;Uint8Array&gt;): void
Z
zengyawen 已提交
600 601 602

用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组。

V
VictoriaGuo 已提交
603 604
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
605 606 607 608 609
**参数:** 
| 参数名      | 类型                              | 必填   | 说明                 |
| -------- | ------------------------------- | ---- | ------------------ |
| resId    | number                          | 是    | 资源ID值              |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是    | 异步回调,用于返回获取的媒体文件内容 |
Z
zengyawen 已提交
610

H
huangjie 已提交
611 612 613 614 615 616 617
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |

H
HelloCrease 已提交
618
**示例:** 
H
HelloCrease 已提交
619
  ```ts
H
huangjie 已提交
620 621 622 623 624 625 626 627 628 629 630
  try {
    this.context.resourceManager.getMediaContent($r('app.media.test').id, (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let media = value;
        }
    });
  } catch (error) {
    console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`)
  }
Z
zengyawen 已提交
631 632 633
  ```


H
huangjie 已提交
634
### getMediaContent
Z
zengyawen 已提交
635

H
huangjie 已提交
636
getMediaContent(resId: number): Promise&lt;Uint8Array&gt;
Z
zengyawen 已提交
637 638 639

用户获取指定资源ID对应的媒体文件内容,使用Promise形式返回字节数组。

V
VictoriaGuo 已提交
640 641
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
642 643 644 645
**参数:** 
| 参数名   | 类型     | 必填   | 说明    |
| ----- | ------ | ---- | ----- |
| resId | number | 是    | 资源ID值 |
Z
zengyawen 已提交
646

H
HelloCrease 已提交
647 648 649 650
**返回值:** 
| 类型                        | 说明             |
| ------------------------- | -------------- |
| Promise&lt;Uint8Array&gt; | 资源ID值对应的媒体文件内容 |
Z
zengyawen 已提交
651

H
huangjie 已提交
652 653 654 655 656 657 658
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |

H
HelloCrease 已提交
659
**示例:** 
H
HelloCrease 已提交
660
  ```ts
H
huangjie 已提交
661 662
  try {
      mgr.getMediaContent($r('app.media.test').id).then(value => {
W
wplan1 已提交
663
          let media = value;
Z
zengyawen 已提交
664
      }).catch(error => {
H
huangjie 已提交
665
          console.log("getMediaContent promise error is " + error);
Z
zengyawen 已提交
666
      });
H
huangjie 已提交
667 668 669
  } catch (error) {
    console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`)
  }
Z
zengyawen 已提交
670 671
  ```

H
huangjie 已提交
672
### getMediaContent<sup>9+</sup>
673

H
huangjie 已提交
674
getMediaContent(resource: Resource, callback: AsyncCallback&lt;Uint8Array&gt;): void
675 676 677 678 679 680

用户获取指定resource对象对应的媒体文件内容,使用callback形式返回字节数组。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
681 682 683
| 参数名      | 类型                              | 必填   | 说明                 |
| -------- | ------------------------------- | ---- | ------------------ |
| resource | [Resource](#resource9)          | 是    | 资源信息               |
684 685
| callback | AsyncCallback&lt;Uint8Array&gt; | 是    | 异步回调,用于返回获取的媒体文件内容 |

H
huangjie 已提交
686 687 688 689 690 691 692
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |

693
**示例:** 
H
HelloCrease 已提交
694
  ```ts
695 696 697 698 699
  let resource = {
      bundleName: "com.example.myapplication",
      moduleName: "entry",
      id: $r('app.media.test').id
  };
H
huangjie 已提交
700 701 702
  try {
    this.context.resourceManager.getMediaContent(resource, (error, value) => {
        if (error != null) {
703
          console.log("error is " + error);
H
huangjie 已提交
704
        } else {
705
          let media = value;
H
huangjie 已提交
706 707 708 709 710
        }
    });
  } catch (error) {
    console.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`)
  }
711 712
  ```

H
huangjie 已提交
713
### getMediaContent<sup>9+</sup>
714

H
huangjie 已提交
715
getMediaContent(resource: Resource): Promise&lt;Uint8Array&gt;
716 717 718 719 720 721

用户获取指定resource对象对应的媒体文件内容,使用Promise形式返回字节数组。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
722 723
| 参数名      | 类型                     | 必填   | 说明   |
| -------- | ---------------------- | ---- | ---- |
724
| resource | [Resource](#resource9) | 是    | 资源信息 |
725 726

**返回值:** 
H
HelloCrease 已提交
727 728
| 类型                        | 说明                  |
| ------------------------- | ------------------- |
729 730
| Promise&lt;Uint8Array&gt; | resource对象对应的媒体文件内容 |

H
huangjie 已提交
731 732 733 734 735 736 737
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |

738
**示例:** 
H
HelloCrease 已提交
739
  ```ts
740 741 742 743 744
  let resource = {
      bundleName: "com.example.myapplication",
      moduleName: "entry",
      id: $r('app.media.test').id
  };
H
huangjie 已提交
745 746
  try {
    this.context.resourceManager.getMediaContent(resource).then(value => {
747
      let media = value;
H
huangjie 已提交
748 749 750 751 752 753
    }).catch(error => {
      console.log("getMediaContent promise error is " + error);
    });
  } catch (error) {
    console.error(`promise getMediaContent failed, error code: ${error.code}, message: ${error.message}.`)
  }
754
  ```
Z
zengyawen 已提交
755 756


H
huangjie 已提交
757 758 759
### getMediaContentBase64

getMediaContentBase64(resId: number, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
760 761 762

用户获取指定资源ID对应的图片资源Base64编码,使用callback形式返回字符串。

V
VictoriaGuo 已提交
763 764
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
765 766 767 768 769
**参数:** 
| 参数名      | 类型                          | 必填   | 说明                       |
| -------- | --------------------------- | ---- | ------------------------ |
| resId    | number                      | 是    | 资源ID值                    |
| callback | AsyncCallback&lt;string&gt; | 是    | 异步回调,用于返回获取的图片资源Base64编码 |
Z
zengyawen 已提交
770

H
huangjie 已提交
771 772 773 774 775 776 777
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |

H
HelloCrease 已提交
778
**示例:** 
H
HelloCrease 已提交
779
  ```ts
H
huangjie 已提交
780 781 782 783 784 785 786 787 788 789 790
  try {
    mgr.getMediaContentBase64($r('app.media.test').id, (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let media = value;
        }
    });       
  } catch (error) {
    console.error(`callback getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`)
  }
Z
zengyawen 已提交
791 792 793
  ```


H
huangjie 已提交
794
### getMediaContentBase64
Z
zengyawen 已提交
795

H
huangjie 已提交
796
getMediaContentBase64(resId: number): Promise&lt;string&gt;
Z
zengyawen 已提交
797 798 799

用户获取指定资源ID对应的图片资源Base64编码,使用Promise形式返回字符串。

V
VictoriaGuo 已提交
800 801
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
802 803 804 805
**参数:** 
| 参数名   | 类型     | 必填   | 说明    |
| ----- | ------ | ---- | ----- |
| resId | number | 是    | 资源ID值 |
Z
zengyawen 已提交
806

H
HelloCrease 已提交
807 808 809 810
**返回值:** 
| 类型                    | 说明                   |
| --------------------- | -------------------- |
| Promise&lt;string&gt; | 资源ID值对应的图片资源Base64编码 |
Z
zengyawen 已提交
811

H
huangjie 已提交
812 813 814 815 816 817 818
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |

H
HelloCrease 已提交
819
**示例:** 
H
HelloCrease 已提交
820
  ```ts
H
huangjie 已提交
821 822 823 824 825 826 827 828 829
  try {
    mgr.getMediaContentBase64($r('app.media.test').id).then(value => {
        let media = value;
    }).catch(error => {
        console.log("getMediaContentBase64 promise error is " + error);
    });
  } catch (error) {
    console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`)
  } 
Z
zengyawen 已提交
830 831
  ```

H
huangjie 已提交
832
### getMediaContentBase64<sup>9+</sup>
833

H
huangjie 已提交
834
getMediaContentBase64(resource: Resource, callback: AsyncCallback&lt;string&gt;): void
835 836 837 838 839 840 841 842

用户获取指定resource对象对应的图片资源Base64编码,使用callback形式返回字符串。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名      | 类型                          | 必填   | 说明                       |
| -------- | --------------------------- | ---- | ------------------------ |
H
HelloCrease 已提交
843
| resource | [Resource](#resource9)      | 是    | 资源信息                     |
844 845
| callback | AsyncCallback&lt;string&gt; | 是    | 异步回调,用于返回获取的图片资源Base64编码 |

H
huangjie 已提交
846 847 848 849 850 851 852
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |

853
**示例:** 
H
HelloCrease 已提交
854
  ```ts
855 856 857 858 859
  let resource = {
      bundleName: "com.example.myapplication",
      moduleName: "entry",
      id: $r('app.media.test').id
  };
H
huangjie 已提交
860 861 862 863 864 865 866 867 868 869 870
  try {
    this.context.resourceManager.getMediaContentBase64(resource, (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let media = value;
        }
    });
  } catch (error) {
    console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`)
  }
871 872
  ```

H
huangjie 已提交
873
### getMediaContentBase64<sup>9+</sup>
874

H
huangjie 已提交
875
getMediaContentBase64(resource: Resource): Promise&lt;string&gt;
876 877 878 879 880 881

用户获取指定resource对象对应的图片资源Base64编码,使用Promise形式返回字符串。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
882 883
| 参数名      | 类型                     | 必填   | 说明   |
| -------- | ---------------------- | ---- | ---- |
884
| resource | [Resource](#resource9) | 是    | 资源信息 |
885 886

**返回值:** 
H
HelloCrease 已提交
887 888
| 类型                    | 说明                        |
| --------------------- | ------------------------- |
889 890
| Promise&lt;string&gt; | resource对象对应的图片资源Base64编码 |

H
huangjie 已提交
891 892 893 894 895 896 897
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |

898
**示例:** 
H
HelloCrease 已提交
899
  ```ts
900 901 902 903 904
  let resource = {
      bundleName: "com.example.myapplication",
      moduleName: "entry",
      id: $r('app.media.test').id
  };
H
huangjie 已提交
905 906 907 908 909 910 911 912 913
  try {
    this.context.resourceManager.getMediaContentBase64(resource).then(value => {
        let media = value;
    }).catch(error => {
        console.log("getMediaContentBase64 promise error is " + error);
    });
  } catch (error) {
    console.error(`promise getMediaContentBase64 failed, error code: ${error.code}, message: ${error.message}.`)
  }
914 915
  ```

Z
zengyawen 已提交
916 917 918 919

### getConfiguration

getConfiguration(callback: AsyncCallback&lt;Configuration&gt;): void
Z
zengyawen 已提交
920 921 922

用户获取设备的Configuration,使用callback形式返回Configuration对象。

V
VictoriaGuo 已提交
923 924
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
925 926 927 928
**参数:** 
| 参数名      | 类型                                       | 必填   | 说明                        |
| -------- | ---------------------------------------- | ---- | ------------------------- |
| callback | AsyncCallback&lt;[Configuration](#configuration)&gt; | 是    | 异步回调,用于返回设备的Configuration |
Z
zengyawen 已提交
929

H
HelloCrease 已提交
930
**示例:** 
H
HelloCrease 已提交
931
  ```ts
Z
zengyawen 已提交
932 933 934
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getConfiguration((error, value) => {
          if (error != null) {
W
wplan1 已提交
935
              console.log("error is " + error);
Z
zengyawen 已提交
936
          } else {
W
wplan1 已提交
937 938
              let direction = value.direction;
              let locale = value.locale;
Z
zengyawen 已提交
939 940 941 942
          }
      });
  });
  ```
Z
zengyawen 已提交
943 944


Z
zengyawen 已提交
945
### getConfiguration
Z
zengyawen 已提交
946

Z
zengyawen 已提交
947 948 949
getConfiguration(): Promise&lt;Configuration&gt;

用户获取设备的Configuration,使用Promise形式返回Configuration对象。
Z
zengyawen 已提交
950

V
VictoriaGuo 已提交
951 952
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
953 954 955 956
**返回值:** 
| 类型                                       | 说明               |
| ---------------------------------------- | ---------------- |
| Promise&lt;[Configuration](#configuration)&gt; | 设备的Configuration |
Z
zengyawen 已提交
957

H
HelloCrease 已提交
958
**示例:** 
H
HelloCrease 已提交
959
  ```ts
Z
zengyawen 已提交
960 961
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getConfiguration().then(value => {
W
wplan1 已提交
962 963
          let direction = value.direction;
          let locale = value.locale;
Z
zengyawen 已提交
964
      }).catch(error => {
W
wplan1 已提交
965
          console.log("getConfiguration promise error is " + error);
Z
zengyawen 已提交
966 967 968
      });
  });
  ```
Z
zengyawen 已提交
969 970


Z
zengyawen 已提交
971 972 973
### getDeviceCapability

getDeviceCapability(callback: AsyncCallback&lt;DeviceCapability&gt;): void
Z
zengyawen 已提交
974 975 976

用户获取设备的DeviceCapability,使用callback形式返回DeviceCapability对象。

V
VictoriaGuo 已提交
977 978
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
979 980 981 982
**参数:** 
| 参数名      | 类型                                       | 必填   | 说明                           |
| -------- | ---------------------------------------- | ---- | ---------------------------- |
| callback | AsyncCallback&lt;[DeviceCapability](#devicecapability)&gt; | 是    | 异步回调,用于返回设备的DeviceCapability |
Z
zengyawen 已提交
983

H
HelloCrease 已提交
984
**示例:** 
H
HelloCrease 已提交
985
  ```ts
Z
zengyawen 已提交
986 987 988
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getDeviceCapability((error, value) => {
          if (error != null) {
W
wplan1 已提交
989
              console.log("error is " + error);
Z
zengyawen 已提交
990
          } else {
W
wplan1 已提交
991 992
              let screenDensity = value.screenDensity;
              let deviceType = value.deviceType;
Z
zengyawen 已提交
993 994 995 996
          }
      });
  });
  ```
Z
zengyawen 已提交
997 998


Z
zengyawen 已提交
999
### getDeviceCapability
Z
zengyawen 已提交
1000

Z
zengyawen 已提交
1001 1002 1003
getDeviceCapability(): Promise&lt;DeviceCapability&gt;

用户获取设备的DeviceCapability,使用Promise形式返回DeviceCapability对象。
Z
zengyawen 已提交
1004

V
VictoriaGuo 已提交
1005 1006
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
1007 1008 1009 1010
**返回值:** 
| 类型                                       | 说明                  |
| ---------------------------------------- | ------------------- |
| Promise&lt;[DeviceCapability](#devicecapability)&gt; | 设备的DeviceCapability |
Z
zengyawen 已提交
1011

H
HelloCrease 已提交
1012
**示例:** 
H
HelloCrease 已提交
1013
  ```ts
Z
zengyawen 已提交
1014 1015
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getDeviceCapability().then(value => {
W
wplan1 已提交
1016 1017
          let screenDensity = value.screenDensity;
          let deviceType = value.deviceType;
Z
zengyawen 已提交
1018
      }).catch(error => {
W
wplan1 已提交
1019
          console.log("getDeviceCapability promise error is " + error);
Z
zengyawen 已提交
1020 1021 1022
      });
  });
  ```
Z
zengyawen 已提交
1023 1024


H
huangjie 已提交
1025
### getPluralStringValue
Z
zengyawen 已提交
1026

H
huangjie 已提交
1027
getPluralStringValue(resId: number, num: number, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
1028 1029 1030

根据指定数量获取指定ID字符串表示的单复数字符串,使用callback形式返回字符串。

V
VictoriaGuo 已提交
1031 1032
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
1033 1034 1035 1036 1037 1038
**参数:** 
| 参数名      | 类型                          | 必填   | 说明                              |
| -------- | --------------------------- | ---- | ------------------------------- |
| resId    | number                      | 是    | 资源ID值                           |
| num      | number                      | 是    | 数量值                             |
| callback | AsyncCallback&lt;string&gt; | 是    | 异步回调,返回根据指定数量获取指定ID字符串表示的单复数字符串 |
Z
zengyawen 已提交
1039

H
huangjie 已提交
1040 1041 1042 1043 1044 1045 1046 1047
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

H
HelloCrease 已提交
1048
**示例:** 
H
HelloCrease 已提交
1049
  ```ts
H
huangjie 已提交
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
  try {
    this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1, (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let str = value;
        }
    });
  } catch (error) {
    console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`)
  }   
Z
zengyawen 已提交
1061
  ```
Z
zengyawen 已提交
1062 1063


H
huangjie 已提交
1064
### getPluralStringValue
Z
zengyawen 已提交
1065

H
huangjie 已提交
1066
getPluralStringValue(resId: number, num: number): Promise&lt;string&gt;
Z
zengyawen 已提交
1067 1068

根据指定数量获取对指定ID字符串表示的单复数字符串,使用Promise形式返回字符串。
Z
zengyawen 已提交
1069

V
VictoriaGuo 已提交
1070 1071
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
1072 1073 1074 1075 1076
**参数:** 
| 参数名   | 类型     | 必填   | 说明    |
| ----- | ------ | ---- | ----- |
| resId | number | 是    | 资源ID值 |
| num   | number | 是    | 数量值   |
Z
zengyawen 已提交
1077

H
HelloCrease 已提交
1078 1079 1080 1081
**返回值:** 
| 类型                    | 说明                        |
| --------------------- | ------------------------- |
| Promise&lt;string&gt; | 根据提供的数量获取对应ID字符串表示的单复数字符串 |
Z
zengyawen 已提交
1082

H
huangjie 已提交
1083 1084 1085 1086 1087 1088 1089 1090
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

H
HelloCrease 已提交
1091
**示例:** 
H
HelloCrease 已提交
1092
  ```ts
H
huangjie 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101
  try {
    this.context.resourceManager.getPluralStringValue($r("app.plural.test").id, 1).then(value => {
        let str = value;
    }).catch(error => {
        console.log("getPluralStringValue promise error is " + error);
    });
  } catch (error) {
    console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`)
  }  
Z
zengyawen 已提交
1102
  ```
Z
zengyawen 已提交
1103

H
huangjie 已提交
1104
### getPluralStringValue<sup>9+</sup>
1105

H
huangjie 已提交
1106
getPluralStringValue(resource: Resource, num: number, callback: AsyncCallback&lt;string&gt;): void
1107 1108 1109 1110 1111 1112

根据指定数量获取指定resource对象表示的单复数字符串,使用callback形式返回字符串。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
1113 1114 1115 1116
| 参数名      | 类型                          | 必填   | 说明                                   |
| -------- | --------------------------- | ---- | ------------------------------------ |
| resource | [Resource](#resource9)      | 是    | 资源信息                                 |
| num      | number                      | 是    | 数量值                                  |
1117 1118
| callback | AsyncCallback&lt;string&gt; | 是    | 异步回调,返回根据指定数量获取指定resource对象表示的单复数字符串 |

H
huangjie 已提交
1119 1120 1121 1122 1123 1124 1125 1126
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

1127
**示例:** 
H
HelloCrease 已提交
1128
  ```ts
1129 1130 1131 1132 1133
  let resource = {
      bundleName: "com.example.myapplication",
      moduleName: "entry",
      id: $r('app.plural.test').id
  };
H
huangjie 已提交
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
  try {
    this.context.resourceManager.getPluralStringValue(resource, 1, (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let str = value;
        }
    });
  } catch (error) {
    console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`)
  }  
  
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
  ```

### getPluralString<sup>9+</sup>

getPluralString(resource: Resource, num: number): Promise&lt;string&gt;

根据指定数量获取对指定resource对象表示的单复数字符串,使用Promise形式返回字符串。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
1157 1158
| 参数名      | 类型                     | 必填   | 说明   |
| -------- | ---------------------- | ---- | ---- |
1159
| resource | [Resource](#resource9) | 是    | 资源信息 |
H
HelloCrease 已提交
1160
| num      | number                 | 是    | 数量值  |
1161 1162

**返回值:** 
H
HelloCrease 已提交
1163 1164
| 类型                    | 说明                             |
| --------------------- | ------------------------------ |
1165 1166
| Promise&lt;string&gt; | 根据提供的数量获取对应resource对象表示的单复数字符串 |

H
huangjie 已提交
1167 1168 1169 1170 1171 1172 1173 1174
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

1175
**示例:** 
H
HelloCrease 已提交
1176
  ```ts
1177 1178 1179 1180 1181
  let resource = {
      bundleName: "com.example.myapplication",
      moduleName: "entry",
      id: $r('app.plural.test').id
  };
H
huangjie 已提交
1182 1183 1184 1185 1186 1187 1188 1189 1190
  try {
    this.context.resourceManager.getPluralString(resource, 1).then(value => {
        let str = value;
    }).catch(error => {
        console.log("getPluralString promise error is " + error);
    });
  } catch (error) {
    console.error(`callback getPluralStringValue failed, error code: ${error.code}, message: ${error.message}.`)
  }
1191 1192
  ```

Z
zengyawen 已提交
1193

H
huangjie 已提交
1194 1195 1196
### getRawFileContent<sup>9+</sup>

getRawFileContent(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void
Z
zengyawen 已提交
1197

H
huangjie 已提交
1198
用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback形式返回字节数组。
Z
zengyawen 已提交
1199

V
VictoriaGuo 已提交
1200 1201
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
1202 1203 1204 1205 1206
**参数:** 
| 参数名      | 类型                              | 必填   | 说明                      |
| -------- | ------------------------------- | ---- | ----------------------- |
| path     | string                          | 是    | rawfile文件路径             |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是    | 异步回调,用于返回获取的rawfile文件内容 |
Z
zengyawen 已提交
1207

H
huangjie 已提交
1208 1209 1210 1211 1212 1213
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005  | The resource not found by path.          |

H
HelloCrease 已提交
1214
**示例:** 
H
HelloCrease 已提交
1215
  ```ts
H
huangjie 已提交
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
  try {
    this.context.resourceManager.getRawFileContent("test.xml", (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let rawFile = value;
        }
    });
  } catch (error) {
    console.error(`callback getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`)
  }
      
Z
zengyawen 已提交
1228 1229
  ```

H
huangjie 已提交
1230
### getRawFileContent<sup>9+</sup>
Z
zengyawen 已提交
1231

H
huangjie 已提交
1232
getRawFileContent(path: string): Promise&lt;Uint8Array&gt;
Z
zengyawen 已提交
1233

H
huangjie 已提交
1234
用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise形式返回字节数组。
Z
zengyawen 已提交
1235

V
VictoriaGuo 已提交
1236 1237
**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
1238 1239 1240 1241
**参数:** 
| 参数名  | 类型     | 必填   | 说明          |
| ---- | ------ | ---- | ----------- |
| path | string | 是    | rawfile文件路径 |
Z
zengyawen 已提交
1242

H
HelloCrease 已提交
1243 1244 1245 1246
**返回值:** 
| 类型                        | 说明          |
| ------------------------- | ----------- |
| Promise&lt;Uint8Array&gt; | rawfile文件内容 |
Z
zengyawen 已提交
1247

H
huangjie 已提交
1248 1249 1250 1251 1252 1253
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005  | The resource not found by path.          |

H
HelloCrease 已提交
1254
**示例:** 
H
HelloCrease 已提交
1255
  ```ts
H
huangjie 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264
  try {
    this.context.resourceManager.getRawFileContent("test.xml").then(value => {
        let rawFile = value;
    }).catch(error => {
        console.log("getRawFileContent promise error is " + error);
    });
  } catch (error) {
    console.error(`promise getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`)
  }
Z
zengyawen 已提交
1265
  ```
W
wplan1 已提交
1266 1267


H
huangjie 已提交
1268 1269 1270
### getRawFd<sup>9+</sup>

getRawFd(path: string, callback: AsyncCallback&lt;RawFileDescriptor&gt;): void
W
wplan1 已提交
1271

H
huangjie 已提交
1272
用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用callback形式返回。
W
wplan1 已提交
1273 1274 1275

**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
1276
**参数:** 
H
HelloCrease 已提交
1277 1278 1279
| 参数名      | 类型                                       | 必填   | 说明                               |
| -------- | ---------------------------------------- | ---- | -------------------------------- |
| path     | string                                   | 是    | rawfile文件路径                      |
H
huangjie 已提交
1280 1281 1282 1283 1284 1285 1286
| callback | AsyncCallback&lt;[getRawFd](#getrawfd9)&gt; | 是    | 异步回调,用于返回获取的rawfile文件的descriptor |

以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005  | The resource not found by path.          |
W
wplan1 已提交
1287

H
HelloCrease 已提交
1288
**示例:** 
H
HelloCrease 已提交
1289
  ```ts
H
huangjie 已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
  try {
    this.context.resourceManager.getRawFd("test.xml", (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let fd = value.fd;
            let offset = value.offset;
            let length = value.length;
        }
    });
  } catch(error => {
    console.log("getRawFd callback error is " + error);
W
wplan1 已提交
1302 1303 1304
  });
  ```

H
huangjie 已提交
1305
### getRawFd<sup>9+</sup>
W
wplan1 已提交
1306

H
huangjie 已提交
1307
getRawFd(path: string): Promise&lt;RawFileDescriptor&gt;
W
wplan1 已提交
1308

H
huangjie 已提交
1309
用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用Promise形式返回。
W
wplan1 已提交
1310 1311 1312

**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
1313 1314 1315 1316
**参数:** 
| 参数名  | 类型     | 必填   | 说明          |
| ---- | ------ | ---- | ----------- |
| path | string | 是    | rawfile文件路径 |
W
wplan1 已提交
1317

H
HelloCrease 已提交
1318 1319 1320
**返回值:** 
| 类型                                       | 说明                  |
| ---------------------------------------- | ------------------- |
H
huangjie 已提交
1321 1322 1323 1324 1325 1326 1327
| Promise&lt;[getRawFd](#getrawfd9-1)&gt; | rawfile文件descriptor |

以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005  | The resource not found by path.          |
W
wplan1 已提交
1328

H
HelloCrease 已提交
1329
**示例:** 
H
HelloCrease 已提交
1330
  ```ts
H
huangjie 已提交
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
  try {
    this.context.resourceManager.getRawFd("test.xml").then(value => {
        let fd = value.fd;
        let offset = value.offset;
        let length = value.length;
    }).catch(error => {
        console.log("getRawFd promise error is " + error);
    });
  } catch (error) {
    console.log("getRawFd promise error is " + error);
  };
W
wplan1 已提交
1342 1343 1344 1345 1346 1347
  ```

### closeRawFileDescriptor<sup>8+</sup>

closeRawFileDescriptor(path: string, callback: AsyncCallback&lt;void&gt;): void

H
huangjie 已提交
1348
用户关闭resources/rawfile目录下rawfile文件的descriptor,使用callback形式返回。
W
wplan1 已提交
1349 1350 1351

**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
1352 1353 1354 1355 1356
**参数:** 
| 参数名      | 类型                        | 必填   | 说明          |
| -------- | ------------------------- | ---- | ----------- |
| path     | string                    | 是    | rawfile文件路径 |
| callback | AsyncCallback&lt;void&gt; | 是    | 异步回调        |
W
wplan1 已提交
1357

H
HelloCrease 已提交
1358
**示例:** 
H
HelloCrease 已提交
1359
  ```ts
W
wplan1 已提交
1360 1361 1362
  resourceManager.getResourceManager((error, mgr) => {
      mgr.closeRawFileDescriptor("test.xml", (error, value) => {
          if (error != null) {
W
wplan1 已提交
1363
              console.log("error is " + error);
W
wplan1 已提交
1364 1365 1366 1367 1368 1369 1370 1371 1372
          }
      });
  });
  ```

### closeRawFileDescriptor<sup>8+</sup>

closeRawFileDescriptor(path: string): Promise&lt;void&gt;

H
huangjie 已提交
1373
用户关闭resources/rawfile目录下rawfile文件的descriptor,使用Promise形式返回。
W
wplan1 已提交
1374 1375 1376

**系统能力**:SystemCapability.Global.ResourceManager

H
HelloCrease 已提交
1377 1378 1379 1380
**参数:** 
| 参数名  | 类型     | 必填   | 说明          |
| ---- | ------ | ---- | ----------- |
| path | string | 是    | rawfile文件路径 |
W
wplan1 已提交
1381

H
HelloCrease 已提交
1382 1383 1384 1385
**返回值:** 
| 类型                  | 说明   |
| ------------------- | ---- |
| Promise&lt;void&gt; | 无返回值 |
W
wplan1 已提交
1386

H
HelloCrease 已提交
1387
**示例:** 
H
HelloCrease 已提交
1388
  ```ts
W
wplan1 已提交
1389 1390
  resourceManager.getResourceManager((error, mgr) => {
      mgr.closeRawFileDescriptor("test.xml").then(value => {
W
wplan1 已提交
1391
          let result = value;
W
wplan1 已提交
1392
      }).catch(error => {
W
wplan1 已提交
1393
          console.log("closeRawFileDescriptor promise error is " + error);
W
wplan1 已提交
1394 1395 1396 1397 1398
      });
  });
  ```


H
huangjie 已提交
1399
### closeRawFd<sup>9+</sup>
W
wplan1 已提交
1400

H
huangjie 已提交
1401
closeRawFd(path: string, callback: AsyncCallback&lt;void&gt;): void
H
huangjie 已提交
1402

H
huangjie 已提交
1403
用户关闭resources/rawfile目录下rawfile文件的descriptor,使用callback形式返回。
H
huangjie 已提交
1404

H
huangjie 已提交
1405
**系统能力**:SystemCapability.Global.ResourceManager
H
huangjie 已提交
1406

H
huangjie 已提交
1407 1408 1409 1410 1411
**参数:** 
| 参数名      | 类型                        | 必填   | 说明          |
| -------- | ------------------------- | ---- | ----------- |
| path     | string                    | 是    | rawfile文件路径 |
| callback | AsyncCallback&lt;void&gt; | 是    | 异步回调        |
H
huangjie 已提交
1412

H
huangjie 已提交
1413
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)
H
huangjie 已提交
1414

H
huangjie 已提交
1415 1416 1417
| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005  | The resource not found by path.          |
H
huangjie 已提交
1418

H
huangjie 已提交
1419
**示例:** 
H
HelloCrease 已提交
1420
  ```ts
H
huangjie 已提交
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
  try {
    mgr.closeRawFd("test.xml", (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        }
    });
  } catch (error) {
    console.error(`callback closeRawFd failed, error code: ${error.code}, message: ${error.message}.`)
  }
      
H
huangjie 已提交
1431 1432
  ```

H
huangjie 已提交
1433
### closeRawFd<sup>8+</sup>
H
huangjie 已提交
1434

H
huangjie 已提交
1435
closeRawFd(path: string): Promise&lt;void&gt;
H
huangjie 已提交
1436

H
huangjie 已提交
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
用户关闭resources/rawfile目录下rawfile文件的descriptor,使用Promise形式返回。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名  | 类型     | 必填   | 说明          |
| ---- | ------ | ---- | ----------- |
| path | string | 是    | rawfile文件路径 |

**返回值:** 
| 类型                  | 说明   |
| ------------------- | ---- |
| Promise&lt;void&gt; | 无返回值 |

以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001005  | The resource not found by path.          |

**示例:** 
  ```ts
  try {
    mgr.closeRawFd("test.xml").then(value => {
        let result = value;
    }).catch(error => {
        console.log("closeRawFd promise error is " + error);
    });
  } catch (error) {
    console.error(`promise closeRawFd failed, error code: ${error.code}, message: ${error.message}.`)
  }
  ```

### release<sup>7+</sup>

release()

用户释放创建的resourceManager。

**系统能力**:SystemCapability.Global.ResourceManager

**示例:** 
  ```ts
  resourceManager.getResourceManager((error, mgr) => {
      mgr.release();
  });
  ```

### getStringByName<sup>9+</sup>

getStringByName(resName: string, callback: AsyncCallback&lt;string&gt;): void

用户获取指定资源名称对应的字符串,使用callback形式返回字符串。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:**
| 参数名      | 类型                          | 必填   | 说明              |
| -------- | --------------------------- | ---- | --------------- |
| resName  | string                      | 是    | 资源名称            |
| callback | AsyncCallback&lt;string&gt; | 是    | 异步回调,用于返回获取的字符串 |

以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003  | The resName invalid.                     |
| 9001004  | The resource not found by resName.       |
| 9001006  | The resource re-ref too much.            |

**示例:**
  ```ts
  try {
    this.context.resourceManager.getStringByName("test", (error, value) => {
        if (error != null) {
             console.log("error is " + error);
        } else {
            let string = value;
        }
    });
  } catch (error) {
    console.error(`callback getStringByName failed, error code: ${error.code}, message: ${error.message}.`)
  }
  
  ```

### getStringByName<sup>9+</sup>

getStringByName(resName: string): Promise&lt;string&gt;

用户获取指定资源名称对应的字符串,使用Promise形式返回字符串。
H
huangjie 已提交
1528 1529 1530 1531

**系统能力**:SystemCapability.Global.ResourceManager

**参数:**
H
HelloCrease 已提交
1532
| 参数名     | 类型     | 必填   | 说明   |
H
huangjie 已提交
1533
| ------- | ------ | ---- | ---- |
H
HelloCrease 已提交
1534
| resName | string | 是    | 资源名称 |
H
huangjie 已提交
1535 1536

**返回值:**
H
HelloCrease 已提交
1537 1538
| 类型                    | 说明         |
| --------------------- | ---------- |
H
huangjie 已提交
1539 1540
| Promise&lt;string&gt; | 资源名称对应的字符串 |

H
huangjie 已提交
1541 1542 1543 1544 1545 1546 1547 1548
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003  | The resName invalid.                     |
| 9001004  | The resource not found by resName.       |
| 9001006  | The resource re-ref too much.            |

H
huangjie 已提交
1549
**示例:**
H
HelloCrease 已提交
1550
  ```ts
H
huangjie 已提交
1551 1552 1553 1554 1555 1556 1557 1558 1559
  try {
    this.context.resourceManager.getStringByName("test").then(value => {
        let string = value;
    }).catch(error => {
        console.log("getStringByName promise error is " + error);
    });
  } catch (error) {
    console.error(`promise getStringByName failed, error code: ${error.code}, message: ${error.message}.`)
  }
H
huangjie 已提交
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
  ```

### getStringArrayByName<sup>9+</sup>

getStringArrayByName(resName: string, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void

用户获取指定资源名称对应的字符串数组,使用callback形式返回字符串数组。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:**
| 参数名      | 类型                                       | 必填   | 说明                |
| -------- | ---------------------------------------- | ---- | ----------------- |
H
HelloCrease 已提交
1573
| resName  | string                                   | 是    | 资源名称              |
H
huangjie 已提交
1574 1575
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是    | 异步回调,用于返回获取的字符串数组 |

H
huangjie 已提交
1576 1577 1578 1579 1580 1581 1582 1583
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003  | The resName invalid.                     |
| 9001004  | The resource not found by resName.       |
| 9001006  | The resource re-ref too much.            |

H
huangjie 已提交
1584
**示例:** 
H
HelloCrease 已提交
1585
  ```ts
H
huangjie 已提交
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
  try {
    this.context.resourceManager.getStringArrayByName("test", (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let strArray = value;
        }
    });
  } catch (error) {
    console.error(`callback getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`)
  }
H
huangjie 已提交
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
  ```

### getStringArrayByName<sup>9+</sup>

getStringArrayByName(resName: string): Promise&lt;Array&lt;string&gt;&gt;

用户获取指定资源名称对应的字符串数组,使用Promise形式返回字符串数组。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
1608 1609 1610
| 参数名     | 类型     | 必填   | 说明   |
| ------- | ------ | ---- | ---- |
| resName | string | 是    | 资源名称 |
H
huangjie 已提交
1611 1612

**返回值:** 
H
HelloCrease 已提交
1613 1614
| 类型                                 | 说明           |
| ---------------------------------- | ------------ |
H
huangjie 已提交
1615 1616
| Promise&lt;Array&lt;string&gt;&gt; | 资源名称对应的字符串数组 |

H
huangjie 已提交
1617 1618 1619 1620 1621 1622 1623 1624
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003  | The resName invalid.                     |
| 9001004  | The resource not found by resName.       |
| 9001006  | The resource re-ref too much.            |

H
huangjie 已提交
1625
**示例:** 
H
HelloCrease 已提交
1626
  ```ts
H
huangjie 已提交
1627 1628 1629 1630 1631 1632 1633 1634 1635
  try {
    this.context.resourceManager.getStringArrayByName("test").then(value => {
        let strArray = value;
    }).catch(error => {
        console.log("getStringArrayByName promise error is " + error);
    });
  } catch (error) {
    console.error(`promise getStringArrayByName failed, error code: ${error.code}, message: ${error.message}.`)
  }
H
huangjie 已提交
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
  ```

### getMediaByName<sup>9+</sup>

getMediaByName(resName: string, callback: AsyncCallback&lt;Uint8Array&gt;): void

用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名      | 类型                              | 必填   | 说明                 |
| -------- | ------------------------------- | ---- | ------------------ |
H
HelloCrease 已提交
1649
| resName  | string                          | 是    | 资源名称               |
H
huangjie 已提交
1650 1651
| callback | AsyncCallback&lt;Uint8Array&gt; | 是    | 异步回调,用于返回获取的媒体文件内容 |

H
huangjie 已提交
1652 1653 1654 1655 1656 1657 1658 1659
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003  | The resName invalid.                     |
| 9001004  | The resource not found by resName.       |
| 9001006  | The resource re-ref too much.            |

H
huangjie 已提交
1660
**示例:** 
H
HelloCrease 已提交
1661
  ```ts
H
huangjie 已提交
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
  try {
    this.context.resourceManager.getMediaByName("test", (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let media = value;
        }
    });
  } catch (error) {
    console.error(`callback getMediaByName failed, error code: ${error.code}, message: ${error.message}.`)
  }
H
huangjie 已提交
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
  ```

### getMediaByName<sup>9+</sup>

getMediaByName(resName: string): Promise&lt;Uint8Array&gt;

用户获取指定资源名称对应的媒体文件内容,使用Promise形式返回字节数组。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
1684 1685
| 参数名     | 类型     | 必填   | 说明   |
| ------- | ------ | ---- | ---- |
H
huangjie 已提交
1686 1687 1688
| resName | string | 是    | 资源名称 |

**返回值:** 
H
HelloCrease 已提交
1689 1690
| 类型                        | 说明            |
| ------------------------- | ------------- |
H
huangjie 已提交
1691 1692
| Promise&lt;Uint8Array&gt; | 资源名称对应的媒体文件内容 |

H
huangjie 已提交
1693 1694 1695 1696 1697 1698 1699 1700
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003  | The resName invalid.                     |
| 9001004  | The resource not found by resName.       |
| 9001006  | The resource re-ref too much.            |

H
huangjie 已提交
1701
**示例:** 
H
HelloCrease 已提交
1702
  ```ts
H
huangjie 已提交
1703 1704 1705 1706 1707 1708 1709 1710 1711
  try {
    this.context.resourceManager.getMediaByName("test").then(value => {
        let media = value;
    }).catch(error => {
        console.log("getMediaByName promise error is " + error);
    });
  } catch (error) {
    console.error(`promise getMediaByName failed, error code: ${error.code}, message: ${error.message}.`)
  }
H
huangjie 已提交
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
  ```

### getMediaBase64ByName<sup>9+</sup>

getMediaBase64ByName(resName: string, callback: AsyncCallback&lt;string&gt;): void

用户获取指定资源名称对应的图片资源Base64编码,使用callback形式返回字符串。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名      | 类型                          | 必填   | 说明                       |
| -------- | --------------------------- | ---- | ------------------------ |
H
HelloCrease 已提交
1725
| resName  | string                      | 是    | 资源名称                     |
H
huangjie 已提交
1726 1727
| callback | AsyncCallback&lt;string&gt; | 是    | 异步回调,用于返回获取的图片资源Base64编码 |

H
huangjie 已提交
1728 1729 1730 1731 1732 1733 1734 1735
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003  | The resName invalid.                     |
| 9001004  | The resource not found by resName.       |
| 9001006  | The resource re-ref too much.            |

H
huangjie 已提交
1736
**示例:** 
H
HelloCrease 已提交
1737
  ```ts
H
huangjie 已提交
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
  try {
    this.context.resourceManager.getMediaBase64ByName("test", (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let media = value;
        }
    });
  } catch (error) {
    console.error(`callback getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`)
  }
H
huangjie 已提交
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
  ```

### getMediaBase64ByName<sup>9+</sup>

getMediaBase64ByName(resName: string): Promise&lt;string&gt;

用户获取指定资源名称对应的图片资源Base64编码,使用Promise形式返回字符串。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
1760 1761 1762
| 参数名     | 类型     | 必填   | 说明   |
| ------- | ------ | ---- | ---- |
| resName | string | 是    | 资源名称 |
H
huangjie 已提交
1763 1764

**返回值:** 
H
HelloCrease 已提交
1765 1766
| 类型                    | 说明                  |
| --------------------- | ------------------- |
H
huangjie 已提交
1767 1768
| Promise&lt;string&gt; | 资源名称对应的图片资源Base64编码 |

H
huangjie 已提交
1769 1770 1771 1772 1773 1774 1775 1776
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003  | The resName invalid.                     |
| 9001004  | The resource not found by resName.       |
| 9001006  | The resource re-ref too much.            |

H
huangjie 已提交
1777
**示例:** 
H
HelloCrease 已提交
1778
  ```ts
H
huangjie 已提交
1779 1780 1781 1782 1783 1784 1785 1786 1787
  try {
    this.context.resourceManager.getMediaBase64ByName("test").then(value => {
        let media = value;
    }).catch(error => {
        console.log("getMediaBase64ByName promise error is " + error);
    });
  } catch (error) {
    console.error(`promise getMediaBase64ByName failed, error code: ${error.code}, message: ${error.message}.`)
  }
H
huangjie 已提交
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
  ```

### getPluralStringByName<sup>9+</sup>

getPluralStringByName(resName: string, num: number, callback: AsyncCallback&lt;string&gt;): void

根据传入的数量值,获取资源名称对应的字符串资源,使用callback形式返回字符串。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
1799 1800 1801 1802
| 参数名      | 类型                          | 必填   | 说明                            |
| -------- | --------------------------- | ---- | ----------------------------- |
| resName  | string                      | 是    | 资源名称                          |
| num      | number                      | 是    | 数量值                           |
H
huangjie 已提交
1803 1804
| callback | AsyncCallback&lt;string&gt; | 是    | 异步回调,返回根据传入的数量值获取资源名称对应的字符串资源 |

H
huangjie 已提交
1805 1806 1807 1808 1809 1810 1811 1812
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003  | The resName invalid.                     |
| 9001004  | The resource not found by resName.       |
| 9001006  | The resource re-ref too much.            |

H
huangjie 已提交
1813
**示例:** 
H
HelloCrease 已提交
1814
  ```ts
H
huangjie 已提交
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
  try {
    this.context.resourceManager.getPluralStringByName("test", 1, (error, value) => {
        if (error != null) {
            console.log("error is " + error);
        } else {
            let str = value;
        }
    });
  } catch (error) {
    console.error(`callback getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`)
  }
  
H
huangjie 已提交
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
  ```

### getPluralStringByName<sup>9+</sup>

getPluralStringByName(resName: string, num: number): Promise&lt;string&gt;

根据传入的数量值,获取资源名称对应的字符串资源,使用Promise形式返回字符串。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
1838 1839
| 参数名     | 类型     | 必填   | 说明   |
| ------- | ------ | ---- | ---- |
H
huangjie 已提交
1840
| resName | string | 是    | 资源名称 |
H
HelloCrease 已提交
1841
| num     | number | 是    | 数量值  |
H
huangjie 已提交
1842 1843

**返回值:** 
H
HelloCrease 已提交
1844 1845
| 类型                    | 说明                     |
| --------------------- | ---------------------- |
H
huangjie 已提交
1846 1847
| Promise&lt;string&gt; | 根据传入的数量值获取资源名称对应的字符串资源 |

H
huangjie 已提交
1848 1849 1850 1851 1852 1853 1854 1855
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003  | The resName invalid.                     |
| 9001004  | The resource not found by resName.       |
| 9001006  | The resource re-ref too much.            |

H
huangjie 已提交
1856
**示例:** 
H
HelloCrease 已提交
1857
  ```ts
H
huangjie 已提交
1858 1859
  try {
    this.context.resourceManager.getPluralStringByName("test", 1).then(value => {
H
huangjie 已提交
1860
      let str = value;
H
huangjie 已提交
1861
    }).catch(error => {
H
huangjie 已提交
1862
      console.log("getPluralStringByName promise error is " + error);
H
huangjie 已提交
1863 1864 1865 1866
    });
  } catch (error) {
    console.error(`promise getPluralStringByName failed, error code: ${error.code}, message: ${error.message}.`)
  }
H
huangjie 已提交
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
  ```

### getStringSync<sup>9+</sup>

getStringSync(resId: number): string

用户获取指定资源ID对应的字符串,使用同步方式返回字符串。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名   | 类型     | 必填   | 说明    |
| ----- | ------ | ---- | ----- |
| resId | number | 是    | 资源ID值 |

**返回值:** 
H
HelloCrease 已提交
1883 1884
| 类型     | 说明          |
| ------ | ----------- |
H
huangjie 已提交
1885 1886 1887
| string | 资源ID值对应的字符串 |

**示例:** 
H
HelloCrease 已提交
1888
  ```ts
H
huangjie 已提交
1889 1890 1891 1892 1893
  try {
    this.context.resourceManager.getStringSync($r('app.string.test').id);
  } catch (error) {
    console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`)
  }
H
huangjie 已提交
1894 1895
  ```

1896 1897 1898 1899 1900 1901 1902 1903 1904
### getStringSync<sup>9+</sup>

getStringSync(resource: Resource): string

用户获取指定resource对象对应的字符串,使用同步方式返回字符串。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
1905 1906
| 参数名      | 类型                     | 必填   | 说明   |
| -------- | ---------------------- | ---- | ---- |
1907
| resource | [Resource](#resource9) | 是    | 资源信息 |
1908 1909

**返回值:** 
H
HelloCrease 已提交
1910 1911
| 类型     | 说明               |
| ------ | ---------------- |
1912 1913
| string | resource对象对应的字符串 |

H
huangjie 已提交
1914 1915 1916 1917 1918 1919 1920 1921
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

1922
**示例:** 
H
HelloCrease 已提交
1923
  ```ts
1924 1925 1926 1927 1928
  let resource = {
      bundleName: "com.example.myapplication",
      moduleName: "entry",
      id: $r('app.string.test').id
  };
H
huangjie 已提交
1929 1930 1931 1932 1933
  try {
    this.context.resourceManager.getStringSync(resource);
  } catch (error) {
    console.error(`getStringSync failed, error code: ${error.code}, message: ${error.message}.`)
  }
1934 1935
  ```

H
huangjie 已提交
1936 1937 1938 1939 1940 1941 1942 1943 1944
### getStringByNameSync<sup>9+</sup>

getStringByNameSync(resName: string): string

用户获取指定资源名称对应的字符串,使用同步方式返回字符串。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
1945 1946
| 参数名     | 类型     | 必填   | 说明   |
| ------- | ------ | ---- | ---- |
H
huangjie 已提交
1947 1948 1949
| resName | string | 是    | 资源名称 |

**返回值:** 
H
HelloCrease 已提交
1950 1951
| 类型     | 说明         |
| ------ | ---------- |
H
huangjie 已提交
1952 1953
| string | 资源名称对应的字符串 |

H
huangjie 已提交
1954 1955 1956 1957 1958 1959 1960 1961
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003  | The resName invalid.                     |
| 9001004  | The resource not found by resName.       |
| 9001006  | The resource re-ref too much.            |

H
huangjie 已提交
1962
**示例:** 
H
HelloCrease 已提交
1963
  ```ts
H
huangjie 已提交
1964 1965 1966 1967 1968
  try {
    this.context.resourceManager.getStringByNameSync("test");
  } catch (error) {
    console.error(`getStringByNameSync failed, error code: ${error.code}, message: ${error.message}.`)
  }
H
huangjie 已提交
1969 1970
  ```

H
huangjie 已提交
1971
### getBoolean<sup>9+</sup>
H
huangjie 已提交
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984

getBoolean(resId: number): boolean

使用同步方式,返回获取指定资源ID对应的布尔结果。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名   | 类型     | 必填   | 说明    |
| ----- | ------ | ---- | ----- |
| resId | number | 是    | 资源ID值 |

**返回值:** 
H
HelloCrease 已提交
1985 1986
| 类型      | 说明           |
| ------- | ------------ |
H
huangjie 已提交
1987 1988
| boolean | 资源ID值对应的布尔结果 |

H
huangjie 已提交
1989 1990 1991 1992 1993 1994 1995 1996
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

H
huangjie 已提交
1997
**示例:** 
H
HelloCrease 已提交
1998
  ```ts
H
huangjie 已提交
1999 2000 2001 2002 2003
  try {
    this.context.resourceManager.getBoolean($r('app.boolean.boolean_test').id);
  } catch (error) {
    console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`)
  }
H
huangjie 已提交
2004
  ```
2005 2006 2007 2008 2009 2010 2011 2012 2013
### getBoolean<sup>9+</sup>

getBoolean(resource: Resource): boolean

使用同步方式,返回获取指定resource对象对应的布尔结果。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
2014 2015
| 参数名      | 类型                     | 必填   | 说明   |
| -------- | ---------------------- | ---- | ---- |
2016
| resource | [Resource](#resource9) | 是    | 资源信息 |
2017 2018

**返回值:** 
H
HelloCrease 已提交
2019 2020
| 类型      | 说明                |
| ------- | ----------------- |
2021 2022
| boolean | resource对象对应的布尔结果 |

H
huangjie 已提交
2023 2024 2025 2026 2027 2028 2029 2030
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

2031
**示例:** 
H
HelloCrease 已提交
2032
  ```ts
2033 2034 2035 2036 2037
  let resource = {
      bundleName: "com.example.myapplication",
      moduleName: "entry",
      id: $r('app.boolean.boolean_test').id
  };
H
huangjie 已提交
2038 2039 2040 2041 2042
  try {
    this.context.resourceManager.getBoolean(resource);
  } catch (error) {
    console.error(`getBoolean failed, error code: ${error.code}, message: ${error.message}.`)
  }
2043
  ```
H
huangjie 已提交
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053

### getBooleanByName<sup>9+</sup>

getBooleanByName(resName: string): boolean

使用同步方式,返回获取指定资源名称对应的布尔结果

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
2054 2055 2056
| 参数名     | 类型     | 必填   | 说明   |
| ------- | ------ | ---- | ---- |
| resName | string | 是    | 资源名称 |
H
huangjie 已提交
2057 2058

**返回值:** 
H
HelloCrease 已提交
2059 2060
| 类型      | 说明          |
| ------- | ----------- |
H
huangjie 已提交
2061 2062
| boolean | 资源名称对应的布尔结果 |

H
huangjie 已提交
2063 2064 2065 2066 2067 2068 2069 2070
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003  | The resName invalid.                     |
| 9001004  | The resource not found by resName.       |
| 9001006  | The resource re-ref too much.            |

H
huangjie 已提交
2071
**示例:** 
H
HelloCrease 已提交
2072
  ```ts
H
huangjie 已提交
2073 2074 2075 2076 2077
  try {
    this.context.resourceManager.getBooleanByName("boolean_test");
  } catch (error) {
    console.error(`getBooleanByName failed, error code: ${error.code}, message: ${error.message}.`)
  }
H
huangjie 已提交
2078 2079
  ```

H
huangjie 已提交
2080
### getNumber<sup>9+</sup>
H
huangjie 已提交
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093

getNumber(resId: number): number

用户获取指定资源ID对应的integer数值或者float数值,使用同步方式返回资源对应的数值。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名   | 类型     | 必填   | 说明    |
| ----- | ------ | ---- | ----- |
| resId | number | 是    | 资源ID值 |

**返回值:** 
H
HelloCrease 已提交
2094 2095
| 类型     | 说明         |
| ------ | ---------- |
H
huangjie 已提交
2096 2097
| number | 资源ID值对应的数值 |

H
huangjie 已提交
2098 2099 2100 2101 2102 2103 2104 2105
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

H
huangjie 已提交
2106
**示例:** 
H
HelloCrease 已提交
2107
  ```ts
H
huangjie 已提交
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
  try {
    this.context.resourceManager.getNumber($r('app.integer.integer_test').id);
  } catch (error) {
    console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`)
  }

  try {
    this.context.resourceManager.getNumber($r('app.float.float_test').id);
  } catch (error) {
    console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`)
  }
H
huangjie 已提交
2119 2120
  ```

2121 2122 2123 2124 2125 2126 2127 2128 2129
### getNumber<sup>9+</sup>

getNumber(resource: Resource): number

用户获取指定resource对象对应的integer数值或者float数值,使用同步方式返回资源对应的数值。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
2130 2131
| 参数名      | 类型                     | 必填   | 说明   |
| -------- | ---------------------- | ---- | ---- |
2132
| resource | [Resource](#resource9) | 是    | 资源信息 |
2133 2134

**返回值:** 
H
HelloCrease 已提交
2135 2136
| 类型     | 说明              |
| ------ | --------------- |
2137 2138
| number | resource对象对应的数值 |

H
huangjie 已提交
2139 2140 2141 2142 2143 2144 2145 2146
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001001  | The resId invalid.                       |
| 9001002  | The resource not found by resId.         |
| 9001006  | The resource re-ref too much.            |

2147
**示例:** 
H
HelloCrease 已提交
2148
  ```ts
2149 2150 2151 2152 2153
  let resource = {
      bundleName: "com.example.myapplication",
      moduleName: "entry",
      id: $r('app.integer.integer_test').id
  };
H
huangjie 已提交
2154 2155 2156 2157 2158
  try {
    this.context.resourceManager.getNumber(resource);
  } catch (error) {
    console.error(`getNumber failed, error code: ${error.code}, message: ${error.message}.`)
  }
2159 2160
  ```

H
huangjie 已提交
2161 2162 2163 2164 2165 2166 2167 2168 2169
### getNumberByName<sup>9+</sup>

getNumberByName(resName: string): number

用户获取指定资源名称对应的integer数值或者float数值,使用同步方式资源对应的数值。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
H
HelloCrease 已提交
2170 2171
| 参数名     | 类型     | 必填   | 说明   |
| ------- | ------ | ---- | ---- |
H
huangjie 已提交
2172 2173 2174
| resName | string | 是    | 资源名称 |

**返回值:** 
H
HelloCrease 已提交
2175 2176
| 类型     | 说明        |
| ------ | --------- |
H
huangjie 已提交
2177 2178
| number | 资源名称对应的数值 |

H
huangjie 已提交
2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314
以下错误码的详细介绍请参见[资源管理错误码](../errorcodes/errorcode-resource-manager.md)

| 错误码ID | 错误信息 |
| -------- | ---------------------------------------- |
| 9001003  | The resName invalid.                     |
| 9001004  | The resource not found by resName.       |
| 9001006  | The resource re-ref too much.            |

**示例:** 
  ```ts
  try {
    this.context.resourceManager.getNumberByName("integer_test");
  } catch (error) {
    console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`)
  }

  try {
    this.context.resourceManager.getNumberByName("float_test");
  } catch (error) {
    console.error(`getNumberByName failed, error code: ${error.code}, message: ${error.message}.`)
  }
  ```


### getString<sup>(deprecated)</sup>

getString(resId: number, callback: AsyncCallback&lt;string&gt;): void

用户获取指定资源ID对应的字符串,使用callback形式返回字符串。

从API version 9开始不再维护,建议使用[getStringValue](#getstringvalue9)代替。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名      | 类型                          | 必填   | 说明              |
| -------- | --------------------------- | ---- | --------------- |
| resId    | number                      | 是    | 资源ID值           |
| callback | AsyncCallback&lt;string&gt; | 是    | 异步回调,用于返回获取的字符串 |

**示例:** 
  ```ts
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getString($r('app.string.test').id, (error, value) => {
          if (error != null) {
              console.log("error is " + error);
          } else {
              let str = value;
          }
      });
  });
  ```


### getString<sup>(deprecated)</sup>

getString(resId: number): Promise&lt;string&gt;

用户获取指定资源ID对应的字符串,使用Promise形式返回字符串。

从API version 9开始不再维护,建议使用[getStringValue](#getstringvalue9-1)代替。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名   | 类型     | 必填   | 说明    |
| ----- | ------ | ---- | ----- |
| resId | number | 是    | 资源ID值 |

**返回值:** 
| 类型                    | 说明          |
| --------------------- | ----------- |
| Promise&lt;string&gt; | 资源ID值对应的字符串 |

**示例:** 
  ```ts
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getString($r('app.string.test').id).then(value => {
          let str = value;
      }).catch(error => {
          console.log("getstring promise error is " + error);
      });
  });
  ```


### getStringArray<sup>(deprecated)</sup>

getStringArray(resId: number, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void

用户获取指定资源ID对应的字符串数组,使用callback形式返回字符串数组。

从API version 9开始不再维护,建议使用[getStringArrayValue](#getstringarrayvalue9)代替。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名      | 类型                                       | 必填   | 说明                |
| -------- | ---------------------------------------- | ---- | ----------------- |
| resId    | number                                   | 是    | 资源ID值             |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是    | 异步回调,用于返回获取的字符串数组 |

**示例:** 
  ```ts
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getStringArray($r('app.strarray.test').id, (error, value) => {
          if (error != null) {
              console.log("error is " + error);
          } else {
              let strArray = value;
          }
      });
  });
  ```


### getStringArray<sup>(deprecated)</sup>

getStringArray(resId: number): Promise&lt;Array&lt;string&gt;&gt;

用户获取指定资源ID对应的字符串数组,使用Promise形式返回字符串数组。

从API version 9开始不再维护,建议使用[getStringArrayValue](#getstringarrayvalue9-1)代替。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名   | 类型     | 必填   | 说明    |
| ----- | ------ | ---- | ----- |
| resId | number | 是    | 资源ID值 |

**返回值:** 
| 类型                                 | 说明            |
| ---------------------------------- | ------------- |
| Promise&lt;Array&lt;string&gt;&gt; | 资源ID值对应的字符串数组 |

H
huangjie 已提交
2315
**示例:** 
H
HelloCrease 已提交
2316
  ```ts
H
huangjie 已提交
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638
  resourceManager.getResourceManager((error, mgr) => {
       mgr.getStringArray($r('app.strarray.test').id).then(value => {
          let strArray = value;
      }).catch(error => {
          console.log("getStringArray promise error is " + error);
      });
  });
  ```


### getMedia<sup>(deprecated)</sup>

getMedia(resId: number, callback: AsyncCallback&lt;Uint8Array&gt;): void

用户获取指定资源ID对应的媒体文件内容,使用callback形式返回字节数组。

从API version 9开始不再维护,建议使用[getMediaContent](#getmediacontent)代替。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名      | 类型                              | 必填   | 说明                 |
| -------- | ------------------------------- | ---- | ------------------ |
| resId    | number                          | 是    | 资源ID值              |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是    | 异步回调,用于返回获取的媒体文件内容 |

**示例:** 
  ```ts
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getMedia($r('app.media.test').id, (error, value) => {
          if (error != null) {
              console.log("error is " + error);
          } else {
              let media = value;
          }
      });
  });
  ```


### getMedia<sup>(deprecated)</sup>

getMedia(resId: number): Promise&lt;Uint8Array&gt;

用户获取指定资源ID对应的媒体文件内容,使用Promise形式返回字节数组。

从API version 9开始不再维护,建议使用[getMediaContent](#getmediacontent-1)代替。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名   | 类型     | 必填   | 说明    |
| ----- | ------ | ---- | ----- |
| resId | number | 是    | 资源ID值 |

**返回值:** 
| 类型                        | 说明             |
| ------------------------- | -------------- |
| Promise&lt;Uint8Array&gt; | 资源ID值对应的媒体文件内容 |

**示例:** 
  ```ts
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getMedia($r('app.media.test').id).then(value => {
          let media = value;
      }).catch(error => {
          console.log("getMedia promise error is " + error);
      });
  });
  ```


### getMediaBase64<sup>(deprecated)</sup>

getMediaBase64(resId: number, callback: AsyncCallback&lt;string&gt;): void

用户获取指定资源ID对应的图片资源Base64编码,使用callback形式返回字符串。

从API version 9开始不再维护,建议使用[getMediaContentBase64](#getmediacontentbase64)代替。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名      | 类型                          | 必填   | 说明                       |
| -------- | --------------------------- | ---- | ------------------------ |
| resId    | number                      | 是    | 资源ID值                    |
| callback | AsyncCallback&lt;string&gt; | 是    | 异步回调,用于返回获取的图片资源Base64编码 |

**示例:** 
  ```ts
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getMediaBase64($r('app.media.test').id, (error, value) => {
          if (error != null) {
              console.log("error is " + error);
          } else {
              let media = value;
          }
      });
  });
  ```


### getMediaBase64<sup>(deprecated)</sup>

getMediaBase64(resId: number): Promise&lt;string&gt;

用户获取指定资源ID对应的图片资源Base64编码,使用Promise形式返回字符串。

从API version 9开始不再维护,建议使用[getMediaContentBase64](#getmediacontentbase64-1)代替。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名   | 类型     | 必填   | 说明    |
| ----- | ------ | ---- | ----- |
| resId | number | 是    | 资源ID值 |

**返回值:** 
| 类型                    | 说明                   |
| --------------------- | -------------------- |
| Promise&lt;string&gt; | 资源ID值对应的图片资源Base64编码 |

**示例:** 
  ```ts
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getMediaBase64($r('app.media.test').id).then(value => {
          let media = value;
      }).catch(error => {
          console.log("getMediaBase64 promise error is " + error);
      });
  });
  ```


### getPluralString<sup>(deprecated)</sup>

getPluralString(resId: number, num: number): Promise&lt;string&gt;

根据指定数量获取对指定ID字符串表示的单复数字符串,使用Promise形式返回字符串。

从API version 9开始不再维护,建议使用[getPluralStringValue](#getpluralstringvalue)代替。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名   | 类型     | 必填   | 说明    |
| ----- | ------ | ---- | ----- |
| resId | number | 是    | 资源ID值 |
| num   | number | 是    | 数量值   |

**返回值:** 
| 类型                    | 说明                        |
| --------------------- | ------------------------- |
| Promise&lt;string&gt; | 根据提供的数量获取对应ID字符串表示的单复数字符串 |

**示例:** 
  ```ts
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getPluralString($r("app.plural.test").id, 1).then(value => {
          let str = value;
      }).catch(error => {
          console.log("getPluralString promise error is " + error);
      });
  });
  ```


### getPluralString<sup>(deprecated)</sup>

getPluralString(resId: number, num: number, callback: AsyncCallback&lt;string&gt;): void

根据指定数量获取指定ID字符串表示的单复数字符串,使用callback形式返回字符串。

从API version 9开始不再维护,建议使用[getPluralStringValue](#getpluralstringvalue-1)代替。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名      | 类型                          | 必填   | 说明                              |
| -------- | --------------------------- | ---- | ------------------------------- |
| resId    | number                      | 是    | 资源ID值                           |
| num      | number                      | 是    | 数量值                             |
| callback | AsyncCallback&lt;string&gt; | 是    | 异步回调,返回根据指定数量获取指定ID字符串表示的单复数字符串 |

**示例:** 
  ```ts
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getPluralString($r("app.plural.test").id, 1, (error, value) => {
          if (error != null) {
              console.log("error is " + error);
          } else {
              let str = value;
          }
      });
  });
  ```


### getRawFile<sup>(deprecated)</sup>

getRawFile(path: string, callback: AsyncCallback&lt;Uint8Array&gt;): void

用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback形式返回字节数组。

从API version 9开始不再维护,建议使用[getRawFileContent](#getrawfilecontent9)代替。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名      | 类型                              | 必填   | 说明                      |
| -------- | ------------------------------- | ---- | ----------------------- |
| path     | string                          | 是    | rawfile文件路径             |
| callback | AsyncCallback&lt;Uint8Array&gt; | 是    | 异步回调,用于返回获取的rawfile文件内容 |

**示例:** 
  ```ts
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getRawFile("test.xml", (error, value) => {
          if (error != null) {
              console.log("error is " + error);
          } else {
              let rawFile = value;
          }
      });
  });
  ```


### getRawFile<sup>(deprecated)</sup>

getRawFile(path: string): Promise&lt;Uint8Array&gt;

用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise形式返回字节数组。

从API version 9开始不再维护,建议使用[getRawFileContent](#getrawfilecontent9-1)代替。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名  | 类型     | 必填   | 说明          |
| ---- | ------ | ---- | ----------- |
| path | string | 是    | rawfile文件路径 |

**返回值:** 
| 类型                        | 说明          |
| ------------------------- | ----------- |
| Promise&lt;Uint8Array&gt; | rawfile文件内容 |

**示例:** 
  ```ts
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getRawFile("test.xml").then(value => {
          let rawFile = value;
      }).catch(error => {
          console.log("getRawFile promise error is " + error);
      });
  });
  ```


### getRawFileDescriptor<sup>(deprecated)</sup>

getRawFileDescriptor(path: string, callback: AsyncCallback&lt;RawFileDescriptor&gt;): void

用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用callback形式返回。

从API version 9开始不再维护,建议使用[getRawFd](#getrawfd9)代替。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名      | 类型                                       | 必填   | 说明                               |
| -------- | ---------------------------------------- | ---- | -------------------------------- |
| path     | string                                   | 是    | rawfile文件路径                      |
| callback | AsyncCallback&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | 是    | 异步回调,用于返回获取的rawfile文件的descriptor |

**示例:** 
  ```ts
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getRawFileDescriptor("test.xml", (error, value) => {
          if (error != null) {
              console.log("error is " + error);
          } else {
              let fd = value.fd;
              let offset = value.offset;
              let length = value.length;
          }
      });
  });
  ```

### getRawFileDescriptor<sup>(deprecated)</sup>

getRawFileDescriptor(path: string): Promise&lt;RawFileDescriptor&gt;

用户获取resources/rawfile目录下对应rawfile文件的descriptor,使用Promise形式返回。

从API version 9开始不再维护,建议使用[getRawFd](#getrawfd9-1)代替。

**系统能力**:SystemCapability.Global.ResourceManager

**参数:** 
| 参数名  | 类型     | 必填   | 说明          |
| ---- | ------ | ---- | ----------- |
| path | string | 是    | rawfile文件路径 |

**返回值:** 
| 类型                                       | 说明                  |
| ---------------------------------------- | ------------------- |
| Promise&lt;[RawFileDescriptor](#rawfiledescriptor8)&gt; | rawfile文件descriptor |

**示例:** 
  ```ts
  resourceManager.getResourceManager((error, mgr) => {
      mgr.getRawFileDescriptor("test.xml").then(value => {
          let fd = value.fd;
          let offset = value.offset;
          let length = value.length;
      }).catch(error => {
          console.log("getRawFileDescriptor promise error is " + error);
      });
  });
H
huangjie 已提交
2639
  ```