FileUri Development (C/C++)
When to Use
FileUri 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.
Basic Concepts
Result set: path or URI that meets the service requirements.
Constraints
-
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.
-
To ensure data accuracy, only one object can be processed during the conversion or verification of a URI.
Available APIs
For details about the APIs, see API Reference.
| API | Description |
|---|---|
| FileManagement_ErrCode OH_FileUri_GetUriFromPath(const char *path, unsigned int length, char **result) | Converts the input path to the URI of the application. 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. |
| FileManagement_ErrCode OH_FileUri_GetPathFromUri(const char *uri, unsigned int length, char **result) | Converts the URI to the corresponding sandbox path. 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. 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. |
| FileManagement_ErrCode OH_FileUri_GetFullDirectoryUri(const char *uri, unsigned int length, char **result) | Obtains the URI of the path. 1. If the URI points to a file, the URI of the path is returned. 2. If the URI points to a directory, the original string is returned without processing. 3. If the file specified by the URI does not exist or the property fails to be obtained, an empty string is returned. |
| 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. |
| FileManagement_ErrCode OH_FileUri_GetFileName(const char *uri, unsigned int length, char **result) | Obtains the file name based on the input URI. (If the file name contains ASCII codes, the ASCII codes will be decoded and concatenated to the original position.) |
How to Develop
Linking the Dynamic Library in the CMake Script
Add the following library to CMakeLists.txt.
target_link_libraries(sample PUBLIC libohfileuri.so)
Adding the Header File
#include <filemanagement/file_uri/oh_file_uri.h>
-
Call OH_FileUri_GetUriFromPath to obtain the URI from a path. The memory allocated must be released using free().
Example:static napi_value NAPI_Global_OH_FileUri_GetUriFromPathExample(napi_env env, napi_callback_info info) { // ... // Allocate memory for char* uri. char *path = new char[strLength + 1]; // +1 for null terminator // Copy the JavaScript string to uri. // ... unsigned int length = strlen(path); // Output the input path string. // ... char *uriResult = nullptr; FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(path, length, &uriResult); // Output the result URI string. // ... if (ret == 0 && uriResult != nullptr) { // Convert the C string to napi_value. napi_status status = napi_create_string_utf8(env, uriResult, NAPI_AUTO_LENGTH, &result); if (status != napi_ok) { free(uriResult); return nullptr; } free(uriResult); // Release the temporary string. } else { // Convert the C string to napi_value. napi_status status = napi_create_string_utf8(env, "Hello World", NAPI_AUTO_LENGTH, &result); if (status != napi_ok) { return nullptr; } } return result; } -
Call OH_FileUri_GetPathFromUri to convert the URI into a path. The memory allocated must be released using free(). The sample code is as follows:
static napi_value NAPI_Global_OH_FileUri_GetPathFromUriExample(napi_env env, napi_callback_info info) { // ... char *uri = new char[strLength + 1]; // +1 for null terminator // Copy the JavaScript string to uri. napi_get_value_string_utf8(env, args[0], uri, strLength + 1, &strLength); unsigned int length = strlen(uri); // Output the input URI string. OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.WatcherType=OnTrigger: %{public}s", uri); char *pathResult = nullptr; FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(uri, length, &pathResult); // Output the result string of the obtained path. // ... if (ret == 0 && pathResult != nullptr) { // Convert the C string to napi_value. napi_status status = napi_create_string_utf8(env, pathResult, NAPI_AUTO_LENGTH, &result); if (status != napi_ok) { free(pathResult); return nullptr; } free(pathResult); // Release the temporary string. } else { // Convert the empty string to napi_value. napi_status status = napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &result); if (status != napi_ok) { return nullptr; } } return result; } -
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(). The sample code is as follows:
static napi_value NAPI_Global_OH_FileUri_GetFullDirectoryUriExample(napi_env env, napi_callback_info info) { // ... char *uri = new char[strLength + 1]; // +1 for null terminator // Copy the JavaScript string to uri. napi_get_value_string_utf8(env, args[0], uri, strLength + 1, &strLength); unsigned int length = strlen(uri); // Output the input URI string. OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.WatcherType=OnTrigger: %{public}s", uri); char *uriResult = nullptr; FileManagement_ErrCode ret = OH_FileUri_GetFullDirectoryUri(uri, length, &uriResult); // Output the URI string of the path. // ... if (ret == 0 && uriResult != nullptr) { // Use NAPIs to create a napi_value of the string type to return the correct result. napi_create_string_utf8(env, uriResult, NAPI_AUTO_LENGTH, &result); } else { // Use NAPIs to create a napi_value of the null type to return the error or null value. napi_get_null(env, &result); } if (uriResult != nullptr) { free(uriResult); } return result; } -
Call OH_FileUri_IsValidUri to check whether a URI is valid. The sample code is as follows:
static napi_value NAPI_Global_OH_FileUri_IsValidUriExample(napi_env env, napi_callback_info info) { // ... char *uri = new char[strLength + 1]; // +1 for null terminator // Copy the JavaScript string to uri. napi_get_value_string_utf8(env, args[0], uri, strLength + 1, &strLength); unsigned int length = strlen(uri); // Output the input URI string. OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.WatcherType=OnTrigger: %{public}s", uri); bool flags = OH_FileUri_IsValidUri(uri, length); // ... } -
Call OH_FileUri_GetFileName to obtain the file name from the URI. The memory allocated must be released using free(). The sample code is as follows:
static napi_value NAPI_Global_OH_FileUri_GetFileNameExample(napi_env env, napi_callback_info info) { // ... char *uri = new char[strLength + 1]; // +1 for null terminator // Copy the JavaScript string to uri. napi_get_value_string_utf8(env, args[0], uri, strLength + 1, &strLength); unsigned int length = strlen(uri); // Output the input URI string. OH_LOG_INFO(LogType::LOG_APP, "HiAppEvent eventInfo.WatcherType=OnTrigger: %{public}s", uri); char *uriResult = nullptr; FileManagement_ErrCode ret = OH_FileUri_GetFileName(uri, length, &uriResult); // Output the obtained file name. // ... if (ret == 0 && uriResult != nullptr) { // Convert the C string to napi_value. napi_status status = napi_create_string_utf8(env, uriResult, NAPI_AUTO_LENGTH, &result); if (status != napi_ok) { free(uriResult); return NULL; } free(uriResult); // Release the temporary string. } else { // Convert the empty string to napi_value. napi_status status = napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &result); if (status != napi_ok) { return nullptr; } } return result; }