Jor*_*uez 4 android android-ndk
我有一些代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
此代码始终在 logcat 中打印“pAsset NULL”。
我将 asset_test.txt 文件放入我的 asset 目录中,然后通过将 .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);
Run Code Online (Sandbox Code Playgroud)
该代码不打印任何内容。换句话说,无论我传入什么路径,在资产目录中都找不到文件。
注意: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);
Run Code Online (Sandbox Code Playgroud)
将资产管理器指针隐藏在某处,并从现在开始将其用于所有 AAssetManager_*() 函数。
| 归档时间: |
|
| 查看次数: |
8751 次 |
| 最近记录: |