1e41f4b71Sopenharmony_ci# @ohos.fileshare (文件分享)(系统接口)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci该模块提供文件分享能力,提供系统应用将公共目录文件统一资源标志符(Uniform Resource Identifier,URI)以读写权限授权给其他应用的接口,授权后应用可通过[@ohos.file.fs](js-apis-file-fs.md)的相关接口进行相关open、read、write等操作,实现文件分享。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8e41f4b71Sopenharmony_ci> - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.fileshare (文件分享)](js-apis-fileShare-sys.md)。
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## 导入模块
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci```ts
13e41f4b71Sopenharmony_ciimport  { fileShare } from '@kit.CoreFileKit';
14e41f4b71Sopenharmony_ci```
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci## fileShare.grantUriPermission
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_cigrantUriPermission(uri: string, bundleName: string, flag: wantConstant.Flags, callback: AsyncCallback<void>): void
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci对公共目录文件URI进行授权操作,使用callback异步回调。  
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.WRITE_MEDIA  
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci**系统接口**:此接口为系统接口  
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.AppFileService
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci**参数:**
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                       |
31e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- |
32e41f4b71Sopenharmony_ci| uri   | string | 是   | 公共目录文件URI |
33e41f4b71Sopenharmony_ci| bundleName   | string | 是   | 分享目标的包名 |
34e41f4b71Sopenharmony_ci| flag   | [wantConstant.Flags](../apis-ability-kit/js-apis-app-ability-wantConstant.md#wantconstantflags) | 是   | 授权的权限。<br/>wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION:读授权<br/>wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION:写授权|
35e41f4b71Sopenharmony_ci | callback | AsyncCallback&lt;void&gt;  | 是    | 异步授权之后的回调                             |
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci**错误码:**
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci| 错误码ID                     | 错误信息        |
42e41f4b71Sopenharmony_ci| ---------------------------- | ---------- |
43e41f4b71Sopenharmony_ci| 201 | Permission verification failed |
44e41f4b71Sopenharmony_ci| 202 | The caller is not a system application |
45e41f4b71Sopenharmony_ci| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types |
46e41f4b71Sopenharmony_ci| 143000001 | IPC error |
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci**示例:**
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci  ```ts
51e41f4b71Sopenharmony_ci  import { wantConstant } from '@kit.AbilityKit';
52e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
53e41f4b71Sopenharmony_ci  let uri: string = 'file://media/image/8';
54e41f4b71Sopenharmony_ci  let bundleName: string = 'com.demo.test';
55e41f4b71Sopenharmony_ci  try {
56e41f4b71Sopenharmony_ci    fileShare.grantUriPermission(uri, bundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION |
57e41f4b71Sopenharmony_ci      wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION, (err: BusinessError) => {
58e41f4b71Sopenharmony_ci      if (err) {
59e41f4b71Sopenharmony_ci        console.error("grantUriPermission failed with error: " + JSON.stringify(err));
60e41f4b71Sopenharmony_ci        return;
61e41f4b71Sopenharmony_ci      }
62e41f4b71Sopenharmony_ci      console.info("grantUriPermission success!");
63e41f4b71Sopenharmony_ci    });
64e41f4b71Sopenharmony_ci  } catch (err) {
65e41f4b71Sopenharmony_ci    let error: BusinessError = err as BusinessError;
66e41f4b71Sopenharmony_ci    console.error("grantUriPermission failed with error:" + JSON.stringify(error));
67e41f4b71Sopenharmony_ci  }
68e41f4b71Sopenharmony_ci  ```
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci## fileShare.grantUriPermission
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_cigrantUriPermission(uri: string, bundleName: string, flag: wantConstant.Flags): Promise&lt;void&gt;
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci将公共目录文件URI进行授权操作,使用Promise异步回调。  
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.WRITE_MEDIA  
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci**系统接口**:此接口为系统接口  
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.FileManagement.AppFileService  
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci**参数:**
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci| 参数名 | 类型   | 必填 | 说明                       |
85e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- |
86e41f4b71Sopenharmony_ci| uri   | string | 是   | 公共目录文件URI |
87e41f4b71Sopenharmony_ci| bundleName   | string | 是   | 分享目标的包名 |
88e41f4b71Sopenharmony_ci| flag   | [wantConstant.Flags](../apis-ability-kit/js-apis-app-ability-wantConstant.md#wantconstantflags) | 是   | 授权的权限。<br/>wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION:读授权<br/>wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION:写授权|
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci**返回值:**
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci  | 类型                           | 说明         |
93e41f4b71Sopenharmony_ci  | ---------------------------- | ---------- |
94e41f4b71Sopenharmony_ci  | Promise&lt;void&gt; | Promise对象,无返回值 |
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci**错误码:**
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci| 错误码ID                     | 错误信息        |
101e41f4b71Sopenharmony_ci| ---------------------------- | ---------- |
102e41f4b71Sopenharmony_ci| 201 | Permission verification failed |
103e41f4b71Sopenharmony_ci| 202 | The caller is not a system application |
104e41f4b71Sopenharmony_ci| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types |
105e41f4b71Sopenharmony_ci| 143000001 | IPC error |
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci**示例:**
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci  ```ts
110e41f4b71Sopenharmony_ci  import { wantConstant } from '@kit.AbilityKit';
111e41f4b71Sopenharmony_ci  import { BusinessError } from '@kit.BasicServicesKit';
112e41f4b71Sopenharmony_ci  let uri: string = 'file://media/image/8';
113e41f4b71Sopenharmony_ci  let bundleName: string = 'com.demo.test';
114e41f4b71Sopenharmony_ci  try {
115e41f4b71Sopenharmony_ci    fileShare.grantUriPermission(uri, bundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION |
116e41f4b71Sopenharmony_ci      wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION).then(() => {
117e41f4b71Sopenharmony_ci      console.info("grantUriPermission success!");
118e41f4b71Sopenharmony_ci    }).catch((error: BusinessError) => {
119e41f4b71Sopenharmony_ci      console.error("grantUriPermission failed with error:" + JSON.stringify(error));
120e41f4b71Sopenharmony_ci    });
121e41f4b71Sopenharmony_ci  } catch (err) {
122e41f4b71Sopenharmony_ci    let error: BusinessError = err as BusinessError;
123e41f4b71Sopenharmony_ci    console.error("grantUriPermission failed with error:" + JSON.stringify(error));
124e41f4b71Sopenharmony_ci  }
125e41f4b71Sopenharmony_ci  ```
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_cideactivatePermission(policies: Array&lt;PolicyInfo>): Promise&lt;void&gt;
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci异步方法取消使能授权过的多个文件或目录,以promise形式返回结果,该接口仅对具有该系统能力的设备开放。
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.FILE_ACCESS_PERSIST
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.FileManagement.AppFileService.FolderAuthorization
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci**参数:**
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明                      |
139e41f4b71Sopenharmony_ci| -------- | -------- | -------- |-------------------------|
140e41f4b71Sopenharmony_ci| policies| Array&lt;[PolicyInfo](js-apis-fileShare.md#policyinfo11)> | 是 | 需要授权URI的策略信息。           |
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci**返回值:**
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci| 类型 | 说明 |
145e41f4b71Sopenharmony_ci| -------- | -------- |
146e41f4b71Sopenharmony_ci| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci**错误码:**
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。
151e41f4b71Sopenharmony_ci如果存在URI取消使能权限失败,则抛出13900001错误码,且失败URI信息将抛出异常data属性中以Array<[PolicyErrorResult](js-apis-fileShare.md#policyerrorresult11)>形式提供错误信息。
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci| 错误码ID    | 错误信息       |
154e41f4b71Sopenharmony_ci|----------| --------- |
155e41f4b71Sopenharmony_ci| 201      | Permission verification failed, usually the result returned by VerifyAccessToken.|
156e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
157e41f4b71Sopenharmony_ci| 801      | Capability not supported. |
158e41f4b71Sopenharmony_ci| 13900001 | Operation not permitted.            |
159e41f4b71Sopenharmony_ci| 13900042 | Unknown error                          |
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci**示例:**
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci  ```ts
164e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
165e41f4b71Sopenharmony_ciimport  { picker } from '@kit.CoreFileKit';
166e41f4b71Sopenharmony_ci  
167e41f4b71Sopenharmony_ci  async function deactivatePermissionExample() {
168e41f4b71Sopenharmony_ci    try {
169e41f4b71Sopenharmony_ci      let uri = "file://docs/storage/Users/username/tmp.txt";
170e41f4b71Sopenharmony_ci      let policyInfo: fileShare.PolicyInfo = {
171e41f4b71Sopenharmony_ci        uri: uri,
172e41f4b71Sopenharmony_ci        operationMode: fileShare.OperationMode.READ_MODE,
173e41f4b71Sopenharmony_ci      };
174e41f4b71Sopenharmony_ci      let policies: Array<fileShare.PolicyInfo> = [policyInfo];
175e41f4b71Sopenharmony_ci      fileShare.deactivatePermission(policies).then(() => {
176e41f4b71Sopenharmony_ci        console.info("deactivatePermission successfully");
177e41f4b71Sopenharmony_ci      }).catch((err: BusinessError<Array<fileShare.PolicyErrorResult>>) => {
178e41f4b71Sopenharmony_ci        console.info("deactivatePermission failed with error message: " + err.message + ", error code: " + err.code);
179e41f4b71Sopenharmony_ci          if (err.code == 13900001 && err.data) {
180e41f4b71Sopenharmony_ci            for (let i = 0; i < err.data.length; i++) {
181e41f4b71Sopenharmony_ci              console.log("error code : " + JSON.stringify(err.data[i].code));
182e41f4b71Sopenharmony_ci              console.log("error uri : " + JSON.stringify(err.data[i].uri));
183e41f4b71Sopenharmony_ci              console.log("error reason : " + JSON.stringify(err.data[i].message));
184e41f4b71Sopenharmony_ci            }
185e41f4b71Sopenharmony_ci          }
186e41f4b71Sopenharmony_ci      });
187e41f4b71Sopenharmony_ci    } catch (error) {
188e41f4b71Sopenharmony_ci      let err: BusinessError = error as BusinessError;
189e41f4b71Sopenharmony_ci      console.error('deactivatePermission failed with err: ' + JSON.stringify(err));
190e41f4b71Sopenharmony_ci    }
191e41f4b71Sopenharmony_ci  }
192e41f4b71Sopenharmony_ci  ```