HarmonyOS Native侧怎么样从env中获取resMgr?

想在Native侧使用rawfile的接口,不涉及ArkTS交互。通过Napi作为桥梁,获取到env,再从env中获取到this,再从this中获取到context,但是从context获取到resourceManager的时候,返回resourceManager为null。native侧怎么样从env中获取到resourceManager?

示例代码:

static napi_env s_Env = NULL;
static napi_value s_Context = NULL;
static napi_value s_ResourceManager = NULL;
static NativeResourceManager *s_NativeResourceManager = NULL;


napi_env& GetNapiEnv() {
  if (s_Env == NULL) {
    OH_LOG_Print(LOG_APP, LOG_ERROR, 1, "hxtest", "env is null");
  }
  OH_LOG_Print(LOG_APP, LOG_ERROR, 1, "hxtest", "env is not null");
  return s_Env;

}

static napi_value GetGlobal() {
  napi_env &env = GetNapiEnv();
  napi_status status;

  napi_value global;
  status = napi_get_global(env, &global);
  status = napi_get_named_property(env, global, "globalThis", &global);
  if ( status != napi_ok) {
    OH_LOG_Print(LOG_APP, LOG_ERROR, 1, "hxtest", "get globalThis failed");
  }

  return global;
}

napi_value& GetContext() {
  if (s_Context == NULL) {
    napi_env &env = GetNapiEnv();
    napi_status status;

    napi_value global = GetGlobal();
    napi_value context;
    status = napi_get_named_property(env, global, "context", &context);
    if (status == napi_ok) {
      OH_LOG_Print(LOG_APP, LOG_ERROR, 1, "hxtest", "context is not null");
      napi_valuetype valuetype_;
      status = napi_typeof(env, context, &valuetype_);
      if (status == napi_ok && valuetype_ == napi_object) {
        s_Context = context;
      }
    }
    if ( context == NULL) {
      OH_LOG_Print(LOG_APP, LOG_ERROR, 1, "hxtest", "context is  null");
    }
  }
  return s_Context;
}

napi_value& GetResourceManager() {
  if (s_ResourceManager == NULL) {
    napi_env &env = GetNapiEnv();

    napi_value &context = GetContext();
    napi_value resourceManager;
    napi_status status = napi_get_named_property(env, context, "resourceManager", &resourceManager);

    if (status == napi_ok) {
      napi_valuetype valuetype_;
      OH_LOG_Print(LOG_APP, LOG_ERROR, 1, "hxtest", "resource manager get");
      status = napi_typeof(env, resourceManager, &valuetype_);
      if (status == napi_ok && valuetype_ == napi_object) {
        s_ResourceManager = resourceManager;
      }
    }
    if (s_ResourceManager == NULL) {
      OH_LOG_Print(LOG_APP, LOG_ERROR, 1, "hxtest", "resource manager is null");
    }
  }
  return s_ResourceManager;
}

NativeResourceManager* GetNativeResourceManager() {
  if (s_NativeResourceManager == NULL) {
    napi_env &env = GetNapiEnv();
    napi_value &resMgr = GetResourceManager();
    s_NativeResourceManager = OH_ResourceManager_InitNativeResourceManager(env, resMgr);
  }
  return s_NativeResourceManager;
}

RawFile* asset_open(const char *pathValue) {
  NativeResourceManager *resMgr = GetNativeResourceManager();
  RawFile *rawFile = OH_ResourceManager_OpenRawFile(resMgr, pathValue);
  if (rawFile == NULL) {
    OH_LOG_Print(LOG_APP, LOG_ERROR, 1, "hxtest", "raw file is null");
    return NULL;
  }
  OH_LOG_Print(LOG_APP, LOG_ERROR, 1, "hxtest", "raw file is not null");
  return rawFile;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
  // 通过manager初始化xcomponent,并获取native window
  PluginManager::GetInstance()->Export(env, exports);
  s_Env = env;

  const char *path = "test.txt";
  asset_open(path);

  return exports;
}
EXTERN_C_END
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
HarmonyOS
2024-12-23 15:45:08
578浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

创建对应的hsp的资源对象。

分享
微博
QQ
微信
回复
2024-12-23 19:31:51
相关问题
怎么样获取后台弹出界面的权限?
6327浏览 • 1回复 待解决
HarmonyOS 怎么样使线程休眠
963浏览 • 1回复 待解决
鸿蒙系统怎么样好用吗
7浏览 • 0回复 待解决
怎么样设置代理setup proxy
383浏览 • 0回复 待解决
鸿蒙系统还用吗怎么样
2浏览 • 0回复 待解决
FD自由开发者平台怎么样
18518浏览 • 1回复 待解决
native的log获取不到
2194浏览 • 1回复 待解决
关于ArkTS的线程机制是怎么样的?
1473浏览 • 2回复 待解决
PolarDB向量化执行架构是怎么样的?
3964浏览 • 1回复 待解决
Redis 的内存用光了会怎么样呢?
4060浏览 • 1回复 待解决
效果怎么样
448浏览 • 2回复 已解决
鸿蒙系统的未来发展是怎么样
5055浏览 • 1回复 待解决
鸿蒙对渲染WebGL的支持目前怎么样
3495浏览 • 1回复 待解决
鸿蒙的刷新机制是怎么样的?
1658浏览 • 1回复 待解决
如何在Native获取APP版本信息
3028浏览 • 1回复 待解决