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