想在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
创建对应的hsp的资源对象。