1# oh_file_uri.h 2<!--Kit: Core File Kit--> 3<!--Subsystem: FileManagement--> 4<!--Owner: @lvzhenjie--> 5<!--Designer: @wang_zhangjun; @chenxi0605--> 6<!--Tester: @liuhonggang123--> 7<!--Adviser: @foryourself--> 8 9## Overview 10 11FileUri provides APIs for URI operations, including performing URI-path conversion, obtaining directory URIs, and verifying URIs. 12 13**Library**: libohfileuri.so 14 15**System capability**: SystemCapability.FileManagement.AppFileService 16 17**Since**: 12 18 19**Related module**: [fileUri](capi-fileuri.md) 20 21## Summary 22 23### Functions 24 25| Name| Description| 26| -- | -- | 27| [FileManagement_ErrCode OH_FileUri_GetUriFromPath(const char *path, unsigned int length, char **result)](#oh_fileuri_geturifrompath) | Converts the input path to the URI of the application.<br> In this process, Chinese characters and non-digit characters in the path are compiled into the corresponding ASCII code and concatenated to the URI.| 28| [FileManagement_ErrCode OH_FileUri_GetPathFromUri(const char *uri, unsigned int length, char **result)](#oh_fileuri_getpathfromuri) | Converts the URI to the corresponding sandbox path.<br> 1. In this process, the ASCII code in the URI is decoded and then concatenated to the original position. If the URI generated by a non-system API contains special characters that cannot be decoded, the string will not be concatenated.<br>2. Since strings are replaced based on specific rules that may evolve with the system, and no verification is performed, the converted path may not be accessible.| 29| [FileManagement_ErrCode OH_FileUri_GetFullDirectoryUri(const char *uri, unsigned int length, char **result)](#oh_fileuri_getfulldirectoryuri) | Obtains the URI of the path. <br>If the URI points to a file, the URI of the path is returned; if the URI points to a directory, the original string is returned.<br> If the file specified by the URI does not exist or the property fails to be obtained, an empty string is returned.| 30| [bool OH_FileUri_IsValidUri(const char *uri, unsigned int length)](#oh_fileuri_isvaliduri) | Checks whether the format of the input URI is correct. The system only checks whether the URI meets the format specifications defined by the system. The validity of the URI is not verified.| 31| [FileManagement_ErrCode OH_FileUri_GetFileName(const char *uri, unsigned int length, char **result)](#oh_fileuri_getfilename) | Obtains the file name based on the URI. <br>The ASCII code in the file name will be decoded and concatenated to the original position.| 32 33## Function Description 34 35### OH_FileUri_GetUriFromPath() 36 37``` 38FileManagement_ErrCode OH_FileUri_GetUriFromPath(const char *path, unsigned int length, char **result) 39``` 40 41**Description** 42 43Converts the input path to the URI of the application.<br> In this process, Chinese characters and non-digit characters in the path are compiled into the corresponding ASCII code and concatenated to the URI. 44 45**System capability**: SystemCapability.FileManagement.AppFileService 46 47**Since**: 12 48 49 50**Parameters** 51 52| Name| Description| 53| -- | -- | 54| const char *path | Pointer to the path to convert.| 55| unsigned int length | Length of the path to convert.| 56| char **result | Double pointer to the URI obtained. You also need to use **free()** of the standard library to release the memory allocated.| 57 58**Returns** 59 60| Type| Description| 61| -- | -- | 62| [FileManagement_ErrCode](capi-error-code-h.md#filemanagement_errcode) | Returns a specific error code. For details, see [FileManagement_ErrCode](capi-error-code-h.md#filemanagement_errcode).<br> ERR_PARAMS 401 - The input parameter is invalid. Possible causes:<br> 1. The **path** parameter is a null pointer.<br> 2. The **result** parameter is a null pointer.<br> 3. The length of the input path is inconsistent with the value of **length**.<br> [ERR_UNKNOWN](capi-error-code-h.md#filemanagement_errcode) 13900042 - An unknown error occurs. This error is returned if the length of the converted URI is 0.<br> [ERR_ENOMEM](capi-error-code-h.md#filemanagement_errcode) 13900011 - The memory allocation or copy fails.<br> [ERR_OK](capi-error-code-h.md#filemanagement_errcode) 0 - The API is called successfully.| 63 64### OH_FileUri_GetPathFromUri() 65 66``` 67FileManagement_ErrCode OH_FileUri_GetPathFromUri(const char *uri, unsigned int length, char **result) 68``` 69 70**Description** 71 72Converts the URI to the corresponding sandbox path.<br> 1. In this process, the ASCII code in the URI is decoded and then concatenated to the original position. If the URI generated by a non-system API contains special characters that cannot be decoded, the string will not be concatenated.<br>2. Since strings are replaced based on specific rules that may evolve with the system, and no verification is performed, the converted path may not be accessible. 73 74**System capability**: SystemCapability.FileManagement.AppFileService 75 76**Since**: 12 77 78 79**Parameters** 80 81| Name| Description| 82| -- | -- | 83| const char *uri | Pointer to the URI to convert.| 84| unsigned int length | Length of the URI to convert.| 85| char **result | Double pointer to the path obtained. You also need to use **free()** of the standard library to release the memory allocated.| 86 87**Returns** 88 89| Type| Description| 90| -- | -- | 91| [FileManagement_ErrCode](capi-error-code-h.md#filemanagement_errcode) | Returns a specific error code. For details, see [FileManagement_ErrCode](capi-error-code-h.md#filemanagement_errcode).<br> {@link ERR_PARAMS} 401 - The input parameter is invalid. Possible causes:<br> 1. The **uri** parameter is a null pointer.<br> 2. The **result** parameter is a null pointer.<br> 3. The length of the input path is inconsistent with the value of **length**.<br> [ERR_UNKNOWN](capi-error-code-h.md#filemanagement_errcode) 13900042 - An unknown error occurs. This error is returned if the length of the converted path is 0.<br> [ERR_ENOMEM](capi-error-code-h.md#filemanagement_errcode) 13900011 - The memory allocation or copy fails.<br> [ERR_OK](capi-error-code-h.md#filemanagement_errcode) 0 - The API is called successfully.| 92 93### OH_FileUri_GetFullDirectoryUri() 94 95``` 96FileManagement_ErrCode OH_FileUri_GetFullDirectoryUri(const char *uri, unsigned int length, char **result) 97``` 98 99**Description** 100 101Obtains the URI of the path. <br>If the URI points to a file, the URI of the path is returned; if the URI points to a directory, the original string is returned.<br> If the file specified by the URI does not exist or the property fails to be obtained, an empty string is returned. 102 103**System capability**: SystemCapability.FileManagement.AppFileService 104 105**Since**: 12 106 107 108**Parameters** 109 110| Name| Description| 111| -- | -- | 112| const char *uri | Pointer to the target URI.| 113| unsigned int length | Length of the URI, in bytes.| 114| char **result | Double pointer to the URI obtained. You also need to use **free()** of the standard library to release the memory allocated.| 115 116**Returns** 117 118| Type| Description| 119| -- | -- | 120| [FileManagement_ErrCode](capi-error-code-h.md#filemanagement_errcode) | Returns a specific error code. For details, see [FileManagement_ErrCode](capi-error-code-h.md#filemanagement_errcode).<br> ERR_PARAMS 401 - The input parameter is invalid. Possible causes:<br> 1. The **uri** parameter is a null pointer.<br> 2. The **result** parameter is a null pointer.<br> 3. The length of the input path is inconsistent with the value of **length**.<br> [ERR_ENOMEM](capi-error-code-h.md#filemanagement_errcode) 13900011 - The memory allocation or copy fails.<br> [ERR_ENOENT](capi-error-code-h.md#filemanagement_errcode) 13900002 - The file or directory does not exist.<br> [ERR_UNKNOWN](capi-error-code-h.md#filemanagement_errcode) 13900042 - An unknown error occurs. This error is returned if the length of the obtained directory URI is 0.<br> [ERR_OK](capi-error-code-h.md#filemanagement_errcode) 0 - The API is called successfully.| 121 122### OH_FileUri_IsValidUri() 123 124``` 125bool OH_FileUri_IsValidUri(const char *uri, unsigned int length) 126``` 127 128**Description** 129 130Checks whether the format of the input URI is correct. The system only checks whether the URI meets the format specifications defined by the system. The validity of the URI is not verified. 131 132**System capability**: SystemCapability.FileManagement.AppFileService 133 134**Since**: 12 135 136 137**Parameters** 138 139| Name| Description| 140| -- | -- | 141| const char *uri | Pointer to the URI to check.| 142| unsigned int length | Length of the URI to check.| 143 144**Returns** 145 146| Type| Description| 147| -- | -- | 148| bool | Returns **true** if the URI is valid; returns **false** otherwise.| 149 150### OH_FileUri_GetFileName() 151 152``` 153FileManagement_ErrCode OH_FileUri_GetFileName(const char *uri, unsigned int length, char **result) 154``` 155 156**Description** 157 158Obtains the file name based on the URI. The ASCII code in the file name will be decoded and concatenated to the original position. 159 160**System capability**: SystemCapability.FileManagement.AppFileService 161 162**Since**: 13 163 164 165**Parameters** 166 167| Name| Description| 168| -- | -- | 169| const char *uri | Pointer to the URI to obtain.| 170| unsigned int length | Length of the URI, in bytes.| 171| char **result | Double pointer to the file name obtained. You also need to use **free()** of the standard library to release the memory allocated.| 172 173**Returns** 174 175| Type| Description| 176| -- | -- | 177| [FileManagement_ErrCode](capi-error-code-h.md#filemanagement_errcode) | Returns a specific error code. For details, see [FileManagement_ErrCode](capi-error-code-h.md#filemanagement_errcode).<br> ERR_PARAMS 401 - The input parameter is invalid. Possible causes:<br> 1. The **uri** parameter is a null pointer.<br> 2. The **result** parameter is a null pointer.<br> 3. The length of the input path is inconsistent with the value of **length**.<br> 4. The URI is in incorrect format.<br> [ERR_ENOMEM](capi-error-code-h.md#filemanagement_errcode) 13900011 - The memory allocation or copy fails.<br> [ERR_OK](capi-error-code-h.md#filemanagement_errcode) 0 - The API is called successfully.| 178