Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.
HarmonyOS
You can use Environment to allow a third-party application to access files in a user directory.
For details about the APIs, see oh_environment.h.
| API | Description |
|---|---|
| FileManagement_ErrCode OH_Environment_GetUserDownloadDir (char **result) | Obtains the sandbox path of the Download directory. This function supports only 2-in-1 devices. |
| FileManagement_ErrCode OH_Environment_GetUserDesktopDir (char **result) | Obtains the sandbox path of the Desktop directory. This function supports only 2-in-1 devices. |
| FileManagement_ErrCode OH_Environment_GetUserDocumentDir (char **result) | Obtains the sandbox path of the Documents directory. This function supports only 2-in-1 devices. |
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>
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, "Download Path=%{public}s", downloadPath);
free(downloadPath);
} else {
OH_LOG_ERROR(LOG_APP, "GetDownloadPath fail, error code is %{public}d", ret);
}
}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, "Desktop Path=%{public}s", desktopPath);
free(desktopPath);
} else {
OH_LOG_ERROR(LOG_APP, "GetDesktopPath fail, error code is %{public}d", ret);
}
}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, "Document Path=%{public}s", documentPath);
free(documentPath);
} else {
OH_LOG_ERROR(LOG_APP, "GetDocumentPath fail, error code is %{public}d", ret);
}
}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, "Document Path=%{public}s", documentPath);
struct stat fileStat;
int result = stat(documentPath, &fileStat);
if (result == 0) {
OH_LOG_INFO(LOG_APP, "Document Size=%{public}ld", fileStat.st_size);
} else {
OH_LOG_ERROR(LOG_APP, "GetDocumentSize fail, error code is %{public}ld", result);
}
free(documentPath);
} else {
OH_LOG_ERROR(LOG_APP, "GetDocumentPath fail, error code is %{public}d", ret);
}
}Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
Quick start
Helps you find desired resources with ease.