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
HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

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

分享
微博
QQ
微信
回复
3天前
相关问题
怎么样获取后台弹出界面的权限?
5273浏览 • 1回复 待解决
HarmonyOS 怎么样使线程休眠
93浏览 • 1回复 待解决
鸿蒙系统怎么样好用吗
7浏览 • 0回复 待解决
鸿蒙系统还用吗怎么样
2浏览 • 0回复 待解决
FD自由开发者平台怎么样
17230浏览 • 1回复 待解决
native的log获取不到
1662浏览 • 1回复 待解决
关于ArkTS的线程机制是怎么样的?
526浏览 • 2回复 待解决
PolarDB向量化执行架构是怎么样的?
3144浏览 • 1回复 待解决
鸿蒙系统的未来发展是怎么样
3527浏览 • 1回复 待解决
Redis 的内存用光了会怎么样呢?
3124浏览 • 1回复 待解决
鸿蒙对渲染WebGL的支持目前怎么样
2464浏览 • 1回复 待解决
鸿蒙的刷新机制是怎么样的?
641浏览 • 1回复 待解决
如何在Native获取APP版本信息
2324浏览 • 1回复 待解决