1e41f4b71Sopenharmony_ci# FileUri Development (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## When to Use 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciYou can use the APIs provided by the **fileUri** module to perform basic URI operations. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Basic Concepts 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci**result**: path or URI that meets the service requirements. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## Constraints 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci- The parameters passed in must be correct and valid before a URI is converted or verified. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci- To ensure data accuracy, only one object can be processed during the conversion or verification of a URI. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## Available APIs 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciFor details about the APIs, see [File URI](../reference/apis-core-file-kit/fileuri.md). 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci| API| Description| 22e41f4b71Sopenharmony_ci| -------- | -------- | 23e41f4b71Sopenharmony_ci| FileManagement_ErrCode OH_FileUri_GetUriFromPath(const char *path, unsigned int length, char **result)| Obtains the URI from a path.| 24e41f4b71Sopenharmony_ci| FileManagement_ErrCode OH_FileUri_GetPathFromUri(const char *uri, unsigned int length, char **result) | Obtains the sandbox path from a URI.| 25e41f4b71Sopenharmony_ci| FileManagement_ErrCode OH_FileUri_GetFullDirectoryUri(const char *uri, unsigned int length, char **result) | Obtains the URI of the directory, in which a URI is located.| 26e41f4b71Sopenharmony_ci| bool OH_FileUri_IsValidUri(const char *uri, unsigned int length) | Checks whether a URI is valid.| 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci## How to Develop 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci**Adding the Dynamic Link Library** 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ciAdd the following library to **CMakeLists.txt**. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci```txt 35e41f4b71Sopenharmony_citarget_link_libraries(sample PUBLIC libohfileuri.so) 36e41f4b71Sopenharmony_ci``` 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**Adding the Header File** 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci```c++ 41e41f4b71Sopenharmony_ci#include <filemanagement/file_uri/oh_file_uri.h> 42e41f4b71Sopenharmony_ci``` 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci1. Use **OH_FileUri_GetUriFromPath** to obtain the URI from a path. The memory allocated must be released using **free()**. <br>Example: 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci ```c 47e41f4b71Sopenharmony_ci #include <cstring> 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci void OH_FileUri_GetUriFromPathExample() { 50e41f4b71Sopenharmony_ci char *path = "/data/storage/el2/base/files/test.txt"; 51e41f4b71Sopenharmony_ci unsigned int length = strlen(path); 52e41f4b71Sopenharmony_ci char *uriResult = NULL; 53e41f4b71Sopenharmony_ci FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(path, length ,&uriResult); 54e41f4b71Sopenharmony_ci if (ret == 0 && uriResult !=NULL) { 55e41f4b71Sopenharmony_ci printf("pathUri= %s", uriResult); // The URI obtained by application a is file://com.example.demo/data/storage/el2/base/files/test.txt. 56e41f4b71Sopenharmony_ci } 57e41f4b71Sopenharmony_ci if (uriResult != NULL) { 58e41f4b71Sopenharmony_ci free(uriResult); 59e41f4b71Sopenharmony_ci } 60e41f4b71Sopenharmony_ci } 61e41f4b71Sopenharmony_ci ``` 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci2. Use **OH_FileUri_GetPathFromUri** to convert a URI into a path. The memory allocated must be released using **free()**. <br>Example: 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci ```c 66e41f4b71Sopenharmony_ci #include <cstring> 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci void OH_FileUri_GetPathFromUriExample() { 69e41f4b71Sopenharmony_ci char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt"; 70e41f4b71Sopenharmony_ci unsigned int length = strlen(uri); 71e41f4b71Sopenharmony_ci char *pathResult = NULL; 72e41f4b71Sopenharmony_ci FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(uri, length, &pathResult); 73e41f4b71Sopenharmony_ci if (ret == 0 && pathResult != NULL) { 74e41f4b71Sopenharmony_ci printf ("pathResult= %s", pathResult); // PathResult is /data/storage/el2/base/files/test.txt. 75e41f4b71Sopenharmony_ci } 76e41f4b71Sopenharmony_ci if (pathResult != NULL) { 77e41f4b71Sopenharmony_ci free(pathResult); 78e41f4b71Sopenharmony_ci } 79e41f4b71Sopenharmony_ci } 80e41f4b71Sopenharmony_ci ``` 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci3. Use **OH_FileUri_GetFullDirectoryUri** to obtain the URI of the directory where the specified URI is located. The memory allocated must be released using **free()**. <br>Example: 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci ```c 85e41f4b71Sopenharmony_ci #include <cstring> 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci void OH_FileUri_GetFullDirectoryUriExample() { 88e41f4b71Sopenharmony_ci char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt"; 89e41f4b71Sopenharmony_ci unsigned int length = strlen(uri); 90e41f4b71Sopenharmony_ci char *uriResult = NULL; 91e41f4b71Sopenharmony_ci FileManagement_ErrCode ret = OH_FileUri_GetFullDirectoryUri(uri, length, &uriResult); 92e41f4b71Sopenharmony_ci if (ret == 0 && uriResult != NULL) { 93e41f4b71Sopenharmony_ci printf("pathUri= %s",uriResult);// The URI obtained is file://com.example.demo/data/storage/el2/base/files/. 94e41f4b71Sopenharmony_ci } 95e41f4b71Sopenharmony_ci if (uriResult != NULL) { 96e41f4b71Sopenharmony_ci free(uriResult); 97e41f4b71Sopenharmony_ci } 98e41f4b71Sopenharmony_ci } 99e41f4b71Sopenharmony_ci ``` 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci4. Use **OH_FileUri_IsValidUri** to check whether a URI is valid. <br>Example: 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci ```c 104e41f4b71Sopenharmony_ci #include <cstring> 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci void OH_FileUri_IsValidUriExample() { 107e41f4b71Sopenharmony_ci char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt"; 108e41f4b71Sopenharmony_ci unsigned int length = strlen(uri); 109e41f4b71Sopenharmony_ci bool falgs = OH_FileUri_IsValidUri(uri, length); 110e41f4b71Sopenharmony_ci printf("The URI is valid? falgs=%d", falgs); 111e41f4b71Sopenharmony_ci } 112e41f4b71Sopenharmony_ci ``` 113