目录

Obtaining the User Directory Environment (C/C++)

When to Use

You can use Environment to allow a third-party application to access files in a user directory.

Constraints

Available APIs

For details about the APIs, see oh_environment.h.

API Name Description
FileManagement_ErrCode OH_Environment_GetUserDownloadDir (char **result)

Obtains the sandbox path of the user's Download directory. Supported on 2-in-1 devices.

Supported on tablet devices starting from API version 26.0.0.

FileManagement_ErrCode OH_Environment_GetUserDesktopDir (char **result)

Obtains the sandbox path of the user's Desktop directory. Supported on 2-in-1 devices.

Supported on tablet devices starting from API version 26.0.0.

FileManagement_ErrCode OH_Environment_GetUserDocumentDir (char **result)

Obtains the sandbox path of the user's Document directory. Supported on 2-in-1 devices.

Supported on tablet devices starting from API version 26.0.0.

How to Develop

Adding the Dynamic Link Library

Add the following library to CMakeLists.txt.

target_link_libraries(sample PUBLIC libohenvironment.so libhilog_ndk.z.so)

Adding Header Files

#include <cstdlib>
#include <filemanagement/environment/oh_environment.h>
#include <filemanagement/fileio/oh_fileio.h>
#include <hilog/log.h>
  1. Call OH_Environment_GetUserDownloadDir to obtain the sandbox path of the user Download directory. The memory allocated must be released using free().

    Example:

    void GetUserDownloadDirPathExample()
    {
        char *downloadPath = nullptr;
        FileManagement_ErrCode ret = OH_Environment_GetUserDownloadDir(&downloadPath);
        if (ret == 0) {
            OH_LOG_INFO(LOG_APP, "Succeeded in getting user download directory, path=%{public}s", downloadPath);
        } else {
            OH_LOG_ERROR(LOG_APP, "Failed to get download path, error code is %{public}d", ret);
        }
        free(downloadPath);
    }
  2. Call OH_Environment_GetUserDesktopDir to obtain the sandbox path of the user Desktop directory. The memory allocated must be released using free().

    Example:

    void GetUserDesktopDirPathExample()
    {
        char *desktopPath = nullptr;
        FileManagement_ErrCode ret = OH_Environment_GetUserDesktopDir(&desktopPath);
        if (ret == 0) {
            OH_LOG_INFO(LOG_APP, "Succeeded in getting user desktop directory, path=%{public}s", desktopPath);
        } else {
            OH_LOG_ERROR(LOG_APP, "Failed to get user desktop path, error code is %{public}d", ret);
        }
        free(desktopPath);
    }
  3. Call OH_Environment_GetUserDocumentDir to obtain the sandbox path of the user Document directory. The memory allocated must be released using free().

    Example:

    void GetUserDocumentDirPathExample()
    {
        char *documentPath = nullptr;
        FileManagement_ErrCode ret = OH_Environment_GetUserDocumentDir(&documentPath);
        if (ret == 0) {
            OH_LOG_INFO(LOG_APP, "Succeeded in getting user document directory, path=%{public}s", documentPath);
        } else {
            OH_LOG_ERROR(LOG_APP, "Failed to get user document path, error code is %{public}d", ret);
        }
        free(documentPath);
    }
  4. Call OH_Environment_GetUserDocumentDir to obtain the sandbox path of the user Document directory, and use the stat function to determine the space of the Document directory.

    Example:

    Include the following header file before using the API.

    #include <sys/stat.h>
    void GetUserDownloadDirSizeExample()
    {
        char *documentPath = nullptr;
        FileManagement_ErrCode ret = OH_Environment_GetUserDocumentDir(&documentPath);
        if (ret == 0) {
            OH_LOG_INFO(LOG_APP, "Succeeded in getting user document directory, path=%{public}s", documentPath);
            struct stat fileStat;
            int result = stat(documentPath, &fileStat);
            if (result == 0) {
                OH_LOG_INFO(LOG_APP, "Succeeded in getting file info. document Size=%{public}ld", fileStat.st_size);
            } else {
                OH_LOG_ERROR(LOG_APP, "Failed to stat user document directory, error code is %{public}d", result);
            }
        } else {
            OH_LOG_ERROR(LOG_APP, "Failed to get user document directory, error code is %{public}d", ret);
        }
        free(documentPath);
    }