1# @ohos.sendableResourceManager (资源管理)
2
3资源管理导入sendableResourceManager模块,通过调用[resourceToSendableResource](#sendableresourcemanagerresourcetosendableresource)和[sendableResourceToResource](#sendableresourcemanagersendableresourcetoresource)方法可以将[Resource](#resource)对象和[SendableResource](#sendableresource)对象进行互转。
4
5Resource对象通过转换为SendableResource对象后,可以被[Sendable类](../../arkts-utils/arkts-sendable.md)持有。Sendable类在跨线程传输后,取出持有的SendableResource对象转为Resource对象,作为参数获取资源。
6
7> **说明:**
8>
9> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
10
11## 导入模块
12
13```js
14import sendableResourceManager from '@ohos.sendableResourceManager';
15```
16
17## sendableResourceManager.resourceToSendableResource
18
19resourceToSendableResource(resource: Resource): SendableResource
20
21将Resource对象转换为SendableResource对象。
22
23**系统能力**:SystemCapability.Global.ResourceManager
24
25**参数:**
26
27| 参数名      | 类型                                       | 必填   | 说明                            |
28| -------- | ---------------------------------------- | ---- | ----------------------------- |
29| resource | [Resource](#resource) | 是    | Resource对象。 |
30
31**返回值:**
32
33| 类型     | 说明          |
34| ------ | ---------------------------- |
35| [SendableResource](#sendableresource)  | 转换后的SendableResource对象。 |
36
37**错误码:**
38
39以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
40
41| 错误码ID | 错误信息 |
42| -------- | ---------------------------------------- |
43| 401 | If the input parameter invalid. Possible causes: <br>1.Incorrect parameter types; <br>2.Parameter verification failed.                 |
44
45**示例:**
46  ```js
47import sendableResourceManager from '@ohos.sendableResourceManager';
48import { BusinessError } from '@ohos.base';
49
50try {
51    let sendableResource: sendableResourceManager.SendableResource = sendableResourceManager.resourceToSendableResource($r('app.string.test'));
52} catch (error) {
53    let code = (error as BusinessError).code;
54    let message = (error as BusinessError).message;
55    console.error(`resourceToSendableResource failed, error code: ${code}, message: ${message}.`);
56}
57  ```
58
59## sendableResourceManager.sendableResourceToResource
60
61sendableResourceToResource(sendableResource: SendableResource): Resource
62
63将SendableResource对象转换为Resource对象。
64
65**系统能力**:SystemCapability.Global.ResourceManager
66
67**参数:**
68
69| 参数名      | 类型                                       | 必填   | 说明                            |
70| -------- | ---------------------------------------- | ---- | ----------------------------- |
71| sendableResource | [SendableResource](#sendableresource) | 是    | SendableResource对象。 |
72
73**返回值:**
74
75| 类型     | 说明          |
76| ------ | ---------------------------- |
77| [Resource](#resource) | 转换后的Resource对象。 |
78
79**错误码:**
80
81以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
82
83| 错误码ID | 错误信息 |
84| -------- | ---------------------------------------- |
85| 401 | If the input parameter invalid. Possible causes: <br>1.Incorrect parameter types; <br>2.Parameter verification failed.                 |
86
87**示例:**
88  ```js
89import sendableResourceManager from '@ohos.sendableResourceManager';
90import { BusinessError } from '@ohos.base';
91
92try {
93    let resource: sendableResourceManager.Resource = sendableResourceManager.sendableResourceToResource(sendableResourceManager.resourceToSendableResource($r('app.string.test')));
94} catch (error) {
95    let code = (error as BusinessError).code;
96    let message = (error as BusinessError).message;
97    console.error(`resourceToSendableResource failed, error code: ${code}, message: ${message}.`);
98}
99  ```
100
101## Resource
102
103表示的Resource资源信息。
104
105**系统能力:** 以下各项对应的系统能力均为SystemCapability.Global.ResourceManager
106
107**参数:**
108
109| 名称         | 类型     | 只读   | 可选  |说明          |
110| ---------- | ------ | ----- | ----  | ---------------|
111| bundleName | string | 否    | 否 | 应用的bundle名称。 |
112| moduleName | string | 否    | 否 | 应用的module名称。 |
113| id         | number | 否    | 否 | 资源的id值。      |
114| params     | any[] | 否    | 是 | 其他资源参数,包括资源名、格式化接口的替换值、复数接口的量词。      |
115| type       | number | 否    | 是 | 资源的类型。      |
116
117## SendableResource
118
119type SendableResource = _SendableResource
120
121**系统能力:** SystemCapability.Global.ResourceManager
122
123| 类型         | 说明     |
124| ---------- | ------ |
125| [_SendableResource](sendableResource.md#sendableresource-1)|表示的SendableResource资源信息。|