HarmonyOS 鸿蒙Next中有类似于assets的目录吗,需要在cpp文件中访问资源文件.ttf

HarmonyOS 鸿蒙Next中有类似于assets的目录吗,需要在cpp文件中访问资源文件.ttf 在学习freetype纹理贴图来实现绘制字体,试了许多方法没法访问到字体文件,最后放到应用沙箱里面才能读取到,但是绘制效果只有一根根线。

沙箱代码:

copy_rawfile__to_sandbox() {
    try {
      let context = getContext(this)
      let file = context.filesDir + "/arial.ttf";
      let sss = fs.createStreamSync(file, "w"); //没有会创建一个空的input.txt
      sss.closeSync();

      //获取rawfile下input.txt
      getContext(this).resourceManager.getRawFileDescriptor('rawfile/HarmonyOS_Sans_Black.ttf', (error, value) => {
        if (error != null) { //getRawFileDescriptor运行失败
          console.info("[rawfile_copy_to_sandbox] ———————————————————————————————————————————————————————");
          console.log("[rawfile_copy_to_sandbox] getRawFileDescriptor api 运行失败: ${error.code}, message: ${error.message}.");
          console.log("[rawfile_copy_to_sandbox] 未能成功将rawfile下的文件拷贝到应用沙箱下 ");
        } else { //getRawFileDescriptor运行成功
          let fd = value.fd;
          fs.copyFileSync(fd, file);
          Logger.info("[rawfile]", file)
          console.info("[rawfile_copy_to_sandbox] ———————————————————————————————————————————————————————");
          console.log("[rawfile_copy_to_sandbox] getRawFileDescriptor api 运行成功");
          console.log("[rawfile_copy_to_sandbox] 成功将rawfile下的文件拷贝到应用沙箱下");
        }
      });
    } catch (error) {
      console.info("[rawfile_copy_to_sandbox] ———————————————————————————————————————————————————————");
      console.info("[rawfile_copy_to_sandbox] getRawFileDescriptor api 运行失败" + error);
      console.log("[rawfile_copy_to_sandbox] 未能成功将rawfile下的文件拷贝到应用沙箱下");
    }
}

更多关于HarmonyOS 鸿蒙Next中有类似于assets的目录吗,需要在cpp文件中访问资源文件.ttf的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复
src/main/resources/rawfile/font/xxx.otf

更多关于HarmonyOS 鸿蒙Next中有类似于assets的目录吗,需要在cpp文件中访问资源文件.ttf的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


换个问题,rawfile的路径是怎样的

在HarmonyOS(鸿蒙)Next中,资源文件的管理和访问方式与Android的assets目录有所不同。鸿蒙Next采用了一种基于资源标识符(Resource ID)的资源管理机制。对于需要在C++文件中访问的资源文件(如.ttf字体文件),可以通过以下步骤实现:

  1. 资源文件放置:将.ttf文件放置在resources目录下的rawfile子目录中。例如,resources/rawfile/myfont.ttf

  2. 资源访问:在C++代码中,使用鸿蒙提供的资源管理API来访问这些文件。可以通过OH_ResourceManager接口获取资源文件的路径或数据。

  3. 代码示例:在C++文件中,可以使用如下代码来获取资源文件的路径或数据:

#include "resource_manager.h"

// 获取资源管理器实例
OH_ResourceManager* resourceManager = OH_ResourceManager_GetInstance();

// 获取资源文件的路径
const char* resourcePath = OH_ResourceManager_GetRawFilePath(resourceManager, "myfont.ttf");

// 使用资源路径进行后续操作
回到顶部