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
阅读 399
1 个回答

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏