• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using Want to Share Data Between Applications
2
3Users often need to share data (such as a text or an image) from one application to another. The following uses PDF file sharing as an example to describe how to use Want to share data between applications.
4
5Data sharing requires two UIAbility components (one for the sharing party and the other for the shared party) and one system component (used as the application sharing box). When the sharing party initiates data sharing by calling **startAbility()**, the system implicitly matches and displays all applications that support the type of data to share. After the user selects an application, the system starts the application to complete data sharing.
6
7In this section, data sharing is triggered by touching a button. You can use other ways to trigger data sharing during application development. This section focuses on how to configure Want to implement data sharing.
8
9The following actions are involved for data sharing:
10
11- **ohos.want.action.select**: action of starting the application sharing box.
12- **ohos.want.action.sendData**: action of sending a single data record, that is, transferring data to the shared party.
13
14## Sharing Party
15
16The sharing party starts an application sharing box and transfers the data to the shared party. Therefore, Want of the sharing party must be nested at two layers. In the first layer, implicit Want is used together with the **ohos.want.action.select** action to display the application sharing box. In the second layer, the data to share is declared
17
18in the custom field **parameters**, and then the Want that includes the **ohos.want.action.sendData** action and the **parameters** field is transferred to the application sharing box. The shared party obtains the shared data from **parameters**.
19
20```ts
21import common from '@ohos.app.ability.common';
22
23let fileType = 'application/pdf';
24let fileName = 'TestFile.pdf';
25let fileFd = -1; // Obtain the file descriptor (FD) of the file to share.
26let fileSize; // Obtain the size of the file to share.
27
28function implicitStartAbility() {
29  let context = getContext(this) as common.UIAbilityContext;
30  let wantInfo = {
31    / This action is used to implicitly match the application sharing box.
32    action: 'ohos.want.action.select',
33    // This is the custom parameter in the first layer of Want,
34    / which is intended to add information to the application sharing box.
35    parameters: {
36      // MIME type of PDF.
37      'ability.picker.type': fileType,
38      'ability.picker.fileNames': [fileName],
39      'ability.picker.fileSizes': [fileSize],
40      // This is nested Want ,which will be directly sent to the selected application.
41      'ability.want.params.INTENT': {
42        'action': 'ohos.want.action.sendData',
43        'type': 'application/pdf',
44        'parameters': {
45          'keyFd': { 'type': 'FD', 'value': fileFd }
46        }
47      }
48    }
49  }
50  context.startAbility(wantInfo).then(() => {
51    // ...
52  }).catch((err) => {
53    // ...
54  })
55}
56```
57
58> **NOTE**
59>
60> Data sharing can be implemented only in FD format. For details about how to obtain the FD and file name, see [File Management](../reference/apis/js-apis-file-fs.md).
61
62In the preceding code, under the custom field **parameters**, the following **ability.picker.*** fields are used to pass the information to be displayed on the application sharing box:
63
64- **ability.picker.type**: file type icon.
65- **ability.picker.fileNames**: file name.
66- **ability.picker.fileSizes**: file size, in bytes.
67- **ability.picker.fileNames** and **ability.picker.fileSizes** are arrays and have a one-to-one mapping.
68
69The following figure shows an example.
70
71![stage-want2](figures/stage-want2.png)
72
73## Shared Party
74
75To enable the shared party to identify the shared content, configure **skills** in the [module.json5 file](../quick-start/module-configuration-file.md) of the UIAbility of the shared party. The **actions** and **type** fields in **uris** match the **action** and **type** fields in **ability.want.params.INTENT** of the sharing party, respectively.
76
77```json
78{
79  "module": {
80    // ...
81    "abilities": [
82      {
83        // ...
84        "skills": [
85          {
86            // ...
87            "actions": [
88              "action.system.home",
89              "ohos.want.action.sendData"
90              // ...
91            ],
92            "uris": [
93              {
94                "type": "application/pdf"
95              },
96            ]
97          }
98        ]
99      }
100    ]
101  }
102}
103```
104
105After the user selects an application, the Want nested in the **ability.want.params.INTENT** field is passed to that application. The UIAbility of the shared party, after being started, can call [onCreate()](../reference/apis/js-apis-app-ability-uiAbility.md#uiabilityoncreate) or [onNewWant()](../reference/apis/js-apis-app-ability-uiAbility.md#uiabilityonnewwant) to obtain the passed Want.
106
107The following is an example of the Want obtained. You can use the FD of the shared file to perform required operations.
108
109```json
110{
111    "deviceId": "",
112    "bundleName": "com.example.myapplication",
113    "abilityName": "EntryAbility",
114    "moduleName": "entry",
115    "uri": "",
116    "type": "application/pdf",
117    "flags": 0,
118    "action": "ohos.want.action.sendData",
119    "parameters": {
120        "component.startup.newRules": true,
121        "keyFd": {
122            "type": "FD",
123            "value": 36
124        },
125        "mime-type": "application/pdf",
126        "moduleName": "entry",
127        "ohos.aafwk.param.callerPid": 3488,
128        "ohos.aafwk.param.callerToken": 537379209,
129        "ohos.aafwk.param.callerUid": 20010014
130    },
131    "entities": []
132}
133```
134