harmony 鸿蒙Using startAbility to Start a File Application

  • 2025-06-06
  • 浏览 (2)

Using startAbility to Start a File Application

When to Use

You can call startAbility to search for an installed application that meets the requirements to open a specific file. For example, when a browser downloads a PDF file, this API can be called to select a file application to open the PDF file. You must set the file URI (specified by uri) and type (specified by type) 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.

Figure 1 Example of opening a file

Key API Parameters

You can call startAbility to start an installed vertical application to open a file.

Table 1 Description of want in startAbility

Parameter Type Mandatory Description
uri string Yes URI of the file to open. This parameter is used together with type.
The URI format is file:\/\/bundleName\/path.
- file: indicates a file URI.
- bundleName: specifies the owner of the file.
- path: specifies the application sandbox path of the file.
type string No Type of the file to open. UTD is recommended, for example, ‘general.plain-text’ and ‘general.image’. The MIME type is also supported, for example, ‘text/xml’ and ‘image/*’.
NOTE
1. The type field is optional. If it is not passed, the system attempts to match the file type based on the URI suffix. If it is passed, ensure that it is the same as the file type specified in the URI. Otherwise, no proper application can be matched. For details about the mappings between file name extensions and file types, see Prebuilt UTDs.
2. */* is not supported.
parameters Record No Custom parameters that are defined by the system and assigned values by developers as required. For details, see Table 2.
flags number No Processing mode. For details, see Table 3.

Table 2 Description of parameters

Parameter Type Description
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.
ohos.ability.params.showDefaultPicker boolean Whether to forcibly display the dialog box for selecting the file opening mode. The default value is false.
- false: The display of the dialog box is determined by the system policy or default application settings.
- true: Such a dialog box is always displayed.
showCaller boolean Whether the caller application is involved in the matching of the target applications. The default value is false.
- false: not involved in the matching.
- true: involved in the matching.

Table 3 Description of flags

Parameter Value Description
FLAG_AUTH_READ_URI_PERMISSION 0x00000001 Grants the permission to read the URI.
FLAG_AUTH_WRITE_URI_PERMISSION 0x00000002 Grants the permission to write data to the URI.

How to Develop

Procedure for the Caller Application

  1. Import the required modules.

    // xxx.ets
    import { fileUri } from '@kit.CoreFileKit';
    import { UIAbility, Want, common, wantConstant } from '@kit.AbilityKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { window } from '@kit.ArkUI';
    
  2. Obtain the application file paths.

    // xxx.ets
    // Assume that the bundle name is com.example.demo.
    export default class EntryAbility extends UIAbility {
        onWindowStageCreate(windowStage: window.WindowStage) {
            // Obtain the application sandbox path of the file.
            let filePath = this.context.filesDir + '/test1.txt';
            // Convert the application sandbox path into a URI.
            let uri = fileUri.getUriFromPath(filePath);
            // The obtained URI is file://com.example.demo/data/storage/el2/base/files/test.txt.
        }
        // ...
    }
    
  3. Construct request data.

    // xxx.ets
    export default class EntryAbility extends UIAbility {
        onWindowStageCreate(windowStage: window.WindowStage) {
            // Obtain the application sandbox path of the file.
            let filePath = this.context.filesDir + '/test.txt';
            // Convert the application sandbox path into a URI.
            let uri = fileUri.getUriFromPath(filePath);
            // Construct the request data.
            let want: Want = {
            action: 'ohos.want.action.viewData', // Action of viewing data. The value is fixed at this value in the file opening scenario.
            uri: uri,
            type: 'general.plain-text', // Type of the file to open.
            // Grant the read and write permissions on the file to the target application.
            flags: wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION|wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION
            };
        }
        // ...
    }
    
  4. Call the API to start the target application.

    // xxx.ets
    export default class EntryAbility extends UIAbility {
        onWindowStageCreate(windowStage: window.WindowStage) {
            // Obtain the application sandbox path of the file.
            let filePath = this.context.filesDir + '/test.txt';
            // Convert the application sandbox path into a URI.
            let uri = fileUri.getUriFromPath(filePath);
            // Construct the request data.
            let want: Want = {
            action: 'ohos.want.action.viewData', // Action of viewing data. The value is fixed at this value in the file opening scenario.
            uri: uri,
            type: 'general.plain-text', // Type of the file to open.
            // Grant the read and write permissions on the file to the target application.
            flags: wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION|wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION
            };
            // Call startAbility.
            this.context.startAbility(want)
            .then(() => {
                console.info('Succeed to invoke startAbility.');
            })
            .catch((err: BusinessError) => {
                console.error(`Failed to invoke startAbility, code: ${err.code}, message: ${err.message}`);
            });
        }
        // ...
    }
    

Procedure for the Target Application

  1. Declare the capability to open files.

    Applications that are able to open files must declare the file opening capability in the module.json5 file. The uris field indicates the type of the URIs, and the scheme field is fixed at file. The type field indicates the type of file that can be opened. For details, see UTDs (recommended) or MIME type. In the following example, the file type is .txt.

    {
    "module": {
        // ...
        "abilities": [
        {
            // ...
            "skills": [
            {
                "actions": [
                "ohos.want.action.viewData" // Mandatory. Declare the data processing capability.
                ],
                "uris": [
                {
                    // Declare the capability of opening local files whose URIs start with file://.
                    "scheme": "file", // Mandatory. Declare that the scheme type is file.
                    "type": "general.plain-text", // Mandatory. Specify the type of file that can be opened.
                    "linkFeature": "FileOpen" // Mandatory and case sensitive. Specify that the URI is used to open files.
                }
                // ...
                ]
                // ...
            }
            ]
        }
        ]
    }
    }
    
  2. Process the file.

    After the target application is started and obtains the URI of the file to open from the want information, it opens the file and obtains the corresponding file object for reading and writing.

    // xxx.ets
    import fs from '@ohos.file.fs';
    import { Want, AbilityConstant } from '@kit.AbilityKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    
    export default class EntryAbility extends UIAbility {
        onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
            // Obtain the uri field from the want information.
            let uri = want.uri;
            if (uri == null||uri == undefined) {
                console.info('uri is invalid');
                return;
            }
            try {
                // Perform operations on the URI of the file as required. For example, open the URI to obtain the file object in read/write mode.
                let file = fs.openSync(uri, fs.OpenMode.READ_WRITE);
                console.info('Succeed to open file.');
            } catch (err) {
                let error: BusinessError = err as BusinessError;
                console.error(`Failed to open file openSync, code: ${error.code}, message: ${error.message}`);
            }
        }
    }
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Ability Kit

harmony 鸿蒙Obtaining Reasons for Abnormal Application Exits

harmony 鸿蒙UIAbility Backup and Restore

harmony 鸿蒙Using Explicit Want to Start an Application Component

harmony 鸿蒙Introduction to Ability Kit

harmony 鸿蒙AbilityStage Component Container

harmony 鸿蒙Accessing a DataAbility

harmony 鸿蒙Accessing a DataShareExtensionAbility from the FA Model

harmony 鸿蒙Common action and entities Values (Not Recommended)

harmony 鸿蒙API Switching Overview

0  赞