• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# FileUri Development (C/C++)
2
3## When to Use
4
5FileUri provides APIs for basic file URI operations, such as converting URIs to sandbox paths, converting sandbox paths to URIs, and obtaining URIs of directories where the specified URIs are located, facilitating URI access in file sharing services.
6
7## Basic Concepts
8
9**result**: path or URI that meets the service requirements.
10
11## Constraints
12
13- When converting a URI to a path, you are advised to use the system capability to obtain the URI source, for example, the URI returned by the picker, clipboard, dragging, and path-to-URI APIs provided by the system. If the URI combined by applications or users is converted, the converted path may fail to be accessed.
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)| Generates the application URI based on the input path. When a path is converted to a URI, Chinese characters and non-digit characters in the path are compiled into the corresponding ASCII code and combined into the URI.|
24| FileManagement_ErrCode OH_FileUri_GetPathFromUri(const char *uri, unsigned int length, char **result) | Converts the URI to the corresponding sandbox path.<br>1. During URI-to-path conversion, the ASCII code in the URI is decoded and then concatenated to the original position. The URI generated by a non-system API may contain characters beyond the ASCII code parsing range. As a result, the string cannot be concatenated.<br>2. The conversion is performed based on the string replacement rule specified by the system (the rule may change with the system evolution). During the conversion, the path is not verified, so that the conversion result may not be accessible.|
25| FileManagement_ErrCode OH_FileUri_GetFullDirectoryUri(const char *uri, unsigned int length, char **result) | Obtains the URI of the path.<br>If the URI points to a file, the URI of the path is returned. If the URI points to a directory, the original string is returned without processing.<br>If the file specified by the URI does not exist or the attribute fails to be obtained, an empty string is returned.|
26| bool OH_FileUri_IsValidUri(const char *uri, unsigned int length) | Checks whether the format of the input URI is correct. The system only checks whether the URI meets the format specifications defined by the system. The validity of the URI is not verified.|
27| FileManagement_ErrCode OH_FileUri_GetFileName(const char *uri, unsigned int length, char **result) | Obtains the file name based on the given URI. (The ASCII code in the file name will be decoded and then concatenated to the original position.)|
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