1# FileUri开发指导(C/C++) 2 3## 场景介绍 4 5FileUri提供了关于文件URI的基本操作,对外提供了URI与沙箱路径之间互相转换、远端URI判定、获取URI所在目录路径的URI等接口,方便用户将文件URI与沙箱路径相互转换。 6 7## 基本概念 8 9**结果集**:满足使用场景正确的路径或者URI。 10 11## 约束限制 12 13- 转换或者判断URI类型之前必须保证传入的参数正确有效。 14 15- 为保证数据的准确性,在转换或者判断过程中只允许处理一个对象。 16 17## 接口说明 18 19接口的详细说明,请参考[API参考](../reference/apis-core-file-kit/fileuri.md) 20 21| 接口名称 | 描述 | 22| -------- |--------------------------------------------| 23| FileManagement_ErrCode OH_FileUri_GetUriFromPath(const char *path, unsigned int length, char **result)| 通过传入的路径PATH获取到对应的URI。 | 24| FileManagement_ErrCode OH_FileUri_GetPathFromUri(const char *uri, unsigned int length, char **result) | 通过传入的URI获取到对应的沙箱路径PATH。 | 25| FileManagement_ErrCode OH_FileUri_GetFullDirectoryUri(const char *uri, unsigned int length, char **result) | 获取所在路径URI,文件获取所在路径URI,如果URI指向目录则获取当前路径URI。 | 26| bool OH_FileUri_IsValidUri(const char *uri, unsigned int length) | 判断传入的URI的格式是否正确。 | 27| FileManagement_ErrCode OH_FileUri_GetFileName(const char *uri, unsigned int length, char **result) | 通过传入的URI获取到对应的文件名称。 | 28 29## 开发步骤 30 31**在CMake脚本中链接动态库** 32 33CMakeLists.txt中添加以下lib。 34 35```txt 36target_link_libraries(sample PUBLIC libohfileuri.so) 37``` 38 39**添加头文件** 40 41```c++ 42#include <filemanagement/file_uri/oh_file_uri.h> 43``` 44 451. 调用OH_FileUri_GetUriFromPath接口,在接口中malloc的内存需要在使用完后释放,因此需要free对应的内存。示例代码如下所示: 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); // 应用a获取到的URI为:file://com.example.demo/data/storage/el2/base/files/test.txt 57 } 58 if (uriResult != NULL) { 59 free(uriResult); 60 } 61 } 62 ``` 63 642. 调用OH_FileUri_GetPathFromUri通过URi转成对应的PATH,在接口中malloc的内存需要在使用完后释放,因此需要free对应的内存。示例代码如下所示: 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值为:/data/storage/el2/base/files/test.txt 76 } 77 if (pathResult != NULL) { 78 free(pathResult); 79 } 80 } 81 ``` 82 833. 调用OH_FileUri_GetFullDirectoryUri获取URI所在路径的URI,在接口中malloc的内存需要在使用完后释放,因此需要free对应的内存。示例代码如下所示: 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);//URI所在路径的URI:file://com.example.demo/data/storage/el2/base/files/ 95 } 96 if (uriResult != NULL) { 97 free(uriResult); 98 } 99 } 100 ``` 101 1024. 可以调用OH_FileUri_IsValidUri接口进行URI格式验证。 示例代码如下所示: 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. 调用OH_FileUri_GetFileName获取URI中的文件名称,在接口中malloc的内存需要在使用完后释放,因此需要free对应的内存。示例代码如下所示: 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);//获取到URI中的文件名:test.txt 127 } 128 if (uriResult != NULL) { 129 free(uriResult); 130 } 131 } 132 ``` 133 134