1e41f4b71Sopenharmony_ci# @ohos.fileshare (File Sharing) (System API) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **fileShare** module provides APIs for granting the access permissions on a user file to another application based on the file Uniform Resource Identifier (URI). Then, the authorized application can access the file by using the [@ohos.file.fs](js-apis-file-fs.md) APIs. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> - 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. 8e41f4b71Sopenharmony_ci> - This topic describes only the system APIs provided by the module. For details about its public APIs, see [@ohos.fileshare](js-apis-fileShare-sys.md). 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## Modules to Import 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_ciGrants the permissions on a user file to an application. This API uses an asynchronous callback to return the result. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.WRITE_MEDIA 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci**System API**: This is a system API. 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.AppFileService 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci**Parameters** 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 31e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- | 32e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the file.| 33e41f4b71Sopenharmony_ci| bundleName | string | Yes | Application to be grated with the permissions.| 34e41f4b71Sopenharmony_ci| flag | [wantConstant.Flags](../apis-ability-kit/js-apis-app-ability-wantConstant.md#wantconstantflags) | Yes | Permissions to grant.<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.| 35e41f4b71Sopenharmony_ci | callback | AsyncCallback<void> | Yes | Callback used to return the result. | 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci**Error codes** 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ciFor details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci| ID | Error Message | 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**Example** 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<void> 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ciGrants the permissions on a user file to an application. This API uses a promise to return the result. 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.WRITE_MEDIA 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci**System API**: This is a system API. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.AppFileService 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci**Parameters** 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 85e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- | 86e41f4b71Sopenharmony_ci| uri | string | Yes | URI of the file.| 87e41f4b71Sopenharmony_ci| bundleName | string | Yes | Application to be grated with the permissions.| 88e41f4b71Sopenharmony_ci| flag | [wantConstant.Flags](../apis-ability-kit/js-apis-app-ability-wantConstant.md#wantconstantflags) | Yes | Permissions to grant.<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.| 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci**Return value** 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci | Type | Description | 93e41f4b71Sopenharmony_ci | ---------------------------- | ---------- | 94e41f4b71Sopenharmony_ci | Promise<void> | Promise that returns no value.| 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci**Error codes** 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ciFor details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci| ID | Error Message | 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**Example** 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<PolicyInfo>): Promise<void> 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ciDeactivates the permissions on multiple files or folders. This API uses a promise to return the result. <br>This API is available only to the devices that have the required system capability. 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.FILE_ACCESS_PERSIST 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.AppFileService.FolderAuthorization 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci**Parameters** 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 139e41f4b71Sopenharmony_ci| -------- | -------- | -------- |-------------------------| 140e41f4b71Sopenharmony_ci| policies| Array<[PolicyInfo](js-apis-fileShare.md#policyinfo11)> | Yes| Permissions to persist. | 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci**Return value** 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci| Type| Description| 145e41f4b71Sopenharmony_ci| -------- | -------- | 146e41f4b71Sopenharmony_ci| Promise<void> | Promise that returns no value.| 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci**Error codes** 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ciFor details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 151e41f4b71Sopenharmony_ciIf the permission deactivation of some URIs fails, error code 13900001 will be returned and the **data** field provides error information of these URIs in the Array<[PolicyErrorResult](js-apis-fileShare.md#policyerrorresult11)> format. 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci| ID | Error Message | 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**Example** 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 ``` 193