鸿蒙5.0&next开发【获取并使用公共目录】文件基础服务开发

HarmonyOS通过接口获取并访问公共目录

通过 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;
    }
    ts
    
  • 公共目录获取接口仅用于获取公共目录路径,不对公共目录访问权限进行校验。若需访问公共目录需申请对应的公共目录访问权限。三方应用需要访问公共目录时,需通过弹窗授权向用户申请授予 Download 目录权限、Documents 目录权限或 Desktop 目录权限。

    "requestPermissions" : [
        "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY",
        "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY",
        "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY",
    ]
    json
    

示例

  1. 获取公共目录路径。

     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)}`);
         }
     }
    ts
    
  2. 以 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}`);
         }
     }
    ts
    
  3. 以 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}`);
         }
     }
    ts
    

通过 C/C++ 接口获取并使用公共目录

除了通过 ArkTS 访问公共目录的方式,也可通过 C/C++ 接口进行目录访问。

约束限制

  • 使用此接口,需确认设备具有以下系统能力:SystemCapability.FileManagement.File.Environment.FolderObtain。
  • 三方应用需要访问公共目录时,需通过弹窗授权向用户申请授予 Download 目录权限、Documents 目录权限或 Desktop 目录权限。

接口说明

接口名称描述
FileManagement_ErrCode OH_Environment_GetUserDownloadDir (char **result)获取用户Download目录沙箱路径。只支持2in1设备
FileManagement_ErrCode OH_Environment_GetUserDesktopDir (char **result)获取用户Desktop目录沙箱路径。只支持2in1设备
FileManagement_ErrCode OH_Environment_GetUserDocumentDir (char **result)获取用户Document目录沙箱路径。只支持2in1设备

开发步骤

在CMake脚本中链接动态库

CMakeLists.txt中添加以下lib。

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

添加头文件

#include <filemanagement/environment/oh_environment.h>
#include <filemanagement/fileio/oh_fileio.h>
#include <hilog/log.h>
c++ 
  1. 调用 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);
        }
    }
    c++ 
    
  2. 调用 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 < num; i++) {
            OH_LOG_INFO(LOG_APP, "%{public}s", namelist[i]->d_name);
        }
        free(downloadPath);
        free(namelist);
    }
    c++ 
    
  3. 调用 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();
    }
    
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值