我有一些代码:
AAsset* pAsset = AAssetManager_open(pAssetManager, "asset_test.txt", AASSET_MODE_STREAMING);
DebugPrint(pAsset?"pAsset not NULL\n":"pAsset NULL");
if (pAsset)
{
char buf[1024];
AAsset_read(pAsset, buf, sizeof(buf));
DebugPrint(buf);
AAsset_close(pAsset);
}
此代码始终在logcat中打印“ pAsset NULL”。
我将asset_test.txt文件放在资产目录中,并通过将.apk重命名为.zip并使用7zip打开它来查看.apk,以确保该文件存在。
我还有更多代码:
AAssetDir* pAssetDir = AAssetManager_openDir(pAssetManager, sDirectory.c_str());
if (!pAssetDir)
{
DebugPrint("pAssetDir NULL\n");
return;
}
const char* pszDir;
while ((pszDir = AAssetDir_getNextFileName(pAssetDir)) != NULL)
{
DebugPrint(pszDir);
}
AAssetDir_close(pAssetDir);
此代码不打印任何内容。换句话说,无论我通过什么路径进入资产目录,都找不到文件。
注意:DebugPrint只是__android_log_print()的漂亮包装。
我将Activity传递给了AAssetManager_fromJava(),而我应该传递了AssetManager。如果将错误的类传递给AAssetManager_fromJava(),它将失败,而不会在logcat中打印任何内容。
如何使用JNI获取资产管理器:
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
jobject activity = (jobject)SDL_AndroidGetActivity();
jclass activity_class = env->GetObjectClass(activity);
jmethodID activity_class_getAssets = env->GetMethodID(activity_class, "getAssets", "()Landroid/content/res/AssetManager;");
jobject asset_manager = env->CallObjectMethod(activity, activity_class_getAssets); // activity.getAssets();
global_asset_manager = env->NewGlobalRef(asset_manager);
pAssetManager = AAssetManager_fromJava(env, global_asset_manager);
从现在开始,将该资产管理器指针存放在某个地方,并将其用于所有AAssetManager _ *()函数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句