直接在 C++ 中访问 Android APK Asset 数据,无需 Asset Manager 和复制 [英] Access Android APK Asset data directly in c++ without Asset Manager and copying

查看:37
本文介绍了直接在 C++ 中访问 Android APK Asset 数据,无需 Asset Manager 和复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的引擎中使用纯 C++ 在 android 中创建一个游戏引擎.没有单个 java 文件.基本上它是一个应该只存储到外部存储器的游戏.当我通过 adb 手动将我的资产数据移动到我的外部 SD 卡时,游戏运行良好且稳定.

I'm using pure C++ in my engine to create a game engine in android. There is no single java file. Basically it is a game which should only be stored to external memory. When I move my asset data manually via adb to my external sd card the game works already fine and stable.

adb push ..\..\Bin\Data /sdcard/Android/data/com.fantasyhaze.%SMALL_PACKAGE_NAME%/files/Data/

这不是一个好的解决方案,因为它无法交付.因此,我在 Assets 文件夹中有我的资产数据它在构建过程中被移动到 apk 文件中,结构如下:

This is not a good solution because it can not be delivered. Therefore I have my assets-data in Assets folder which gets moved into the apk file during building process with the following structure:

资产/数据/MoreFolders/Withsubfolders资产/数据/引擎数据.zip资产/数据/ScriptData.zip

Assets/Data/MoreFolders/Withsubfolders Assets/Data/EngineData.zip Assets/Data/ScriptData.zip

但我不知道这些文件在文件系统上的哪个位置以使用 C++ 代码访问它们.

But I don't know where those files are on the file systems to access them in c++ code.

所以我尝试获取文件目录的路径.并且由于本机活动状态中的错误我必须用普通代码检索信息.

So I tried to get the path to the file directories. And because of a bug in the native activity state I have to retrieve the information in normal code.

// bug in 2.3 internalDataPath / externalDataPath = null using jni code instead
//FHZ_PRINTF("INTERNAL inter PATH = %s\n", state->activity->internalDataPath);  
//FHZ_PRINTF("EXTERNAL inter PATH = %s\n", state->activity->externalDataPath);

相当于 android.os.Environment.getFilesDir() 的 c++ 代码和 android.os.Environment.getExternalStorageState() 等

c++ code for its equivalent to android.os.Environment.getFilesDir() and android.os.Environment.getExternalStorageState() ect

            // getPath() - java
        JNIEnv *jni_env = Core::HAZEOS::GetJNIEnv();
        jclass cls_Env = jni_env->FindClass("android/app/NativeActivity");
        jmethodID mid_getExtStorage = jni_env->GetMethodID(cls_Env, "getFilesDir","()Ljava/io/File;");
        jobject obj_File = jni_env->CallObjectMethod( gstate->activity->clazz, mid_getExtStorage);
        jclass cls_File = jni_env->FindClass("java/io/File");
        jmethodID mid_getPath = jni_env->GetMethodID(cls_File, "getPath","()Ljava/lang/String;");
        jstring obj_Path = (jstring) jni_env->CallObjectMethod(obj_File, mid_getPath);
        const char* path = jni_env->GetStringUTFChars(obj_Path, NULL);
        FHZ_PRINTF("INTERNAL PATH = %s\n", path);
        jni_env->ReleaseStringUTFChars(obj_Path, path);

        // getCacheDir() - java
        mid_getExtStorage = jni_env->GetMethodID( cls_Env,"getCacheDir", "()Ljava/io/File;");
        obj_File = jni_env->CallObjectMethod(gstate->activity->clazz, mid_getExtStorage, NULL);
        cls_File = jni_env->FindClass("java/io/File");
        mid_getPath = jni_env->GetMethodID(cls_File, "getAbsolutePath", "()Ljava/lang/String;");
        obj_Path = (jstring) jni_env->CallObjectMethod(obj_File, mid_getPath);
        path = jni_env->GetStringUTFChars(obj_Path, NULL);
        FHZ_PRINTF("CACHE DIR = %s\n", path); 
        jni_env->ReleaseStringUTFChars(obj_Path, path);

        // getExternalFilesDir() - java
        mid_getExtStorage = jni_env->GetMethodID( cls_Env,"getExternalFilesDir", "(Ljava/lang/String;)Ljava/io/File;");
        obj_File = jni_env->CallObjectMethod(gstate->activity->clazz, mid_getExtStorage, NULL);
        cls_File = jni_env->FindClass("java/io/File");
        mid_getPath = jni_env->GetMethodID(cls_File, "getPath", "()Ljava/lang/String;");
        obj_Path = (jstring) jni_env->CallObjectMethod(obj_File, mid_getPath);
        path = jni_env->GetStringUTFChars(obj_Path, NULL);
        FHZ_PRINTF("EXTERNAL PATH = %s\n", path);
        jni_env->ReleaseStringUTFChars(obj_Path, path);

        //getPackageCodePath() - java
        mid_getPath = jni_env->GetMethodID(cls_Env, "getPackageCodePath", "()Ljava/lang/String;"); 
        obj_File = jni_env->CallObjectMethod(gstate->activity->clazz, mid_getPath); 
        obj_Path = (jstring) jni_env->CallObjectMethod(obj_File, mid_getPath);
        path = jni_env->GetStringUTFChars(obj_Path, NULL);
        FHZ_PRINTF("Looked up package code path = %s\n", path);

效果很好,结果是

内部路径 =/data/data/com.fantasyhaze.rememory/files

INTERNAL PATH = /data/data/com.fantasyhaze.rememory/files

缓存目录 =/data/data/com.fantasyhaze.rememory/cache

CACHE DIR = /data/data/com.fantasyhaze.rememory/cache

外部路径 =/mnt/sdcard/Android/data/com.fantasyhaze.rememory/files

EXTERNAL PATH = /mnt/sdcard/Android/data/com.fantasyhaze.rememory/files

查找包代码路径 =/mnt/asec/com.fantasyhaze.rememory-2/pkg.apk

Looked up package code path = /mnt/asec/com.fantasyhaze.rememory-2/pkg.apk

但是资产文件夹中没有文件...

But there are no files from the assets folders...

我需要访问一个文件夹作为正常工作目录来读取文件.这在

and I need to access a folder as normal working directory to read the files. which would be possible in

/mnt/sdcard/Android/data/com.fantasyhaze.rememory/files/Data

/mnt/sdcard/Android/data/com.fantasyhaze.rememory/files/Data

但是通过资产管理器将资产文件夹(无论它在哪里)中的所有数据移动到此文件夹,会导致内存消耗翻倍.

But moving all data from the asset folder (wherever it is) via asset manager to this folder, causes doubled consumption of memory.

资产 >1GB 意味着资产 >2GB,这是没有意义的.不仅如此,断言文件夹似乎不能递归地工作,并且仅适用于小数据文件这在使用更大的 pak 文件时是不可能的.也许文件可以在使用时直接从apk访问解压缩系统,然后 uzip 我的资源文件,但因此无论如何我必须选择 apk 路径.

assets >1GB would mean assets >2GB which doesn't make sense. More than that the assert folder seems not to work recusively and only for small data files which is not possible when using bigger pak files. Maybe the files can be access directly from the apk when using the unzip system and then uzip my ressource files, but therefore I have to optain the apk path anyway.

所以我的问题:

  1. 文件系统上 apk 中的 Assets 文件夹在哪里?
  2. 检索 apk 位置或可执行文件位置的代码 (c++) 是什么
  3. 我可以使用普通的文件打开方法直接访问它还是只有在我解压它时才能访问它.如果我不用开箱就可以使用它,如何使用?
  4. 如果安装了 SD 卡,检索信息的代码 (c++) 是什么?

我希望有人能帮助我:)

I hope someone can help me :)

添加缓存目录和包目录代码(及其输出路径),为需要它的其他人提供源.

Added cache directory and the package directory code (and it's output paths) , to provide the source for everyone else who needs it.

推荐答案

我发布了一个答案而不是将其标记为重复,因为从技术上讲,您要求提供的详细信息比我在问题/答案中提供的要多我要在这里链接.因此,要回答您的 4 点中的第一个,请参阅 使用 C++ 和 NativeActivity 类获取 Android APK 的名称.

I'm posting an answer instead of marking this as a duplicate because technically, you're asking for more details than are provided in the question/answer I'm going to link here. So, to answer the first of your 4 points, see Obtaining the name of an Android APK using C++ and the NativeActivity class.

至于查看是否安装了 SD 卡,应该很简单.只需尝试打开 SD 卡上的目录或文件(您知道应该在那里),如果失败,您就知道 SD 卡不可用.请参阅什么是最好的检查文件是否存在于C中的方法?(跨平台).

As for checking to see if the SD card is mounted, that should be rather simply. Simply attempt to open the directory or a file on the SDcard (that you know should be there) and if it fails, you know the SD card is not available. See What's the best way to check if a file exists in C? (cross platform).

这篇关于直接在 C++ 中访问 Android APK Asset 数据,无需 Asset Manager 和复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆