目录

获取用户目录环境(C/C++)

场景介绍

Environment提供了获取公共文件用户目录路径的能力,以支持三方应用在公共文件用户目录下进行文件访问操作。

约束限制

接口说明

接口的详细说明,请参考oh_environment.h。

接口名称 描述
FileManagement_ErrCode OH_Environment_GetUserDownloadDir (char **result)

获取用户Download目录沙箱路径。支持2in1设备。

从API版本26.0.0开始,支持tablet设备。

FileManagement_ErrCode OH_Environment_GetUserDesktopDir (char **result)

获取用户Desktop目录沙箱路径。支持2in1设备。

从API版本26.0.0开始,支持tablet设备。

FileManagement_ErrCode OH_Environment_GetUserDocumentDir (char **result)

获取用户Document目录沙箱路径。支持2in1设备。

从API版本26.0.0开始,支持tablet设备。

开发步骤

在CMake脚本中链接动态库

CMakeLists.txt中添加以下lib。

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

添加头文件

#include <cstdlib>
#include <filemanagement/environment/oh_environment.h>
#include <filemanagement/fileio/oh_fileio.h>
#include <hilog/log.h>
  1. 调用OH_Environment_GetUserDownloadDir接口获取用户Download目录沙箱路径,在接口中使用malloc申请的内存需要在使用完后释放因此需要free对应的内存。示例代码如下所示:

    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. 调用OH_Environment_GetUserDesktopDir接口获取用户Desktop目录沙箱路径,在接口中使用malloc申请的内存需要在使用完后释放因此需要free对应的内存。示例代码如下所示:

    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. 调用OH_Environment_GetUserDocumentDir接口获取用户Document目录沙箱路径,在接口中使用malloc申请的内存需要在使用完后释放因此需要free对应的内存。示例代码如下所示:

    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. 调用OH_Environment_GetUserDocumentDir接口获取用户Document目录沙箱路径,使用stat函数判断Document目录空间大小。示例代码如下所示:

    使用前需要引入如下头文件:

    #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);
    }