如何实现通过调用其他已安装的应用来打开特定文件
=======================

开发者可以通过使用隐式Want机制来调用其他应用打开文件。通过设置合适的携带的数据（uri）、MIME type类型（type）、处理Want的方式（flag）等字段，以便系统能够识别并弹出一个选择框，让用户选择合适的应用来打开文件，若仅匹配到一个应用，则会直接拉起该应用。

效果示意如下图所示：

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/f4/v3/S5HkdmiES6SEoHWVMcow8w/zh-cn_image_0000002624635778.jpg?HW-CC-KV=V1&HW-CC-Date=20260709T133816Z&HW-CC-Expire=86400&HW-CC-Sign=2CB958C73BE1E4DC5B4ED01ECBB97FD741CF60763FA673761764D53CE718AA0D "点击放大")

**调用方**

1.  导入所需模块。
    
    import common from '@ohos.app.ability.common';
    import Want from '@ohos.app.ability.Want';
    import wantConstant from '@ohos.app.ability.wantConstant';
    import { BusinessError } from '@ohos.base';
    
2.  构造请求数据。
    
    _// Construct request data Want, taking opening a Word file as an example_
    let wantInfo: Want = {
      uri: 'file://.../test.docx', _// Indicate the URI path of the file to be opened, usually used in conjunction with type_
      type: 'application/msword', _// Indicate the type of file to be opened_
      flags: wantConstant.Flags.FLAG\_AUTH\_WRITE\_URI\_PERMISSION, _// Authorization to perform write operations on URI_
    }
    
3.  调用接口启动。
    
    _// Call the startAbility interface to open files_
    let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
    context.startAbility(wantInfo).then(() => {
      _// ..._
    }).catch((err: BusinessError) => {
      _// ..._
    })
    

**目标方**

在[module.json5配置文件](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/module-configuration-file)中注册文件打开能力。

{
  "module": {
    _// ..._
    "abilities": \[
      {
        _// ..._
        "skills": \[
          {
            "actions": \[
              "ohos.want.action.viewData" _// Declaration of Data Processing Capability_
            \],
            "uris": \[
              {
                _// Allow opening Word files in URI that start with the file://protocol to identify the local file_
                "scheme": "file",
                "type": "application/msword", _// Indicates supported file types for opening_
                "linkFeature": "FileOpen" _// The function of this URI is to open files_
              }
              _// ..._
            \]
            _// ..._
          }
        \]
      }
    \]
  }
}

目标方应用在应用市场上架时会进行审核。

**参考链接**

[隐式Want匹配原理](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/explicit-implicit-want-mappings#隐式want匹配原理)