• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.fileshare (File Sharing)
2
3The **fileShare** module provides APIs for granting the access permissions on a user file to another application by the Uniform Resource Identifier (URI). Then, the authorized application can access the file by using the APIs provided by [@ohos.file.fs](js-apis-file-fs.md).
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```ts
12import fileShare from '@ohos.fileshare';
13```
14
15## fileShare.grantUriPermission
16
17grantUriPermission(uri: string, bundleName: string, flag: wantConstant.Flags, callback: AsyncCallback<void>): void
18
19Grants permissions on a user file by the URI to an application. This API uses an asynchronous callback to return the result.
20
21**Required permissions**: ohos.permission.WRITE_MEDIA
22
23**System API**: This is a system API.
24
25**System capability**: SystemCapability.FileManagement.AppFileService
26
27**Parameters**
28
29| Name| Type  | Mandatory| Description                      |
30| ------ | ------ | ---- | -------------------------- |
31| uri   | string | Yes  | URI of a user file.|
32| bundleName   | string | Yes  | Bundle name of the application to be grated with the permissions.|
33| flag   | wantConstant.Flags | Yes  | Permissions to grant. For details, see [wantConstant.Flags](js-apis-app-ability-wantConstant.md#wantconstantflags).<br>**wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION**: permission to read the file. <br>**wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION**: permission to write the file.|
34 | callback | AsyncCallback&lt;void&gt;  | Yes   | Callback invoked to return the result.                           |
35
36**Error codes**
37
38For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
39
40| ID                    | Error Message       |
41| ---------------------------- | ---------- |
42| 201 | Permission verification failed |
43| 202 | The caller is not a system application |
44| 401 | The input parameter is invalid |
45| 143000001 | IPC error |
46
47**Example**
48
49  ```ts
50  import wantConstant from '@ohos.app.ability.wantConstant';
51  import { BusinessError } from '@ohos.base';
52  let uri: string = 'file://media/image/8';
53  let bundleName: string = 'com.demo.test';
54  try {
55    fileShare.grantUriPermission(uri, bundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION |
56      wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION, (err: BusinessError) => {
57      if (err) {
58        console.error("grantUriPermission failed with error: " + JSON.stringify(err));
59        return;
60      }
61      console.info("grantUriPermission success!");
62    });
63  } catch (err) {
64    let error: BusinessError = err as BusinessError;
65    console.error("grantUriPermission failed with error:" + JSON.stringify(error));
66  }
67  ```
68
69## fileShare.grantUriPermission
70
71grantUriPermission(uri: string, bundleName: string, flag: wantConstant.Flags): Promise&lt;void&gt;
72
73Grants permissions on a user file by the URI to an application. This API uses a promise to return the result.
74
75**Required permissions**: ohos.permission.WRITE_MEDIA
76
77**System API**: This is a system API.
78
79**System capability**: SystemCapability.FileManagement.AppFileService
80
81**Parameters**
82
83| Name| Type  | Mandatory| Description                      |
84| ------ | ------ | ---- | -------------------------- |
85| uri   | string | Yes  | URI of a user file.|
86| bundleName   | string | Yes  | Bundle name of the application to be grated with the permissions.|
87| flag   | wantConstant.Flags | Yes  | Permissions to grant. For details, see [wantConstant.Flags](js-apis-app-ability-wantConstant.md#wantconstantflags).<br>**wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION**: permission to read the file. <br>**wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION**: permission to write the file.|
88
89**Return value**
90
91  | Type                          | Description        |
92  | ---------------------------- | ---------- |
93  | Promise&lt;void&gt; | Promise that returns no value.|
94
95**Error codes**
96
97For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
98
99| ID                    | Error Message       |
100| ---------------------------- | ---------- |
101| 201 | Permission verification failed |
102| 202 | The caller is not a system application |
103| 401 | The input parameter is invalid |
104| 143000001 | IPC error |
105
106**Example**
107
108  ```ts
109  import wantConstant from '@ohos.app.ability.wantConstant';
110  import { BusinessError } from '@ohos.base';
111  let uri: string = 'file://media/image/8';
112  let bundleName: string = 'com.demo.test';
113  try {
114    fileShare.grantUriPermission(uri, bundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION |
115      wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION).then(() => {
116      console.info("grantUriPermission success!");
117    }).catch((error: BusinessError) => {
118      console.error("grantUriPermission failed with error:" + JSON.stringify(error));
119    });
120  } catch (err) {
121    let error: BusinessError = err as BusinessError;
122    console.error("grantUriPermission failed with error:" + JSON.stringify(error));
123  }
124  ```
125