1# Sharing an Application File 2 3The file of an application can be shared with another application based on the file descriptor (FD) or uniform resource identifier (URI) of the file. However, if the FD of a shared file is closed, the file cannot be opened. Therefore, the file sharing based on the FD is not recommended. This section describes how to share an application file based on its URI. 4 5- You can use **wantConstant.Flags()** of the [ohos.app.ability.wantConstant](../reference/apis/js-apis-app-ability-wantConstant.md#wantconstantflags) module to share an application file in read or read/write mode based on its URI with another application. The target application can use **open()** of the [ohos.file.fs](../reference/apis/js-apis-file-fs.md#fsopen) module to open the URI and then perform read and/or write operations based on the permissions granted. Currently, OpenHarmony API version 9 supports only temporary authorization. The permission on shared file is revoked once the target application exits. 6 7- You can also use **open()** of the ohos.file.fs module to share an application file with the specified permissions to another application based on the FD. After parsing the FD from **Want**, the target application can read and write the file by using **read()** and **write()** APIs of ohos.file.fs. 8 9You can use the related APIs to [share a file with another application](#sharing-a-file-with-another-application) or [use shared application files](#using-shared-application-files). 10 11## File URI Specifications 12 13The file URIs are in the following format: 14 15 file://<bundleName>/<path> 16 17- **file**: indicates a file URI. 18 19- *bundleName*: specifies the owner of the file. 20 21- *path*: specifies the application sandbox path of the file. 22 23## Sharing a File with Another Application 24 25Before sharing application files, you need to [obtain the application file path](../application-models/application-context-stage.md#obtaining-the-application-development-path). 26 271. Obtain the application sandbox path of the file and convert it into the file URI. 28 29 ```ts 30 import UIAbility from '@ohos.app.ability.UIAbility'; 31 import fileuri from '@ohos.file.fileuri'; 32 import window from '@ohos.window'; 33 34 export default class EntryAbility extends UIAbility { 35 onWindowStageCreate(windowStage: window.WindowStage) { 36 // Obtain the application sandbox path of the file. 37 let pathInSandbox = this.context.filesDir + "/test.txt"; 38 // Convert the application sandbox path into a URI. 39 let uri = fileuri.getUriFromPath(pathInSandbox); 40 // The obtained URI is file://com.example.demo/data/storage/el2/base/files/test.txt. 41 } 42 } 43 ``` 44 452. Set the target application, with which you want to share the file, and grant permissions on the file. 46 Use [startAbility()](../reference/apis/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) to share the file with the target application. You need to pass in the URI obtained 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/js-apis-app-ability-want.md#attributes). 47 48 > **NOTE** 49 > 50 > The write permission granted includes the read permission. 51 52 ```ts 53 import fileuri from '@ohos.file.fileuri'; 54 import window from '@ohos.window'; 55 import wantConstant from '@ohos.app.ability.wantConstant'; 56 import UIAbility from '@ohos.app.ability.UIAbility'; 57 58 export default class EntryAbility extends UIAbility { 59 onWindowStageCreate(windowStage: window.WindowStage) { 60 // Obtain the application sandbox path of the file. 61 let filePath = this.context.filesDir + '/test.txt'; 62 // Convert the application sandbox path into a URI. 63 let uri = fileuri.getUriFromPath(filePath); 64 let want = { 65 // Grant the read and write permissions on the shared file to the target application. 66 flags: wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION | wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION, 67 // Set the implicit startup rule for the application that shares the file. 68 action: 'ohos.want.action.sendData', 69 uri: uri, 70 type: 'text/plain' 71 } 72 this.context.startAbility(want) 73 .then(() => { 74 console.info('Invoke getCurrentBundleStats succeeded.'); 75 }) 76 .catch((err) => { 77 console.error(`Invoke startAbility failed, code is ${err.code}, message is ${err.message}`); 78 }); 79 } 80 81 ... 82 } 83 ``` 84 85## Using Shared Files 86 87In the [**module.json5** file](../quick-start/module-configuration-file.md) of the application, which wants to use the shared file, set **actions** to **ohos.want.action.sendData** to allow the application to receive files shared by another application and set **uris** to the type of the URI to receive. In the following example, the application receives only .txt files with **scheme** of **file**. 88 89```json 90{ 91 "module": { 92 ... 93 "abilities": [ 94 { 95 ... 96 "skills": [ 97 { 98 ... 99 "actions": [ 100 "ohos.want.action.sendData" 101 ], 102 "uris": [ 103 { 104 "scheme": "file", 105 "type": "text/plain" 106 } 107 ] 108 } 109 ] 110 } 111 ] 112 } 113} 114``` 115 116After **UIAbility** of the application starts, the application obtains **want** information from [**onCreate()**](../reference/apis/js-apis-app-ability-uiAbility.md#uiabilityoncreate) or [**onNewWant()**](../reference/apis/js-apis-app-ability-uiAbility.md#uiabilityonnewwant). 117 118After obtaining the URI of the shared file through **want**, the application can call **fs.open()** to open the file, and then read and write the file after obtaining the related file object. 119 120```ts 121// xxx.ets 122import fs from '@ohos.file.fs'; 123 124function getShareFile() { 125 try { 126 let want =...; // Obtain the want information sent from the application that shares the file. 127 128 // Obtain the uri field from the want information. 129 let uri = want.uri; 130 if (uri == null || uri == undefined) { 131 console.info('uri is invalid'); 132 return; 133 } 134 try { 135 // 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. 136 let file = fs.openSync(uri, fs.OpenMode.READ_WRITE); 137 console.info('open file successfully!'); 138 } catch (error) { 139 console.error(`Invoke openSync failed, code is ${error.code}, message is ${error.message}`); 140 } 141 } catch (error) { 142 console.error(`Invoke openSync failed, code is ${error.code}, message is ${error.message}`); 143 } 144} 145``` 146