1e41f4b71Sopenharmony_ci# Starting a File Application
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## When to Use
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciYou can call [startAbility](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) to enable the system to search for an application that can handle the intent of opening a file from installed applications. For example, after downloading a PDF file from a browser, you can call this API to start a file application to open the PDF file. You must set the file URI (specified by [uri](#key-api-parameters)) and type (specified by [type](#key-api-parameters)) and other fields in the request so that the system can identify the file to open. Then the system directly starts an application to open the file or displays a dialog box for users to select an application to open the file.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci**Figure 1** Example of opening a file
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci![](figures/file-open.jpeg)
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## Key API Parameters
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ciYou can call [startAbility](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) to start an installed vertical application to open a file.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci**Table 1** Description of [want](../reference/apis-ability-kit/js-apis-app-ability-want.md) in startAbility
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci| Parameter| Type  | Mandatory| Description                 |
18e41f4b71Sopenharmony_ci|----------|--------|----------|---------------------|
19e41f4b71Sopenharmony_ci| uri      | string | Yes      | URI of the file to open. This parameter is used together with **type**.<br>The URI format is file:\/\/bundleName\/path.<br>- **file**: indicates a file URI.<br>- *bundleName*: specifies the owner of the file.<br>- *path*: specifies the application sandbox path of the file.|
20e41f4b71Sopenharmony_ci| type     | string | No      | MIME type of the file to open, for example, **'text/xml'** and **'image/*'**. For details about the MIME type definition, see https://www.iana.org/assignments/media-types/media-types.xhtml?utm_source=ld246.com.                 |
21e41f4b71Sopenharmony_ci| parameters | Record<string, Object>       | No        | Custom parameters that are defined by the system and assigned values by developers as required. For details, see Table 2.        |
22e41f4b71Sopenharmony_ci| flags | number | No| Processing mode. For details, see Table 3.                                                                                                                                                                                      |
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci**Table 2** Description of [parameters](../reference/apis-ability-kit/js-apis-app-ability-wantConstant.md#wantconstantparams)
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci| Parameter                             | Type   | Description                                                                                                                                                               |
28e41f4b71Sopenharmony_ci|---------------------------------------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|
29e41f4b71Sopenharmony_ci| ability.params.stream                 | string  | File URIs to be authorized to the target application. This parameter is used when the file to open depends on other files. For example, opening a local HTML file depends on other local resource files. The value must be an array of file URIs of the string type. For details about how to obtain the file URI, see the **uri** parameter in Table 1.|
30e41f4b71Sopenharmony_ci| ohos.ability.params.showDefaultPicker | string | Whether to display a dialog box to ask users to select a file opening mode when only one matching application is found.<br>- **false**: The display of the dialog box is determined by the system policy or default application settings.<br>- **true**: Such a dialog box is always displayed. The default value is **false**.                                                                           |
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci**Table 3** Description of [flags](../reference/apis-ability-kit/js-apis-app-ability-wantConstant.md#wantconstantflags)
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci| Parameter                      | Value        | Description                      |
35e41f4b71Sopenharmony_ci|--------------------------------|------------|----------------------------|
36e41f4b71Sopenharmony_ci| FLAG_AUTH_READ_URI_PERMISSION  | 0x00000001 | Grants the permission to read the URI.|
37e41f4b71Sopenharmony_ci| FLAG_AUTH_WRITE_URI_PERMISSION | 0x00000002 | Grants the permission to write data to the URI.|
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci## How to Develop
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci### Procedure for the Caller Application
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci1. Import the required modules.
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci    ```ts
46e41f4b71Sopenharmony_ci    // xxx.ets
47e41f4b71Sopenharmony_ci    import { fileUri } from '@kit.CoreFileKit';
48e41f4b71Sopenharmony_ci    import { UIAbility, Want, common, wantConstant } from '@kit.AbilityKit';
49e41f4b71Sopenharmony_ci    import { BusinessError } from '@kit.BasicServicesKit';
50e41f4b71Sopenharmony_ci    ```
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci2. Obtain the [application file paths](application-context-stage.md#obtaining-application-file-paths).
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci    ```ts
55e41f4b71Sopenharmony_ci    // xxx.ets
56e41f4b71Sopenharmony_ci    // Assume that the bundle name is com.example.demo.
57e41f4b71Sopenharmony_ci    export default class EntryAbility extends UIAbility {
58e41f4b71Sopenharmony_ci        onWindowStageCreate(windowStage: window.WindowStage) {
59e41f4b71Sopenharmony_ci            // Obtain the application sandbox path of the file.
60e41f4b71Sopenharmony_ci            let filePath = this.context.filesDir + '/test1.txt';
61e41f4b71Sopenharmony_ci            // Convert the application sandbox path into a URI.
62e41f4b71Sopenharmony_ci            let uri = fileUri.getUriFromPath(filePath);
63e41f4b71Sopenharmony_ci            // The obtained URI is file://com.example.demo/data/storage/el2/base/files/test.txt.
64e41f4b71Sopenharmony_ci        }
65e41f4b71Sopenharmony_ci        // ...
66e41f4b71Sopenharmony_ci    }
67e41f4b71Sopenharmony_ci    ```
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci3. Construct request data.
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci    ```ts
72e41f4b71Sopenharmony_ci    // xxx.ets
73e41f4b71Sopenharmony_ci    export default class EntryAbility extends UIAbility {
74e41f4b71Sopenharmony_ci        onWindowStageCreate(windowStage: window.WindowStage) {
75e41f4b71Sopenharmony_ci            // Obtain the application sandbox path of the file.
76e41f4b71Sopenharmony_ci            let filePath = this.context.filesDir + '/test.txt';
77e41f4b71Sopenharmony_ci            // Convert the application sandbox path into a URI.
78e41f4b71Sopenharmony_ci            let uri = fileUri.getUriFromPath(filePath);
79e41f4b71Sopenharmony_ci            // Construct the request data.
80e41f4b71Sopenharmony_ci            let want: Want = {
81e41f4b71Sopenharmony_ci            uri: uri,
82e41f4b71Sopenharmony_ci            type: 'text/plain', // Type of the file to open.
83e41f4b71Sopenharmony_ci            // Grant the read and write permissions on the file to the target application.
84e41f4b71Sopenharmony_ci            flags: wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION | wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION
85e41f4b71Sopenharmony_ci            };
86e41f4b71Sopenharmony_ci        }
87e41f4b71Sopenharmony_ci        // ...
88e41f4b71Sopenharmony_ci    }
89e41f4b71Sopenharmony_ci    ```
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci4. Call the API to start the target application.
92e41f4b71Sopenharmony_ci    
93e41f4b71Sopenharmony_ci    ```ts
94e41f4b71Sopenharmony_ci    // xxx.ets
95e41f4b71Sopenharmony_ci    export default class EntryAbility extends UIAbility {
96e41f4b71Sopenharmony_ci        onWindowStageCreate(windowStage: window.WindowStage) {
97e41f4b71Sopenharmony_ci            // Obtain the application sandbox path of the file.
98e41f4b71Sopenharmony_ci            let filePath = this.context.filesDir + '/test.txt';
99e41f4b71Sopenharmony_ci            // Convert the application sandbox path into a URI.
100e41f4b71Sopenharmony_ci            let uri = fileUri.getUriFromPath(filePath);
101e41f4b71Sopenharmony_ci            // Construct the request data.
102e41f4b71Sopenharmony_ci            let want: Want = {
103e41f4b71Sopenharmony_ci            uri: uri,
104e41f4b71Sopenharmony_ci            type: 'text/plain', // Type of the file to open.
105e41f4b71Sopenharmony_ci            // Grant the read and write permissions on the file to the target application.
106e41f4b71Sopenharmony_ci            flags: wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION | wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION
107e41f4b71Sopenharmony_ci            };
108e41f4b71Sopenharmony_ci            // Call startAbility.
109e41f4b71Sopenharmony_ci            this.context.startAbility(want)
110e41f4b71Sopenharmony_ci            .then(() => {
111e41f4b71Sopenharmony_ci                console.info('Succeed to invoke startAbility.');
112e41f4b71Sopenharmony_ci            })
113e41f4b71Sopenharmony_ci            .catch((err: BusinessError) => {
114e41f4b71Sopenharmony_ci                console.error(`Failed to invoke startAbility, code: ${err.code}, message: ${err.message}`);
115e41f4b71Sopenharmony_ci            });
116e41f4b71Sopenharmony_ci        }
117e41f4b71Sopenharmony_ci        // ...
118e41f4b71Sopenharmony_ci    }
119e41f4b71Sopenharmony_ci    ```
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci### Procedure for the Target Application
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci1. Declare the capability to open files.
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci    Applications that are able to open files must declare the file opening capability in the [module.json5](../quick-start/module-configuration-file.md) file. The **uris** field indicates the type of the URIs, and the **scheme** field is fixed at **file**. The **type** field indicates the types of files that can be opened. For details, see [MIME Definition](https://www.iana.org/assignments/media-types/media-types.xhtml?utm_source=ld246.com). In the following example, the file type is TXT.
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci    ```ts
128e41f4b71Sopenharmony_ci    {
129e41f4b71Sopenharmony_ci    "module": {
130e41f4b71Sopenharmony_ci        // ...
131e41f4b71Sopenharmony_ci        "abilities": [
132e41f4b71Sopenharmony_ci        {
133e41f4b71Sopenharmony_ci            // ...
134e41f4b71Sopenharmony_ci            "skills": [
135e41f4b71Sopenharmony_ci            {
136e41f4b71Sopenharmony_ci                "actions": [
137e41f4b71Sopenharmony_ci                "ohos.want.action.viewData" // Mandatory. Declare the data processing capability.
138e41f4b71Sopenharmony_ci                ],
139e41f4b71Sopenharmony_ci                "uris": [
140e41f4b71Sopenharmony_ci                {
141e41f4b71Sopenharmony_ci                    // Declare the capability of opening local files whose URIs start with file://.
142e41f4b71Sopenharmony_ci                    "scheme": "file", // Mandatory. Declare that the scheme type is file.
143e41f4b71Sopenharmony_ci                    "type": "text/plain", // Mandatory. Specify the type of file that can be opened.
144e41f4b71Sopenharmony_ci                    "linkFeature": "FileOpen" // Mandatory. Specify that the URI is used to open files.
145e41f4b71Sopenharmony_ci                }
146e41f4b71Sopenharmony_ci                // ...
147e41f4b71Sopenharmony_ci                ]
148e41f4b71Sopenharmony_ci                // ...
149e41f4b71Sopenharmony_ci            }
150e41f4b71Sopenharmony_ci            ]
151e41f4b71Sopenharmony_ci        }
152e41f4b71Sopenharmony_ci        ]
153e41f4b71Sopenharmony_ci    }
154e41f4b71Sopenharmony_ci    }
155e41f4b71Sopenharmony_ci    ```
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci2. Process the file.
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci    After the target application is started and obtains the URI of the file to open from the [want](../reference/apis-ability-kit/js-apis-app-ability-want.md) information, it opens the file and obtains the corresponding file object for reading and writing.
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ci    ```ts
163e41f4b71Sopenharmony_ci    // xxx.ets
164e41f4b71Sopenharmony_ci    import fs from '@ohos.file.fs';
165e41f4b71Sopenharmony_ci    import { Want } from '@kit.AbilityKit';
166e41f4b71Sopenharmony_ci    import { BusinessError } from '@kit.BasicServicesKit';
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci    export default class EntryAbility extends UIAbility {
169e41f4b71Sopenharmony_ci        onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
170e41f4b71Sopenharmony_ci            // Obtain the uri field from the want information.
171e41f4b71Sopenharmony_ci            let uri = want.uri;
172e41f4b71Sopenharmony_ci            if (uri == null || uri == undefined) {
173e41f4b71Sopenharmony_ci                console.info('uri is invalid');
174e41f4b71Sopenharmony_ci                return;
175e41f4b71Sopenharmony_ci            }
176e41f4b71Sopenharmony_ci            try {
177e41f4b71Sopenharmony_ci                // Perform operations on the URI of the file as required. For example, open the URI to obtain the file object in read/write mode.
178e41f4b71Sopenharmony_ci                let file = fs.openSync(uri, fs.OpenMode.READ_WRITE);
179e41f4b71Sopenharmony_ci                console.info('Succeed to open file.');
180e41f4b71Sopenharmony_ci            } catch (err) {
181e41f4b71Sopenharmony_ci                let error: BusinessError = err as BusinessError;
182e41f4b71Sopenharmony_ci                console.error(`Failed to open file openSync, code: ${error.code}, message: ${error.message}`);
183e41f4b71Sopenharmony_ci            }
184e41f4b71Sopenharmony_ci        }
185e41f4b71Sopenharmony_ci    }
186e41f4b71Sopenharmony_ci    ```
187