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| FileManagement_ErrCode OH_FileUri_GetFileName(const char *uri, unsigned int length, char **result) | Obtains the file name based on the given URI.                       |
28
29## How to Develop
30
31**Adding the Dynamic Link Library**
32
33Add the following library to **CMakeLists.txt**.
34
35```txt
36target_link_libraries(sample PUBLIC libohfileuri.so)
37```
38
39**Adding the Header File**
40
41```c++
42#include <filemanagement/file_uri/oh_file_uri.h>
43```
44
451. Call **OH_FileUri_GetUriFromPath** to obtain the URI from a path. The memory allocated must be released using **free()**. <br>Example:
46
47   ```c
48    #include <cstring>
49
50    void OH_FileUri_GetUriFromPathExample() {
51        char *path = "/data/storage/el2/base/files/test.txt";
52        unsigned int length = strlen(path);
53        char *uriResult = NULL;
54        FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(path, length ,&uriResult);
55        if (ret == 0 && uriResult !=NULL) {
56            printf("pathUri= %s", uriResult); // The URI obtained by application a is file://com.example.demo/data/storage/el2/base/files/test.txt.
57        }
58        if (uriResult != NULL) {
59            free(uriResult);
60        }
61    }
62   ```
63
642. Call **OH_FileUri_GetPathFromUri** to convert the URI into a path. The memory allocated must be released using **free()**. <br>Example:
65
66   ```c
67    #include <cstring>
68
69    void OH_FileUri_GetPathFromUriExample() {
70        char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
71        unsigned int length = strlen(uri);
72        char *pathResult = NULL;
73        FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(uri, length, &pathResult);
74        if (ret == 0 && pathResult != NULL) {
75            printf ("pathResult= %s", pathResult); // PathResult is /data/storage/el2/base/files/test.txt.
76        }
77        if (pathResult != NULL) {
78            free(pathResult);
79        }
80    }
81   ```
82
833. Call **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:
84
85   ```c
86    #include <cstring>
87
88    void OH_FileUri_GetFullDirectoryUriExample() {
89        char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
90        unsigned int length = strlen(uri);
91        char *uriResult = NULL;
92        FileManagement_ErrCode ret = OH_FileUri_GetFullDirectoryUri(uri, length, &uriResult);
93        if (ret == 0 && uriResult != NULL) {
94            printf("pathUri= %s",uriResult);// The URI obtained is file://com.example.demo/data/storage/el2/base/files/.
95        }
96        if (uriResult != NULL) {
97            free(uriResult);
98        }
99    }
100   ```
101
1024. Call **OH_FileUri_IsValidUri** to check whether a URI is valid. <br>Example:
103
104   ```c
105    #include <cstring>
106
107    void OH_FileUri_IsValidUriExample() {
108        char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
109        unsigned int length = strlen(uri);
110        bool falgs = OH_FileUri_IsValidUri(uri, length);
111        printf("The URI is valid? falgs=%d", falgs);
112    }
113   ```
114
1155. Call **OH_FileUri_GetFileName** to obtain the file name from the URI. The memory allocated must be released using **free()**. <br>Example:
116
117   ```c
118    #include <cstring>
119
120    void OH_FileUri_GetFileNameExample() {
121        char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
122        unsigned int length = strlen(uri);
123        char *uriResult = NULL;
124        FileManagement_ErrCode ret = OH_FileUri_GetFileName(uri, length, &uriResult);
125        if (ret == 0 && uriResult != NULL) {
126            printf ("pathUri=%s,"uriResult);// Obtain the file name test.txt from the URI.
127        }
128        if (uriResult != NULL) {
129            free(uriResult);
130        }
131    }
132   ```
133