鸿蒙5.0进阶开发:用户文件-获取并使用公共目录
往期鸿蒙5.0全套实战文章必看:(文中附带鸿蒙5.0全栈学习资料)
-
鸿蒙开发核心知识点,看这篇文章就够了
-
最新版!鸿蒙HarmonyOS Next应用开发实战学习路线
-
鸿蒙HarmonyOS NEXT开发技术最全学习路线指南
-
鸿蒙应用开发实战项目,看这一篇文章就够了(部分项目附源码)
通过 ArkTS 接口获取并访问公共目录
目录环境能力接口(ohos.file.environment)提供获取公共目录路径的能力,支持三方应用在公共文件用户目录下进行文件访问操作。
约束限制
- 使用此方式,需确认设备具有以下系统能力:SystemCapability.FileManagement.File.Environment.FolderObtain,当前仅支持2in1设备。
if (!canIUse(\'SystemCapability.FileManagement.File.Environment.FolderObtain\')) { console.error(\'this api is not supported on this device\'); return;}
- 公共目录获取接口仅用于获取公共目录路径,不对公共目录访问权限进行校验。若需访问公共目录需申请对应的公共目录访问权限。三方应用需要访问公共目录时,需通过弹窗授权向用户申请授予 Download 目录权限、Documents 目录权限或 Desktop 目录权限。
\"requestPermissions\" : [ \"ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY\", \"ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY\", ]
示例
-
获取公共目录路径。
import { BusinessError } from \'@kit.BasicServicesKit\'; import { Environment } from \'@kit.CoreFileKit\'; function getUserDirExample() { try { const downloadPath = Environment.getUserDownloadDir(); console.info(`success to getUserDownloadDir: ${downloadPath}`); const documentsPath = Environment.getUserDocumentDir(); console.info(`success to getUserDocumentDir: ${documentsPath}`); } catch (error) { const err: BusinessError = error as BusinessError; console.error(`failed to get user dir, because: ${JSON.stringify(err)}`); } }
-
以 Download 目录为例,访问 Download 目录下的文件。
import { BusinessError } from \'@kit.BasicServicesKit\'; import { Environment } from \'@kit.CoreFileKit\'; import { fileIo as fs } from \'@kit.CoreFileKit\'; import { common } from \'@kit.AbilityKit\'; function readUserDownloadDirExample() { // 检查是否具有 READ_WRITE_DOWNLOAD_DIRECTORY 权限,无权限则需要向用户申请授予权限。 try { // 获取 Download 目录 const downloadPath = Environment.getUserDownloadDir(); console.info(`success to getUserDownloadDir: ${downloadPath}`); const context = getContext() as common.UIAbilityContext; const dirPath = context.filesDir; console.info(`success to get filesDir: ${dirPath}`); // 查看 Download 目录下的文件并拷贝到沙箱目录中 let fileList: string[] = fs.listFileSync(downloadPath); fileList.forEach((file, index) => { console.info(`${downloadPath} ${index}: ${file}`); fs.copyFileSync(`${downloadPath}/${file}`, `${dirPath}/${file}`); }); // 查看沙箱目录下对应的文件 fileList = fs.listFileSync(dirPath); fileList.forEach((file, index) => { console.info(`${dirPath} ${index}: ${file}`); }); } catch (error) { const err: BusinessError = error as BusinessError; console.error(`Error code: ${err.code}, message: ${err.message}`); } }
-
以 Download 目录为例,保存文件到 Download 目录。
import { BusinessError } from \'@kit.BasicServicesKit\'; import { Environment } from \'@kit.CoreFileKit\'; import { fileIo as fs } from \'@kit.CoreFileKit\'; function writeUserDownloadDirExample() { // 检查是否具有 READ_WRITE_DOWNLOAD_DIRECTORY 权限,无权限则需要向用户申请授予权限。 try { // 获取 Download 目录 const downloadPath = Environment.getUserDownloadDir(); console.info(`success to getUserDownloadDir: ${downloadPath}`); // 保存 temp.txt 到 Download 目录下 const file = fs.openSync(`${downloadPath}/temp.txt`, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); fs.writeSync(file.fd, \'write a message\'); fs.closeSync(file); } catch (error) { const err: BusinessError = error as BusinessError; console.error(`Error code: ${err.code}, message: ${err.message}`); } }
通过 C/C++ 接口获取并使用公共目录
除了通过 ArkTS 访问公共目录的方式,也可通过 C/C++ 接口进行目录访问。
约束限制
- 使用此接口,需确认设备具有以下系统能力:SystemCapability.FileManagement.File.Environment.FolderObtain。
- 三方应用需要访问公共目录时,需通过弹窗授权向用户申请授予 Download 目录权限、Documents 目录权限或 Desktop 目录权限。
接口说明
接口的详细说明
开发步骤
在CMake脚本中链接动态库
CMakeLists.txt中添加以下lib。
target_link_libraries(sample PUBLIC libohenvironment.so libhilog_ndk.z.so)
添加头文件
#include #include #include
-
调用 OH_Environment_GetUserDownloadDir 接口获取用户 Download 目录沙箱路径,在接口中使用malloc申请的内存需要在使用完后释放因此需要free对应的内存。示例代码如下所示:
void GetUserDownloadDirExample(){ 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); }}
-
调用 OH_Environment_GetUserDownloadDir 接口获取用户 Download 目录沙箱路径,并查看 Download 目录下的文件。示例代码如下所示:
void ScanUserDownloadDirPathExample(){ // 获取 download 路径 char *downloadPath = nullptr; FileManagement_ErrCode ret = OH_Environment_GetUserDownloadDir(&downloadPath); if (ret == 0) { OH_LOG_INFO(LOG_APP, \"Download Path=%{public}s\", downloadPath); } else { OH_LOG_ERROR(LOG_APP, \"GetDownloadPath fail, error code is %{public}d\", ret); return; } // 查看文件夹下的文件 struct dirent **namelist = {nullptr}; int num = scandir(downloadPath, &namelist, nullptr, nullptr); if (num < 0) { free(downloadPath); OH_LOG_ERROR(LOG_APP, \"Failed to scan dir\"); return; } for (int i = 0; i d_name); } free(downloadPath); free(namelist);}
-
调用 OH_Environment_GetUserDownloadDir 接口获取用户 Download 目录沙箱路径,并保存 temp.txt 到 Download 目录下。示例代码如下所示:
void WriteUserDownloadDirPathExample(){ // 获取 download 路径 char *downloadPath = nullptr; FileManagement_ErrCode ret = OH_Environment_GetUserDownloadDir(&downloadPath); if (ret == 0) { OH_LOG_INFO(LOG_APP, \"Download Path=%{public}s\", downloadPath); } else { OH_LOG_ERROR(LOG_APP, \"GetDownloadPath fail, error code is %{public}d\", ret); return; } // 保存文件到 download 目录下 std::string filePath = std::string(downloadPath) + \"/temp.txt\"; free(downloadPath); std::ofstream outfile; outfile.open(filePath.c_str()); if (!outfile) { OH_LOG_ERROR(LOG_APP, \"Failed to open file\"); return; } std::string msg = \"Write a message\"; outfile.write(msg.c_str(), sizeof(msg)); outfile.close();}