• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# FileUri Development (C/C++)
2<!--Kit: Core File Kit-->
3<!--Subsystem: FileManagement-->
4<!--Owner: @lvzhenjie-->
5<!--Designer: @wang_zhangjun; @chenxi0605-->
6<!--Tester: @liuhonggang123-->
7<!--Adviser: @foryourself-->
8
9## When to Use
10
11FileUri 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.
12
13## Basic Concepts
14
15Result set: path or URI that meets the service requirements.
16
17## Constraints
18
19- 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.
20
21- To ensure data accuracy, only one object can be processed during the conversion or verification of a URI.
22
23## Available APIs
24
25For details about the APIs, see [API Reference](../reference/apis-core-file-kit/capi-oh-file-uri-h.md).
26
27| API| Description|
28| -------- |-------|
29| FileManagement_ErrCode OH_FileUri_GetUriFromPath(const char *path, unsigned int length, char **result)| Converts the input path to the URI of the application.<br>In this process, Chinese characters and non-digit characters in the path will be encoded to their corresponding ASCII codes and concatenated to the URI.|
30| 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.|
31| 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.|
32| 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.|
33| 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.)|
34
35## How to Develop
36
37**Linking the Dynamic Library in the CMake Script**
38
39Add the following library to **CMakeLists.txt**.
40
41```txt
42target_link_libraries(sample PUBLIC libohfileuri.so)
43```
44
45**Adding the Header File**
46
47```c++
48#include <filemanagement/file_uri/oh_file_uri.h>
49```
50
511. Call **OH_FileUri_GetUriFromPath** to obtain the URI from a path. The memory allocated must be released using **free()**. <br>Example:
52
53    ```c
54    #include <cstring>
55
56    void OH_FileUri_GetUriFromPathExample() {
57        char *path = "/data/storage/el2/base/files/test.txt";
58        unsigned int length = strlen(path);
59        char *uriResult = NULL;
60        FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(path, length ,&uriResult);
61        if (ret == 0 && uriResult !=NULL) {
62            printf("pathUri=%s", uriResult); // The URI obtained by application a is file://com.example.demo/data/storage/el2/base/files/test.txt.
63        }
64        if (uriResult != NULL) {
65            free(uriResult);
66        }
67    }
68    ```
69
702. Call **OH_FileUri_GetPathFromUri** to convert the URI into a path. The memory allocated must be released using **free()**. <br>Example:
71
72    ```c
73    #include <cstring>
74
75    void OH_FileUri_GetPathFromUriExample() {
76        char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
77        unsigned int length = strlen(uri);
78        char *pathResult = NULL;
79        FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(uri, length, &pathResult);
80        if (ret == 0 && pathResult != NULL) {
81            printf("pathResult=%s", pathResult); // PathResult is /data/storage/el2/base/files/test.txt.
82        }
83        if (pathResult != NULL) {
84            free(pathResult);
85        }
86    }
87    ```
88
893. 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:
90
91    ```c
92    #include <cstring>
93
94    void OH_FileUri_GetFullDirectoryUriExample() {
95        char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
96        unsigned int length = strlen(uri);
97        char *uriResult = NULL;
98        FileManagement_ErrCode ret = OH_FileUri_GetFullDirectoryUri(uri, length, &uriResult);
99        if (ret == 0 && uriResult != NULL) {
100            printf("pathUri=%s",uriResult);// The URI obtained is file://com.example.demo/data/storage/el2/base/files/.
101        }
102        if (uriResult != NULL) {
103            free(uriResult);
104        }
105    }
106    ```
107
1084. Call **OH_FileUri_IsValidUri** to check whether a URI is valid. <br>Example:
109
110   ```c
111    #include <cstring>
112
113    void OH_FileUri_IsValidUriExample() {
114        char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
115        unsigned int length = strlen(uri);
116        bool flags = OH_FileUri_IsValidUri(uri, length);
117        printf("The URI is valid? flags=%d", flags);
118    }
119   ```
120
1215. Call **OH_FileUri_GetFileName** to obtain the file name from the URI. The memory allocated must be released using **free()**. <br>Example:
122
123    ```c
124    #include <cstring>
125
126    void OH_FileUri_GetFileNameExample() {
127        char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
128        unsigned int length = strlen(uri);
129        char *uriResult = NULL;
130        FileManagement_ErrCode ret = OH_FileUri_GetFileName(uri, length, &uriResult);
131        if (ret == 0 && uriResult != NULL) {
132            printf("pathUri=%s",uriResult);// Obtain the file name test.txt from the URI.
133        }
134        if (uriResult != NULL) {
135            free(uriResult);
136        }
137    }
138    ```
139