Android는 assetManger를 통해서 필요한 에셋(모델 파일 등)을 로드해야합니다. 이를 위해 JNI를 활용하여 C++코드로 Android JAVA에서 제공하는 assetManager를 로드하여 모델파일을 읽어 올 수 있습니다.
void load_asset_to_buf(JNIEnv *env, jobject context, const std::string& asset_path,
uint8_t *&buf, size_t &buf_length) {
jclass classContext = env->FindClass("android/content/Context");
if (!env->IsInstanceOf(context, classContext)) {
env->DeleteLocalRef(classContext);
return;
}
jmethodID methodGetAssets = env->GetMethodID(classContext,"getAssets", "()Landroid/content/res/AssetManager;");
jobject asset_manager = env->CallObjectMethod(context, methodGetAssets);
AAssetManager *mgr = AAssetManager_fromJava(env, asset_manager);
if (!mgr) {
return;
}
AAsset *asset = AAssetManager_open(mgr, asset_path.c_str(), AASSET_MODE_UNKNOWN);
if (!asset) {
AAsset_close(asset);
return;
}
buf_length = AAsset_getLength64(asset);
if (buf_length < 1) {
AAsset_close(asset);
return;
}
if (buf != nullptr) {
delete[] buf;
}
buf = new (std::nothrow) uint8_t [buf_length];
if (buf == nullptr) {
AAsset_close(asset);
return;
}
off_t new_position = AAsset_read(asset, buf, static_cast<size_t>(buf_length));
if (new_position < 0) {
delete[] buf;
buf = nullptr;
AAsset_close(asset);
return;
}
AAsset_close(asset);
return;
}
JavaScript
복사