• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# FileUri开发指导(C/C++)
2
3## 场景介绍
4
5FileUri提供了关于文件uri的基本操作,将uri转换成对应的沙箱路径path、将应用沙箱路径path转换成对应应用自己的uri、获取uri所在目录路径的uri等接口能力,方便应用对文件分享业务中uri的访问。
6
7## 基本概念
8
9**结果集**:满足使用场景正确的路径或者uri。
10
11## 约束限制
12
13- uri转path时,uri来源建议使用系统能力获取,例如:picker、剪切板、拖拽、及系统提供的path转uri接口等系统能力返回的uri;如果转换应用或用户拼接的uri,则转换后的path可能无法访问。
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;将path转uri时,路径中的中文及非数字字母的特殊字符将会被编译成对应的ASCII码,拼接在uri中。|
24| FileManagement_ErrCode OH_FileUri_GetPathFromUri(const char *uri, unsigned int length, char **result) | 将uri转换成对应的沙箱路径path。 <br>1、uri转path过程中会将uri中存在的ASCII码进行解码后拼接在原处,非系统接口生成的uri中可能存在ASCII码解析范围之外的字符,导致字符串无法正常拼接;<br>2、转换处理为系统约定的字符串替换规则(规则随系统演进可能会发生变化),转换过程中不进行路径校验操作,无法保证转换结果的一定可以访问。 |
25| FileManagement_ErrCode OH_FileUri_GetFullDirectoryUri(const char *uri, unsigned int length, char **result) | 获取所在路径uri。<br>uri指向文件则返回所在路径的uri,uri指向目录则不处理直接返回原串;<br>uri指向的文件不存在或属性获取失败则返回空串。|
26| bool OH_FileUri_IsValidUri(const char *uri, unsigned int length) | 判断传入的uri的格式是否正确。仅校验uri是否满足系统定义的格式规范,不校验uri的有效性。|
27| FileManagement_ErrCode OH_FileUri_GetFileName(const char *uri, unsigned int length, char **result) | 通过传入的uri获取到对应的文件名称。(如果文件名中存在ASCII码将会被解码处理后拼接在原处)。|
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