share-app-file.md 6.9 KB
Newer Older
A
Annie_wang 已提交
1 2
# Sharing an Application File

A
Annie_wang 已提交
3
An application can share its file with another application based on the file descriptor (FD) or uniform resource identifier (URI) of the file. However, if the FD of a shared file is closed, the file is no longer opened. Therefore, the FD-based file sharing is not recommended. This section describes how to share an application file based on its URI.
A
Annie_wang 已提交
4

A
Annie_wang 已提交
5
- You can use [wantConstant.Flags](../reference/apis/js-apis-app-ability-wantConstant.md#wantconstantflags) to share a single application file in read or read/write mode based on its URI with another application. The target application can use **open()** of the [ohos.file.fs](../reference/apis/js-apis-file-fs.md#fsopen) module to open the URI and then perform read and/or write operations based on the permissions granted. OpenHarmony API version 9 supports only temporary authorization. The permission on the shared file is revoked once the target application exits.
A
Annie_wang 已提交
6

A
Annie_wang 已提交
7
- You can also use **open()** of the ohos.file.fs module to share a single application file with the specified permissions to another application based on the FD. After parsing the FD from **Want**, the target application can read and write the file by using **read()** and **write()** APIs of ohos.file.fs.
A
Annie_wang 已提交
8 9 10 11 12 13 14

You can use the related APIs to [share a file with another application](#sharing-a-file-with-another-application) or [use shared application files](#using-shared-files).

## File URI Specifications

The file URIs are in the following format:

A
Annie_wang 已提交
15
**file://**<bundleName>/<path>
A
Annie_wang 已提交
16 17 18 19 20 21 22 23 24

- **file**: indicates a file URI.

- *bundleName*: specifies the owner of the file.

- *path*: specifies the application sandbox path of the file.

## Sharing a File with Another Application

A
Annie_wang 已提交
25
Before sharing application files, you need to [obtain the application file path](../application-models/application-context-stage.md#obtaining-application-file-paths).
A
Annie_wang 已提交
26 27 28 29 30 31 32

1. Obtain the application sandbox path of the file and convert it into the file URI.

   ```ts
   import UIAbility from '@ohos.app.ability.UIAbility';
   import fileuri from '@ohos.file.fileuri';
   import window from '@ohos.window';
A
Annie_wang 已提交
33

A
Annie_wang 已提交
34 35 36 37 38 39 40 41 42 43 44 45
   export default class EntryAbility extends UIAbility {
     onWindowStageCreate(windowStage: window.WindowStage) {
       // Obtain the application sandbox path of the file.
       let pathInSandbox = this.context.filesDir + "/test.txt";
       // Convert the application sandbox path into a URI.
       let uri = fileuri.getUriFromPath(pathInSandbox);
       // The obtained URI is file://com.example.demo/data/storage/el2/base/files/test.txt.
     }
   }
   ```

2. Set the target application, with which you want to share the file, and grant permissions on the file.
A
Annie_wang 已提交
46

A
Annie_wang 已提交
47
   Use [startAbility()](../reference/apis/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) to share the file with the target application. You need to pass in the obtained URI in **uri** of the **want** parameter, set the type of the file to share, set **action** to **ohos.want.action.sendData**, and set the granted permission on the file in **flags**. For details, see [Want](../reference/apis/js-apis-app-ability-want.md#attributes).
A
Annie_wang 已提交
48 49 50 51 52 53 54

   > **NOTE**
   >
   > The write permission granted includes the read permission.

   ```ts
   import fileuri from '@ohos.file.fileuri';
A
Annie_wang 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
      import window from '@ohos.window';
      import wantConstant from '@ohos.app.ability.wantConstant';
      import UIAbility from '@ohos.app.ability.UIAbility';
      import Want from '@ohos.app.ability.Want';
      import { BusinessError } from '@ohos.base';

      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);
          let want: Want  = {
            // Grant the read and write permissions on the shared file to the target application.
            flags: wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION | wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
            // Set the implicit startup rule for the application that shares the file.
            action: 'ohos.want.action.sendData',
            uri: uri,
            type: 'text/plain'
          }
          this.context.startAbility(want)
            .then(() => {
              console.info('Invoke getCurrentBundleStats succeeded.');
            })
            .catch((err: BusinessError) => {
              console.error(`Invoke startAbility failed, code is ${err.code}, message is ${err.message}`);
            });
        }
        // ...
      }
A
Annie_wang 已提交
85 86
   ```

A
Annie_wang 已提交
87

A
Annie_wang 已提交
88 89 90
## Using Shared Files

In the [**module.json5** file](../quick-start/module-configuration-file.md) of the application, which wants to use the shared file, set **actions** to **ohos.want.action.sendData** to allow the application to receive files shared by another application and set **uris** to the type of the URI to receive. In the following example, the application receives only .txt files with **scheme** of **file**.
A
Annie_wang 已提交
91

A
Annie_wang 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
```json
{
  "module": {
    ...
    "abilities": [
      {
        ...
        "skills": [
          {
            ...
            "actions": [
              "ohos.want.action.sendData"
            ],
            "uris": [
              {
                "scheme": "file",
                "type": "text/plain"
              }
           ]
          }
        ]
      }
    ]
  }
}
```

After **UIAbility** of the application starts, the application obtains **want** information from [**onCreate()**](../reference/apis/js-apis-app-ability-uiAbility.md#uiabilityoncreate) or [**onNewWant()**](../reference/apis/js-apis-app-ability-uiAbility.md#uiabilityonnewwant).

A
Annie_wang 已提交
121
After obtaining the URI of the shared file from **want**, the application can call **fs.open()** to open the file, and then read and write the file after obtaining the related file object.
A
Annie_wang 已提交
122 123 124 125

```ts
// xxx.ets
import fs from '@ohos.file.fs';
A
Annie_wang 已提交
126 127
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
A
Annie_wang 已提交
128 129 130

function getShareFile() {
  try {
A
Annie_wang 已提交
131
    let want: Want = ...; // Obtain the want sent from the application that shares the file.
A
Annie_wang 已提交
132 133 134 135 136 137 138 139 140 141 142

    // 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 shared 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('open file successfully!');
A
Annie_wang 已提交
143 144
    } catch (err) {
      let error: BusinessError = err as BusinessError;
A
Annie_wang 已提交
145 146 147
      console.error(`Invoke openSync failed, code is ${error.code}, message is ${error.message}`);
    }
  } catch (error) {
A
Annie_wang 已提交
148 149
    let err: BusinessError = error as BusinessError;
    console.error(`Invoke openSync failed, code is ${err.code}, message is ${err.message}`);
A
Annie_wang 已提交
150 151 152
  }
}
```