DEV Community

Cover image for FileUri Development guidance
liu yang
liu yang

Posted on • Edited on

FileUri Development guidance

Development Steps for File URI Management in HarmonyOS

Linking Dynamic Library in CMake Script

To manage file URIs in HarmonyOS, you need to link the appropriate dynamic library in your CMakeLists.txt file. This ensures that your application has access to the necessary functions for file URI operations.

target_link_libraries(sample PUBLIC libohfileuri.so)
Enter fullscreen mode Exit fullscreen mode

Explanation

  • target_link_libraries: This CMake command links the specified libraries to the target. In this case, sample is the target (your application), and libohfileuri.so is the dynamic library that provides file URI management functions.

Including Header File

To use the file URI management functions, you need to include the appropriate header file in your C++ code.

#include <filemanagement/file_uri/oh_file_uri.h>
Enter fullscreen mode Exit fullscreen mode

Explanation

  • oh_file_uri.h: This header file contains the declarations for the file URI management functions, such as OH_FileUri_GetUriFromPath, OH_FileUri_GetPathFromUri, and others.

Calling OH_FileUri_GetUriFromPath

This function converts a file path to a URI.

#include <cstring>

void OH_FileUri_GetUriFromPathExample() {
    char *path = "/data/storage/el2/base/files/test.txt";
    unsigned int length = strlen(path);
    char *uriResult = NULL;
    FileManagement_ErrCode ret = OH_FileUri_GetUriFromPath(path, length, &uriResult); 
    if (ret == 0 && uriResult != NULL) {
        printf("pathUri=%s", uriResult);
    }
    if (uriResult != NULL) {
        free(uriResult);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • OH_FileUri_GetUriFromPath: This function takes a file path and its length, and converts it to a URI. The resulting URI is stored in uriResult.
  • FileManagement_ErrCode: This is the error code returned by the function. A return value of 0 indicates success.
  • Memory Management: The uriResult is dynamically allocated, so it must be freed after use to avoid memory leaks.

Calling OH_FileUri_GetPathFromUri

This function converts a URI back to a file path.

#include <cstring>

void OH_FileUri_GetPathFromUriExample() {
    char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
    unsigned int length = strlen(uri);
    char *pathResult = NULL;
    FileManagement_ErrCode ret = OH_FileUri_GetPathFromUri(uri, length, &pathResult);
    if (ret == 0 && pathResult != NULL) {
        printf("pathResult=%s", pathResult);
    }
    if (pathResult != NULL) {
        free(pathResult);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • OH_FileUri_GetPathFromUri: This function takes a URI and its length, and converts it back to a file path. The resulting path is stored in pathResult.
  • Memory Management: Similar to the previous example, pathResult must be freed after use.

Calling OH_FileUri_GetFullDirectoryUri

This function retrieves the full directory URI from a given URI.

#include <cstring>

void OH_FileUri_GetFullDirectoryUriExample() {
    char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
    unsigned int length = strlen(uri);
    char *uriResult = NULL;
    FileManagement_ErrCode ret = OH_FileUri_GetFullDirectoryUri(uri, length, &uriResult);
    if (ret == 0 && uriResult != NULL) {
        printf("pathUri=%s", uriResult);
    }
    if (uriResult != NULL) {
        free(uriResult);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • OH_FileUri_GetFullDirectoryUri: This function extracts the full directory URI from the given URI. The resulting URI is stored in uriResult.
  • Memory Management: The uriResult must be freed after use to avoid memory leaks.

Calling OH_FileUri_IsValidUri

This function checks if a given URI is valid.

#include <cstring>

void OH_FileUri_IsValidUriExample() {
    char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
    unsigned int length = strlen(uri);
    bool flags = OH_FileUri_IsValidUri(uri, length);
    printf("The URI is valid? flags=%d", flags);
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • OH_FileUri_IsValidUri: This function checks if the provided URI is valid. It returns true if the URI is valid, otherwise false.

Calling OH_FileUri_GetFileName

This function extracts the file name from a given URI.

#include <cstring>

void OH_FileUri_GetFileNameExample() {
    char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
    unsigned int length = strlen(uri);
    char *uriResult = NULL;
    FileManagement_ErrCode ret = OH_FileUri_GetFileName(uri, length, &uriResult);
    if (ret == 0 && uriResult != NULL) {
        printf("pathUri=%s", uriResult);
    }
    if (uriResult != NULL) {
        free(uriResult);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • OH_FileUri_GetFileName: This function extracts the file name from the given URI. The resulting file name is stored in uriResult.
  • Memory Management: The uriResult must be freed after use to avoid memory leaks.

Practical Use Cases

Secure File Access

  • Secure Storage: Use these functions to securely store and access sensitive files by converting paths to URIs and vice versa.
  • User Control: Allow users to control which files are accessible by the application, enhancing privacy and security.

Multi-Device Synchronization

  • File Synchronization: Ensure that files are synchronized across multiple devices by converting paths to URIs, allowing seamless access.
  • Cross-Device Access: Enable cross-device access to files, ensuring that users can access their data from any device running the same application.

Top comments (0)