1e41f4b71Sopenharmony_ci# FileUri开发指导(C/C++)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## 场景介绍
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciFileUri提供了关于文件URI的基本操作,对外提供了URI与沙箱路径之间互相转换、远端URI判定、获取URI所在目录路径的URI等接口,方便用户将文件URI与沙箱路径相互转换。
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## 基本概念
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci**结果集**:满足使用场景正确的路径或者URI。
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## 约束限制
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci- 转换或者判断URI类型之前必须保证传入的参数正确有效。
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci- 为保证数据的准确性,在转换或者判断过程中只允许处理一个对象。
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## 接口说明
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci接口的详细说明,请参考[API参考](../reference/apis-core-file-kit/fileuri.md)
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci| 接口名称 | 描述                                         |
22e41f4b71Sopenharmony_ci| -------- |--------------------------------------------|
23e41f4b71Sopenharmony_ci| FileManagement_ErrCode OH_FileUri_GetUriFromPath(const char *path, unsigned int length, char **result)| 通过传入的路径PATH获取到对应的URI。                      |
24e41f4b71Sopenharmony_ci| FileManagement_ErrCode OH_FileUri_GetPathFromUri(const char *uri, unsigned int length, char **result) | 通过传入的URI获取到对应的沙箱路径PATH。                    |
25e41f4b71Sopenharmony_ci| FileManagement_ErrCode OH_FileUri_GetFullDirectoryUri(const char *uri, unsigned int length, char **result) | 获取所在路径URI,文件获取所在路径URI,如果URI指向目录则获取当前路径URI。 |
26e41f4b71Sopenharmony_ci| bool OH_FileUri_IsValidUri(const char *uri, unsigned int length) | 判断传人的URI的格式是否正确。                           |
27e41f4b71Sopenharmony_ci| FileManagement_ErrCode OH_FileUri_GetFileName(const char *uri, unsigned int length, char **result) | 通过传入的URI获取到对应的文件名称。                        |
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci## 开发步骤
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci**在CMake脚本中链接动态库**
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ciCMakeLists.txt中添加以下lib。
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci```txt
36e41f4b71Sopenharmony_citarget_link_libraries(sample PUBLIC libohfileuri.so)
37e41f4b71Sopenharmony_ci```
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci**添加头文件**
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci```c++
42e41f4b71Sopenharmony_ci#include <filemanagement/file_uri/oh_file_uri.h>
43e41f4b71Sopenharmony_ci```
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci1. 调用OH_FileUri_GetUriFromPath接口,在接口中malloc的内存需要在使用完后释放,因此需要free对应的内存。示例代码如下所示:
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci   ```c
48e41f4b71Sopenharmony_ci    #include <cstring>
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci    void OH_FileUri_GetUriFromPathExample() {
51e41f4b71Sopenharmony_ci        char *path = "/data/storage/el2/base/files/test.txt";
52e41f4b71Sopenharmony_ci        unsigned int length = strlen(path);
53e41f4b71Sopenharmony_ci        char *uriResult = NULL;
54e41f4b71Sopenharmony_ci        FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(path, length ,&uriResult); 
55e41f4b71Sopenharmony_ci        if (ret == 0 && uriResult !=NULL) {
56e41f4b71Sopenharmony_ci            printf("pathUri=%s", uriResult); // 应用a获取到的URI为:file://com.example.demo/data/storage/el2/base/files/test.txt
57e41f4b71Sopenharmony_ci        }
58e41f4b71Sopenharmony_ci        if (uriResult != NULL) {
59e41f4b71Sopenharmony_ci            free(uriResult);
60e41f4b71Sopenharmony_ci        }
61e41f4b71Sopenharmony_ci    }    
62e41f4b71Sopenharmony_ci   ```
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci2. 调用OH_FileUri_GetPathFromUri通过URi转成对应的PATH,在接口中malloc的内存需要在使用完后释放,因此需要free对应的内存。示例代码如下所示:
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci   ```c
67e41f4b71Sopenharmony_ci    #include <cstring>
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci    void OH_FileUri_GetPathFromUriExample() {
70e41f4b71Sopenharmony_ci        char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
71e41f4b71Sopenharmony_ci        unsigned int length = strlen(uri);
72e41f4b71Sopenharmony_ci        char *pathResult = NULL;
73e41f4b71Sopenharmony_ci        FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(uri, length, &pathResult);
74e41f4b71Sopenharmony_ci        if (ret == 0 && pathResult != NULL) {
75e41f4b71Sopenharmony_ci            printf("pathResult=%s", pathResult); // PathResult值为:/data/storage/el2/base/files/test.txt
76e41f4b71Sopenharmony_ci        }
77e41f4b71Sopenharmony_ci        if (pathResult != NULL) {
78e41f4b71Sopenharmony_ci            free(pathResult);
79e41f4b71Sopenharmony_ci        }
80e41f4b71Sopenharmony_ci    }
81e41f4b71Sopenharmony_ci   ```
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci3. 调用OH_FileUri_GetFullDirectoryUri获取URI所在路径的URI,在接口中malloc的内存需要在使用完后释放,因此需要free对应的内存。示例代码如下所示:
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci   ```c
86e41f4b71Sopenharmony_ci    #include <cstring>
87e41f4b71Sopenharmony_ci    
88e41f4b71Sopenharmony_ci    void OH_FileUri_GetFullDirectoryUriExample() {
89e41f4b71Sopenharmony_ci        char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
90e41f4b71Sopenharmony_ci        unsigned int length = strlen(uri);
91e41f4b71Sopenharmony_ci        char *uriResult = NULL;
92e41f4b71Sopenharmony_ci        FileManagement_ErrCode ret = OH_FileUri_GetFullDirectoryUri(uri, length, &uriResult);
93e41f4b71Sopenharmony_ci        if (ret == 0 && uriResult != NULL) {
94e41f4b71Sopenharmony_ci            printf("pathUri=%s",uriResult);//URI所在路径的URI:file://com.example.demo/data/storage/el2/base/files/
95e41f4b71Sopenharmony_ci        }
96e41f4b71Sopenharmony_ci        if (uriResult != NULL) {
97e41f4b71Sopenharmony_ci            free(uriResult);
98e41f4b71Sopenharmony_ci        }
99e41f4b71Sopenharmony_ci    }
100e41f4b71Sopenharmony_ci   ```
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci4. 可以调用OH_FileUri_IsValidUri接口进行URI格式验证。 示例代码如下所示:
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci   ```c
105e41f4b71Sopenharmony_ci    #include <cstring>
106e41f4b71Sopenharmony_ci    
107e41f4b71Sopenharmony_ci    void OH_FileUri_IsValidUriExample() {
108e41f4b71Sopenharmony_ci        char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
109e41f4b71Sopenharmony_ci        unsigned int length = strlen(uri);
110e41f4b71Sopenharmony_ci        bool falgs = OH_FileUri_IsValidUri(uri, length);
111e41f4b71Sopenharmony_ci        printf("The URI is valid? falgs=%d", falgs);
112e41f4b71Sopenharmony_ci    }
113e41f4b71Sopenharmony_ci   ```
114e41f4b71Sopenharmony_ci   
115e41f4b71Sopenharmony_ci5. 调用OH_FileUri_GetFileName获取URI中的文件名称,在接口中malloc的内存需要在使用完后释放,因此需要free对应的内存。示例代码如下所示:
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci   ```c
118e41f4b71Sopenharmony_ci    #include <cstring>
119e41f4b71Sopenharmony_ci    
120e41f4b71Sopenharmony_ci    void OH_FileUri_GetFileNameExample() {
121e41f4b71Sopenharmony_ci        char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
122e41f4b71Sopenharmony_ci        unsigned int length = strlen(uri);
123e41f4b71Sopenharmony_ci        char *uriResult = NULL;
124e41f4b71Sopenharmony_ci        FileManagement_ErrCode ret = OH_FileUri_GetFileName(uri, length, &uriResult);
125e41f4b71Sopenharmony_ci        if (ret == 0 && uriResult != NULL) {
126e41f4b71Sopenharmony_ci            printf("pathUri=%s",uriResult);//获取到URI中的文件名:test.txt
127e41f4b71Sopenharmony_ci        }
128e41f4b71Sopenharmony_ci        if (uriResult != NULL) {
129e41f4b71Sopenharmony_ci            free(uriResult);
130e41f4b71Sopenharmony_ci        }
131e41f4b71Sopenharmony_ci    }
132e41f4b71Sopenharmony_ci   ```
133e41f4b71Sopenharmony_ci
134