1e41f4b71Sopenharmony_ci# Sharing an Application File 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciAn application can share a file with another application based on the file descriptor (FD) or uniform resource identifier (URI) of the file. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci- URI-based file sharing: You can use [wantConstant.Flags](../reference/apis-ability-kit/js-apis-app-ability-wantConstant.md#wantconstantflags) to specify the read or read/write permission on the file for the target application (application with which the file is shared). The target application can call [fs.open](../reference/apis-core-file-kit/js-apis-file-fs.md#fsopen) to open the file based on the URI and perform read and write operations. Currently, only temporary authorization is supported. The permission on the shared file is revoked once the target application exits. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci- FD-based sharing: You can use **open()** of the ohos.file.fs module to specify the read or read/write permission on the file for the target application. After parsing the FD in **Want**, the target application can read or write the file by using **read()** or **write()** API of ohos.file.fs based on the permission granted. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciAfter the FD of a shared file is closed, the target application can no longer open the shared file. Therefore, FD-based file sharing is not recommended. This topic describes how to [share an application file](#sharing-an-application-file) and [use a shared file](#using-a-shared-file) based on the file URI. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## Shareable Application Directories 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci| Application Sandbox Path | Physical Path | Description             | 14e41f4b71Sopenharmony_ci| ------- | ------- | ---- | 15e41f4b71Sopenharmony_ci| /data/storage/el1/base | /data/app/el1/\<currentUserId\>/base/\<PackageName\> | Encrypted database directory under **/el1**.| 16e41f4b71Sopenharmony_ci| /data/storage/el2/base | /data/app/el2/\<currentUserId\>/base/\<PackageName\> | Encrypted database directory under **/el2**.| 17e41f4b71Sopenharmony_ci| /data/storage/el2/distributedfiles | /mnt/hmdfs/\<currentUserId\>/account/device_view/\<networkId\>/data/\<PackageName\> | Distributed data directory under **el2/**.| 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci## File URI Specifications 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciThe file URIs are in the following format: 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci **file**://<bundleName>/<path> 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci- **file**: indicates a file URI. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci- *bundleName*: specifies the owner of the file, that is, the application that shares the file. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci- *path*: specifies the application sandbox path of the file. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci## Sharing an Application File 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciBefore sharing an application file, you need to [obtain the application file path](../application-models/application-context-stage.md#obtaining-application-file-paths). 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci1. Obtain the application sandbox path of the file and convert it into the file URI. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci ```ts 38e41f4b71Sopenharmony_ci import { UIAbility } from '@kit.AbilityKit'; 39e41f4b71Sopenharmony_ci import { fileUri } from '@kit.CoreFileKit'; 40e41f4b71Sopenharmony_ci import { window } from '@kit.ArkUI'; 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci export default class EntryAbility extends UIAbility { 43e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 44e41f4b71Sopenharmony_ci // Obtain the application sandbox path of the file. 45e41f4b71Sopenharmony_ci let pathInSandbox = this.context.filesDir + "/test1.txt"; 46e41f4b71Sopenharmony_ci // Convert the application sandbox path into a URI. 47e41f4b71Sopenharmony_ci let uri = fileUri.getUriFromPath(pathInSandbox); 48e41f4b71Sopenharmony_ci // The obtained URI is file://com.example.demo/data/storage/el2/base/files/test1.txt. 49e41f4b71Sopenharmony_ci } 50e41f4b71Sopenharmony_ci } 51e41f4b71Sopenharmony_ci ``` 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci2. Set the target application and grant permissions on the file.<br> 54e41f4b71Sopenharmony_ci Use [startAbility](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) to start 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-ability-kit/js-apis-app-ability-want.md#properties). 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci > **NOTE** 57e41f4b71Sopenharmony_ci > 58e41f4b71Sopenharmony_ci > The write permission granted includes the read permission. 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci ```ts 61e41f4b71Sopenharmony_ci import { fileUri } from '@kit.CoreFileKit'; 62e41f4b71Sopenharmony_ci import { window } from '@kit.ArkUI'; 63e41f4b71Sopenharmony_ci import { wantConstant } from '@kit.AbilityKit'; 64e41f4b71Sopenharmony_ci import { UIAbility } from '@kit.AbilityKit'; 65e41f4b71Sopenharmony_ci import { Want } from '@kit.AbilityKit'; 66e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci export default class EntryAbility extends UIAbility { 69e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 70e41f4b71Sopenharmony_ci // Obtain the application sandbox path of the file. 71e41f4b71Sopenharmony_ci let filePath = this.context.filesDir + '/test1.txt'; 72e41f4b71Sopenharmony_ci // Convert the application sandbox path into a URI. 73e41f4b71Sopenharmony_ci let uri = fileUri.getUriFromPath(filePath); 74e41f4b71Sopenharmony_ci let want: Want = { 75e41f4b71Sopenharmony_ci // Grant the read and write permissions on the shared file to the target application. 76e41f4b71Sopenharmony_ci flags: wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION | wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION, 77e41f4b71Sopenharmony_ci // Set the implicit startup rule for the target application. 78e41f4b71Sopenharmony_ci action: 'ohos.want.action.sendData', 79e41f4b71Sopenharmony_ci uri: uri, 80e41f4b71Sopenharmony_ci type: 'text/plain' 81e41f4b71Sopenharmony_ci } 82e41f4b71Sopenharmony_ci this.context.startAbility(want) 83e41f4b71Sopenharmony_ci .then(() => { 84e41f4b71Sopenharmony_ci console.info('Invoke getCurrentBundleStats succeeded.'); 85e41f4b71Sopenharmony_ci }) 86e41f4b71Sopenharmony_ci .catch((err: BusinessError) => { 87e41f4b71Sopenharmony_ci console.error(`Invoke startAbility failed, code is ${err.code}, message is ${err.message}`); 88e41f4b71Sopenharmony_ci }); 89e41f4b71Sopenharmony_ci } 90e41f4b71Sopenharmony_ci // ... 91e41f4b71Sopenharmony_ci } 92e41f4b71Sopenharmony_ci ``` 93e41f4b71Sopenharmony_ci**Figure 1** Example<br> 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci## Using a Shared File 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ciIn the [**module.json5** file](../quick-start/module-configuration-file.md) of the target application, set **actions** to **ohos.want.action.sendData** to allow the application to receive files shared by others and set **uris** to the type of the URI to receive. In the following example, the target application receives only .txt files with **scheme** of **file**. 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci```json 101e41f4b71Sopenharmony_ci{ 102e41f4b71Sopenharmony_ci "module": { 103e41f4b71Sopenharmony_ci ... 104e41f4b71Sopenharmony_ci "abilities": [ 105e41f4b71Sopenharmony_ci { 106e41f4b71Sopenharmony_ci ... 107e41f4b71Sopenharmony_ci "skills": [ 108e41f4b71Sopenharmony_ci { 109e41f4b71Sopenharmony_ci ... 110e41f4b71Sopenharmony_ci "actions": [ 111e41f4b71Sopenharmony_ci "ohos.want.action.sendData" 112e41f4b71Sopenharmony_ci ], 113e41f4b71Sopenharmony_ci "uris": [ 114e41f4b71Sopenharmony_ci { 115e41f4b71Sopenharmony_ci "scheme": "file", 116e41f4b71Sopenharmony_ci "type": "text/plain" 117e41f4b71Sopenharmony_ci } 118e41f4b71Sopenharmony_ci ] 119e41f4b71Sopenharmony_ci } 120e41f4b71Sopenharmony_ci ] 121e41f4b71Sopenharmony_ci } 122e41f4b71Sopenharmony_ci ] 123e41f4b71Sopenharmony_ci } 124e41f4b71Sopenharmony_ci} 125e41f4b71Sopenharmony_ci``` 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ciAfter the UIAbility starts, the target application obtains want information via [onCreate()](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityoncreate) or [onNewWant](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonnewwant). 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ciAfter obtaining the URI of the shared file from **want**, the target application can call **fs.open** to open the file and then read and write the file. 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci```ts 132e41f4b71Sopenharmony_ci// xxx.ets 133e41f4b71Sopenharmony_ciimport { fileIo as fs } from '@kit.CoreFileKit'; 134e41f4b71Sopenharmony_ciimport { Want } from '@kit.AbilityKit'; 135e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_cifunction getShareFile() { 138e41f4b71Sopenharmony_ci try { 139e41f4b71Sopenharmony_ci let want: Want = {}; // Change the value to the want information passed by the application that shares the file. 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci // Obtain the uri field from the want information. 142e41f4b71Sopenharmony_ci let uri = want.uri; 143e41f4b71Sopenharmony_ci if (uri == null || uri == undefined) { 144e41f4b71Sopenharmony_ci console.info('uri is invalid'); 145e41f4b71Sopenharmony_ci return; 146e41f4b71Sopenharmony_ci } 147e41f4b71Sopenharmony_ci try { 148e41f4b71Sopenharmony_ci // 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. 149e41f4b71Sopenharmony_ci let file = fs.openSync(uri, fs.OpenMode.READ_WRITE); 150e41f4b71Sopenharmony_ci console.info('open file successfully!'); 151e41f4b71Sopenharmony_ci } catch (err) { 152e41f4b71Sopenharmony_ci let error: BusinessError = err as BusinessError; 153e41f4b71Sopenharmony_ci console.error(`Invoke openSync failed, code is ${error.code}, message is ${error.message}`); 154e41f4b71Sopenharmony_ci } 155e41f4b71Sopenharmony_ci } catch (error) { 156e41f4b71Sopenharmony_ci let err: BusinessError = error as BusinessError; 157e41f4b71Sopenharmony_ci console.error(`Invoke openSync failed, code is ${err.code}, message is ${err.message}`); 158e41f4b71Sopenharmony_ci } 159e41f4b71Sopenharmony_ci} 160e41f4b71Sopenharmony_ci```