# @ohos.fileshare (File Sharing)
The **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.
> **NOTE**
>
> 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.
## Modules to Import
```ts
import fileShare from '@ohos.fileshare';
```
## OperationMode11+
Enumerates the modes for accessing a URI.
**System capability**: SystemCapability.FileManagement.AppFileService.FolderAuthorization
| Name | Value | Description |
| ----- |-----|-----|
| READ_MODE | 0b1 | Read.|
| WRITE_MODE | 0b10 | Write.|
## PolicyErrorCode11+
Enumerates the error codes for a permission policy.
**System capability**: SystemCapability.FileManagement.AppFileService.FolderAuthorization
| Name | Value | Description |
| ----- |-----|-----------|
| PERSISTENCE_FORBIDDEN | 1 | The permission on the URI cannot be persisted. |
| INVALID_MODE | 2 | Invalid mode. |
| INVALID_PATH | 3 | Invalid path. |
## PolicyErrorResult11+
Represents the detailed permission policy error information, which can be used when **persistPermission**, **revokePermission**, **activatePermission**, or **deactivatePermission** throws an error.
**System capability**: SystemCapability.FileManagement.AppFileService.FolderAuthorization
| Name | Type | Description |
|---------|--------------------------------------|-------------------|
| uri | string | URI of the file, on which the permission fails to be granted or activated. |
| code | [PolicyErrorCode](#policyerrorcode11) | Error code.|
| message | string | Cause of the error. |
## PolicyInfo11+
Represents a permission policy, that is, a policy for granting or activating the permission on a file.
**System capability**: SystemCapability.FileManagement.AppFileService.FolderAuthorization
| Name | Type | Mandatory | Description |
|---------------| ---------|-----|---------------------------------------------------------------------------------------------------------------------------------------------------|
| uri | string | Yes | URI of the file. |
| operationMode | number | Yes | Mode for accessing the URI. For details, see [OperationMode](#operationmode11).
For example, **fileShare.OperationMode.READ_MODE** indicates the read permission on the file.
**fileShare.OperationMode.READ_MODE\|fileShare.OperationMode.WRITE_MODE** indicates the read and write permissions on the file. |
## fileShare.grantUriPermission
grantUriPermission(uri: string, bundleName: string, flag: wantConstant.Flags, callback: AsyncCallback<void>): void
Grants the permissions on a user file to an application. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.WRITE_MEDIA
**System API**: This is a system API.
**System capability**: SystemCapability.FileManagement.AppFileService
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | -------------------------- |
| uri | string | Yes | URI of the file. |
| bundleName | string | Yes | Application to be grated with the permissions. |
| flag | wantConstant.Flags | Yes | Permissions to grant. For details, see [wantConstant.Flags](js-apis-app-ability-wantConstant.md#wantconstantflags).
**wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION**: permission to read the file.
**wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION**: permission to write the file.|
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. |
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed |
| 202 | The caller is not a system application |
| 401 | The input parameter is invalid |
| 143000001 | IPC error |
**Example**
```ts
import wantConstant from '@ohos.app.ability.wantConstant';
import { BusinessError } from '@ohos.base';
let uri: string = 'file://media/image/8';
let bundleName: string = 'com.demo.test';
try {
fileShare.grantUriPermission(uri, bundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION |
wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION, (err: BusinessError) => {
if (err) {
console.error("grantUriPermission failed with error: " + JSON.stringify(err));
return;
}
console.info("grantUriPermission success!");
});
} catch (err) {
let error: BusinessError = err as BusinessError;
console.error("grantUriPermission failed with error:" + JSON.stringify(error));
}
```
## fileShare.grantUriPermission
grantUriPermission(uri: string, bundleName: string, flag: wantConstant.Flags): Promise<void>
Grants the permissions on a user file to an application. This API uses a promise to return the result.
**Required permissions**: ohos.permission.WRITE_MEDIA
**System API**: This is a system API.
**System capability**: SystemCapability.FileManagement.AppFileService
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ------ | ---- | -------------------------- |
| uri | string | Yes | URI of the file. |
| bundleName | string | Yes | Application to be grated with the permissions. |
| flag | wantConstant.Flags | Yes | Permissions to grant. For details, see [wantConstant.Flags](js-apis-app-ability-wantConstant.md#wantconstantflags).
**wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION**: permission to read the file.
**wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION**: permission to write the file.|
**Return value**
| Type | Description |
| ---------------------------- | ---------- |
| Promise<void> | Promise that returns no value.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed |
| 202 | The caller is not a system application |
| 401 | The input parameter is invalid |
| 143000001 | IPC error |
**Example**
```ts
import wantConstant from '@ohos.app.ability.wantConstant';
import { BusinessError } from '@ohos.base';
let uri: string = 'file://media/image/8';
let bundleName: string = 'com.demo.test';
try {
fileShare.grantUriPermission(uri, bundleName, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION |
wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION).then(() => {
console.info("grantUriPermission success!");
}).catch((error: BusinessError) => {
console.error("grantUriPermission failed with error:" + JSON.stringify(error));
});
} catch (err) {
let error: BusinessError = err as BusinessError;
console.error("grantUriPermission failed with error:" + JSON.stringify(error));
}
```
## fileShare.persistPermission11+
persistPermission(policies: Array<PolicyInfo>): Promise<void>
Persists the permissions granted to multiple files or folders. This API uses a promise to return the result.
This API is available only to certain devices.
**Required permissions**: ohos.permission.FILE_ACCESS_PERSIST
**System capability**: SystemCapability.FileManagement.AppFileService.FolderAuthorization
**Parameters**
| Name| Type | Mandatory| Description |
| -------- |---------------------------------------| -------- |-------------------------|
| policies| Array<[PolicyInfo](#policyinfo11)> | Yes| Permissions to persist. |
**Return value**
| Type| Description|
| -------- | -------- |
| Promise<void> | Promise that returns no value.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
If the permission persistence of some URIs fails, error code 13900001 will be returned and the **data** field provides error information of these URIs in the Array<[PolicyErrorResult](#policyerrorresult11)> format.
| ID | Error Message |
|----------| --------- |
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken.|
| 401 | Parameter error. |
| 801 | Capability not supported. |
| 13900001 | Operation not permitted. |
| 13900042 | Unknown error |
**Example**
```ts
import { BusinessError } from '@ohos.base';
import picker from '@ohos.file.picker';
async function persistPermissionExample() {
try {
let DocumentSelectOptions = new picker.DocumentSelectOptions();
let documentPicker = new picker.DocumentViewPicker();
let uris = await documentPicker.select(DocumentSelectOptions);
let policyInfo: fileShare.PolicyInfo = {
uri: uris[0],
operationMode: fileShare.OperationMode.READ_MODE,
};
let policies: Array = [policyInfo];
fileShare.persistPermission(policies).then(() => {
console.info("persistPermission successfully");
}).catch((err: BusinessError>) => {
console.info("persistPermission failed with error message: " + err.message + ", error code: " + err.code);
if (err.code == 13900001 && err.data) {
for (let i = 0; i < err.data.length; i++) {
console.log("error code : " + JSON.stringify(err.data[i].code));
console.log("error uri : " + JSON.stringify(err.data[i].uri));
console.log("error reason : " + JSON.stringify(err.data[i].message));
}
}
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('persistPermission failed with err: ' + JSON.stringify(err));
}
}
```
## fileShare.revokePermission11+
revokePermission(policies: Array<PolicyInfo>): Promise<void>
Revokes permissions from multiple files or folders. This API uses a promise to return the result.
This API is available only to certain devices.
**Required permissions**: ohos.permission.FILE_ACCESS_PERSIST
**System capability**: SystemCapability.FileManagement.AppFileService.FolderAuthorization
**Parameters**
| Name| Type | Mandatory| Description |
| -------- |--------------------| -------- |-------------------------|
| policies| Array<[PolicyInfo](#policyinfo11)> | Yes| Permissions to revoke. |
**Return value**
| Type| Description|
| -------- | -------- |
| Promise<void> | Promise that returns no value.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
If the permission revocation of some URIs fails, error code 13900001 will be returned and the **data** field provides error information of these URIs in the Array<[PolicyErrorResult](#policyerrorresult11)> format.
| ID | Error Message |
|----------| --------- |
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken.|
| 401 | Parameter error. |
| 801 | Capability not supported. |
| 13900001 | Operation not permitted. |
| 13900042 | Unknown error |
**Example**
```ts
import { BusinessError } from '@ohos.base';
import picker from '@ohos.file.picker';
async function revokePermissionExample() {
try {
let DocumentSelectOptions = new picker.DocumentSelectOptions();
let documentPicker = new picker.DocumentViewPicker();
let uris = await documentPicker.select(DocumentSelectOptions);
let policyInfo: fileShare.PolicyInfo = {
uri: uris[0],
operationMode: fileShare.OperationMode.READ_MODE,
};
let policies: Array = [policyInfo];
fileShare.revokePermission(policies).then(() => {
console.info("revokePermission successfully");
}).catch((err: BusinessError>) => {
console.info("revokePermission failed with error message: " + err.message + ", error code: " + err.code);
if (err.code == 13900001 && err.data) {
for (let i = 0; i < err.data.length; i++) {
console.log("error code : " + JSON.stringify(err.data[i].code));
console.log("error uri : " + JSON.stringify(err.data[i].uri));
console.log("error reason : " + JSON.stringify(err.data[i].message));
}
}
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('revokePermission failed with err: ' + JSON.stringify(err));
}
}
```
## fileShare.activatePermission11+
activatePermission(policies: Array<PolicyInfo>): Promise<void>
Activates the permissions that have been persisted on multiple files or folders. This API uses a promise to return the result.
This API is available only to certain devices.
**Required permissions**: ohos.permission.FILE_ACCESS_PERSIST
**System capability**: SystemCapability.FileManagement.AppFileService.FolderAuthorization
**Parameters**
| Name| Type| Mandatory| Description |
| -------- | -------- | -------- |-------------------------|
| policies| Array<[PolicyInfo](#policyinfo11)> | Yes| Permissions to activate. |
**Return value**
| Type| Description|
| -------- | -------- |
| Promise<void> | Promise that returns no value.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
If the permission activation of some URIs fails, error code 13900001 will be returned and the **data** field provides error information of these URIs in the Array<[PolicyErrorResult](#policyerrorresult11)> format.
| ID | Error Message |
|----------| --------- |
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken.|
| 401 | Parameter error. |
| 801 | Capability not supported. |
| 13900001 | Operation not permitted. |
| 13900042 | Unknown error |
**Example**
```ts
import { BusinessError } from '@ohos.base';
import picker from '@ohos.file.picker';
async function activatePermissionExample() {
try {
let uri = "file://docs/storage/Users/username/tmp.txt";
let policyInfo: fileShare.PolicyInfo = {
uri: uri,
operationMode: fileShare.OperationMode.READ_MODE,
};
let policies: Array = [policyInfo];
fileShare.activatePermission(policies).then(() => {
console.info("activatePermission successfully");
}).catch((err: BusinessError>) => {
console.info("activatePermission failed with error message: " + err.message + ", error code: " + err.code);
if (err.code == 13900001 && err.data) {
for (let i = 0; i < err.data.length; i++) {
console.log("error code : " + JSON.stringify(err.data[i].code));
console.log("error uri : " + JSON.stringify(err.data[i].uri));
console.log("error reason : " + JSON.stringify(err.data[i].message));
}
}
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('activatePermission failed with err: ' + JSON.stringify(err));
}
}
```
## fileShare.deactivatePermission11+
deactivatePermission(policies: Array<PolicyInfo>): Promise<void>
Deactivates the permissions on multiple files or folders. This API uses a promise to return the result.
This API is available only to certain devices.
**Required permissions**: ohos.permission.FILE_ACCESS_PERSIST
**System capability**: SystemCapability.FileManagement.AppFileService.FolderAuthorization
**Parameters**
| Name| Type| Mandatory| Description |
| -------- | -------- | -------- |-------------------------|
| policies| Array<[PolicyInfo](#policyinfo11)> | Yes| Permissions to deactivate. |
**Return value**
| Type| Description|
| -------- | -------- |
| Promise<void> | Promise that returns no value.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
If 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](#policyerrorresult11)> format.
| ID | Error Message |
|----------| --------- |
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken.|
| 401 | Parameter error. |
| 801 | Capability not supported. |
| 13900001 | Operation not permitted. |
| 13900042 | Unknown error |
**Example**
```ts
import { BusinessError } from '@ohos.base';
import picker from '@ohos.file.picker';
async function deactivatePermissionExample() {
try {
let uri = "file://docs/storage/Users/username/tmp.txt";
let policyInfo: fileShare.PolicyInfo = {
uri: uri,
operationMode: fileShare.OperationMode.READ_MODE,
};
let policies: Array = [policyInfo];
fileShare.deactivatePermission(policies).then(() => {
console.info("deactivatePermission successfully");
}).catch((err: BusinessError>) => {
console.info("deactivatePermission failed with error message: " + err.message + ", error code: " + err.code);
if (err.code == 13900001 && err.data) {
for (let i = 0; i < err.data.length; i++) {
console.log("error code : " + JSON.stringify(err.data[i].code));
console.log("error uri : " + JSON.stringify(err.data[i].uri));
console.log("error reason : " + JSON.stringify(err.data[i].message));
}
}
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('deactivatePermission failed with err: ' + JSON.stringify(err));
}
}
```