1e41f4b71Sopenharmony_ci# @ohos.file.fileuri (File URI) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **fileUri** module allows the uniform resource identifier (URI) of a file to be obtained based on the file path. With the file URI, you can use the APIs provided by [@ohos.file.fs](js-apis-file-fs.md) to operate the file. 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 9e41f4b71Sopenharmony_ci## Modules to Import 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```ts 12e41f4b71Sopenharmony_ciimport { fileUri } from '@kit.CoreFileKit'; 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ciBefore using this module, you need to obtain the application sandbox path of the file. The following is an example: 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci ```ts 18e41f4b71Sopenharmony_ci import { UIAbility } from '@kit.AbilityKit'; 19e41f4b71Sopenharmony_ci import { window } from '@kit.ArkUI'; 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci export default class EntryAbility extends UIAbility { 22e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 23e41f4b71Sopenharmony_ci let context = this.context; 24e41f4b71Sopenharmony_ci let pathDir = context.filesDir; 25e41f4b71Sopenharmony_ci } 26e41f4b71Sopenharmony_ci } 27e41f4b71Sopenharmony_ci ``` 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci## FileUri<sup>10+</sup> 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci### Properties 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.AppFileService 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 36e41f4b71Sopenharmony_ci| -------- | --------| -------- | -------- | 37e41f4b71Sopenharmony_ci| path<sup>10+</sup> | string | Yes| Path of the file.| 38e41f4b71Sopenharmony_ci| name<sup>10+</sup> | string | Yes| Name of the file.| 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci### constructor<sup>10+</sup> 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ciconstructor(uriOrPath: string) 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ciA constructor used to create a **FileUri** instance. 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.AppFileService 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci**Parameters** 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 51e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 52e41f4b71Sopenharmony_ci| uriOrPath | string | Yes| URI or path. The following types of URIs are available:<br>- Application sandbox URI: **file://\<bundleName>/\<sandboxPath>**<br>- User file URI: **file://docs/storage/Users/currentUser/\<publicPath>**<br>- User media asset URI: **file://media/\<mediaType>/IMG_DATATIME_ID/\<displayName>**| 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci**Error codes** 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ciFor details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 57e41f4b71Sopenharmony_ci| ID | Error Message | 58e41f4b71Sopenharmony_ci| ---------------------------- | ---------- | 59e41f4b71Sopenharmony_ci| 13900005 | I/O error | 60e41f4b71Sopenharmony_ci| 13900042 | Unknown error | 61e41f4b71Sopenharmony_ci| 13900020 | invalid argument | 62e41f4b71Sopenharmony_ci| 13900002 | invalid uri | 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci**Example** 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci ```ts 67e41f4b71Sopenharmony_ci let path = pathDir + '/test'; 68e41f4b71Sopenharmony_ci let uri = fileUri.getUriFromPath(path); // file://<packageName>/data/storage/el2/base/haps/entry/files/test 69e41f4b71Sopenharmony_ci let fileUriObject = new fileUri.FileUri(uri); 70e41f4b71Sopenharmony_ci console.info("The name of FileUri is " + fileUriObject.name); 71e41f4b71Sopenharmony_ci ``` 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci### toString<sup>10+</sup> 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_citoString(): string 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.AppFileService 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ciConverts this URI into a string. 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci**Return value** 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci| Type| Description| 84e41f4b71Sopenharmony_ci| -------- | -------- | 85e41f4b71Sopenharmony_ci| string | URI obtained, in the string format.| 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci**Example** 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci ```ts 90e41f4b71Sopenharmony_ci let path = pathDir + '/test'; 91e41f4b71Sopenharmony_ci let fileUriObject = new fileUri.FileUri(path); 92e41f4b71Sopenharmony_ci console.info("The uri of FileUri is " + fileUriObject.toString()); 93e41f4b71Sopenharmony_ci ``` 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci### getFullDirectoryUri<sup>11+</sup> 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_cigetFullDirectoryUri(): string 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ciObtains the URI of the full directory of this file or folder. 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ciFor a file, this API returns the URI of the directory where the file is located. For example, **xxx** will be returned for the **xxx/example.txt** file. 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ciFor a folder, this API returns the URI of the folder. 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.AppFileService 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci**Return value** 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci| Type | Description | 110e41f4b71Sopenharmony_ci| --------------------- |-----------------------------------| 111e41f4b71Sopenharmony_ci| string | URI of the directory where the current file is located or URI of the current folder.| 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci**Error codes** 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ciFor details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci| ID | Error Message | 118e41f4b71Sopenharmony_ci| ---------------------------- |---------------------------| 119e41f4b71Sopenharmony_ci| 13900002 | No such file or directory | 120e41f4b71Sopenharmony_ci| 13900012 | Permission denied | 121e41f4b71Sopenharmony_ci| 13900042 | Unknown error | 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci**Example** 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci ```ts 126e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 127e41f4b71Sopenharmony_ci try { 128e41f4b71Sopenharmony_ci let path = pathDir + '/test.txt'; 129e41f4b71Sopenharmony_ci let fileUriObject = new fileUri.FileUri(path); 130e41f4b71Sopenharmony_ci let directoryUri = fileUriObject.getFullDirectoryUri(); 131e41f4b71Sopenharmony_ci console.log(`success to getFullDirectoryUri: ${JSON.stringify(directoryUri)}`); 132e41f4b71Sopenharmony_ci } catch (error) { 133e41f4b71Sopenharmony_ci console.error(`failed to getFullDirectoryUri because: ${JSON.stringify(error)}`); 134e41f4b71Sopenharmony_ci } 135e41f4b71Sopenharmony_ci ``` 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci### isRemoteUri<sup>12+</sup> 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ciisRemoteUri(): boolean 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ciChecks whether this URI is a remote URI. 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.AppFileService 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci**Return value** 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci| Type | Description | 148e41f4b71Sopenharmony_ci| --------------------- |-----------------------------------| 149e41f4b71Sopenharmony_ci| boolean | - Returns **true** if the URI points to a remote file or folder, for example, **xxx/example.txt? networkid=xxx**.<br>- Returns **false** if the URI points to a local file or folder.| 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci**Error codes** 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ciFor details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md). 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci| ID | Error Message | 156e41f4b71Sopenharmony_ci| ---------------------------- |---------------------------| 157e41f4b71Sopenharmony_ci| 13900042 | Unknown error | 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci**Example** 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci ```ts 162e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 163e41f4b71Sopenharmony_ci function isRemoteUriExample() { 164e41f4b71Sopenharmony_ci let uri = "file://com.example.demo/data/stroage/el2/base/test.txt?networkid=xxxx";// ?networkid identifies a remote device. 165e41f4b71Sopenharmony_ci let fileUriObject = new fileUri.FileUri(uri); 166e41f4b71Sopenharmony_ci let ret = fileUriObject.isRemoteUri(); 167e41f4b71Sopenharmony_ci if (ret) { 168e41f4b71Sopenharmony_ci console.log(`It is a remote uri.`); 169e41f4b71Sopenharmony_ci } 170e41f4b71Sopenharmony_ci } 171e41f4b71Sopenharmony_ci ``` 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci## fileUri.getUriFromPath 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_cigetUriFromPath(path: string): string 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ciObtains the URI based on a file path. This API returns the result synchronously. 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci**System capability**: SystemCapability.FileManagement.AppFileService 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci**Parameters** 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 184e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- | 185e41f4b71Sopenharmony_ci| path | string | Yes | Application sandbox path of the file.| 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci**Return value** 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci | Type | Description | 190e41f4b71Sopenharmony_ci | ---------------------------- | ---------- | 191e41f4b71Sopenharmony_ci | string | File URI obtained.| 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci**Error codes** 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 196e41f4b71Sopenharmony_ci| ID | Error Message | 197e41f4b71Sopenharmony_ci| ---------------------------- | ---------- | 198e41f4b71Sopenharmony_ci| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types | 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci**Example** 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci ```ts 203e41f4b71Sopenharmony_ci let filePath = pathDir + "/test"; 204e41f4b71Sopenharmony_ci let uri = fileUri.getUriFromPath(filePath); 205e41f4b71Sopenharmony_ci ``` 206