直接在C ++中没有资产管理和复制访问的Andr​​oid APK资产数据 [英] Access Android APK Asset data directly in c++ without Asset Manager and copying

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

问题描述

我用纯C ++在我的引擎创造的Andr​​oid游戏引擎。没有单一的Java文件。基本上它是一个游戏,只应该存储到外部存储器。当我通过ADB手动移动我的资产数据,以我的外置SD卡中的游戏作品已经优良且稳定。

 亚行推.. \ .. \ BIN \数据/sdcard/Android/data/com.fantasyhaze.%SMALL_PACKAGE_NAME%/files/Data/
 

这是不是一个很好的解决方案,因为它无法传递。因此,我有资产的文件夹我的资产数据 它得到建设过程具有以下结构的过程中进入了apk文件:

资产/数据/ MoreFolders / Withsubfolders 资产/数据/ EngineData.zip 资产/数据/ ScriptData.zip

但我不知道这些文件是在文件系统访问它们在C + + code。

于是,我试图让路径文件目录。而由于在本地活动状态的bug 我需要检索正常code中的信息。

  //使用JNI code,而不是错误,在2.3 internalDataPath / externalDataPath = NULL
// FHZ_PRINTF(内部跨PATH =%s的\ N,语句>活动 - > internalDataPath);
// FHZ_PRINTF(外部跨PATH =%s的\ N,语句>活动 - > externalDataPath);
 

C ++ $ C $下其相当于android.os.Environment.getFilesDir() 和android.os.Environment.getExternalStorageState()等

  // getPath() -  Java的
        JNIEnv的* jni_env =核心:: HAZEOS :: GetJNIEnv();
        JCLASS cls_Env = jni_env->的findClass(机器人/ APP / NativeActivity的);
        jmethodID mid_getExtStorage = jni_env->的GetMethodID(cls_Env,getFilesDir,()Ljava / IO /文件;);
        jobject obj_File = jni_env-> CallObjectMethod(gstate->活动 - > clazz中,mid_getExtStorage);
        JCLASS cls_File = jni_env->的findClass(爪哇/ IO /文件);
        jmethodID mid_getPath = jni_env->的GetMethodID(cls_File,getPath,()Ljava /朗/字符串;);
        的jstring obj_Path =(的jstring)jni_env-> CallObjectMethod(obj_File,mid_getPath);
        为const char * PATH = jni_env-> GetStringUTFChars(obj_Path,NULL);
        FHZ_PRINTF(内部路径=%s的\ N,路径);
        jni_env-> ReleaseStringUTFChars(obj_Path,路径);

        // getCacheDir() -  Java的
        mid_getExtStorage = jni_env->的GetMethodID(cls_Env,getCacheDir,()Ljava / IO /文件;);
        obj_File = jni_env-> CallObjectMethod(gstate->活动 - > clazz中,mid_getExtStorage,NULL);
        cls_File = jni_env->的findClass(爪哇/ IO /文件);
        mid_getPath = jni_env->的GetMethodID(cls_File,getAbsolutePath,()Ljava /朗/字符串;);
        obj_Path =(的jstring)jni_env-> CallObjectMethod(obj_File,mid_getPath);
        PATH = jni_env-> GetStringUTFChars(obj_Path,NULL);
        FHZ_PRINTF(CACHE DIR =%s的\ N,路径);
        jni_env-> ReleaseStringUTFChars(obj_Path,路径);

        // getExternalFilesDir() -  Java的
        mid_getExtStorage = jni_env->的GetMethodID(cls_Env,getExternalFilesDir,(Ljava /朗/字符串;)Ljava / IO /文件;);
        obj_File = jni_env-> CallObjectMethod(gstate->活动 - > clazz中,mid_getExtStorage,NULL);
        cls_File = jni_env->的findClass(爪哇/ IO /文件);
        mid_getPath = jni_env->的GetMethodID(cls_File,getPath,()Ljava /朗/字符串;);
        obj_Path =(的jstring)jni_env-> CallObjectMethod(obj_File,mid_getPath);
        PATH = jni_env-> GetStringUTFChars(obj_Path,NULL);
        FHZ_PRINTF(外部路径=%s的\ N,路径);
        jni_env-> ReleaseStringUTFChars(obj_Path,路径);

        // getPackage codePATH() -  Java的
        mid_getPath = jni_env->的GetMethodID(cls_Env,getPackage codePATH,()Ljava /朗/字符串;);
        obj_File = jni_env-> CallObjectMethod(gstate->活动 - > clazz中,mid_getPath);
        obj_Path =(的jstring)jni_env-> CallObjectMethod(obj_File,mid_getPath);
        PATH = jni_env-> GetStringUTFChars(obj_Path,NULL);
        FHZ_PRINTF(抬头一看包code路径=%s的\ N,路径);
 

这工作得很好,并导致

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

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

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

抬眼包code路径= /mnt/asec/com.fantasyhaze.rememory-2/pkg.apk

但是,并没有从资产的文件的文件夹...

和我需要访问一个文件夹作为正常的工作目录中读取文件。这将是可能的

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

但是,从资产的文件夹,通​​过资产管理公司中的所有数据(无论是)移动到这个文件夹,原因一倍的内存消耗。

资产> 1GB意味着资产> 2GB这没有任何意义。更重要的是断言文件夹似乎不recusively和仅适用于小数据文件 使用更大朴文件时,这是不可能的。也许文件可以使用时,可以直接从APK访问 解压缩系统,然后uzip我的ressource文件,但因此我无论如何也要optain的apk文件的路径。

所以我的问题:

  1. 在哪里是在文件系统上的APK Assets文件夹?
  2. 在什么将是code(C ++)来检索APK位置或可执行文件
  3. 的位置
  4. 我可以直接使用普通文件打开方式,或只有当我解压访问它。如果我可以使用它没有拆包,怎么样?
  5. 在什么将是code(C ++)来检索信息,如果安装在SD卡?

我希望有人能帮助我:)

编辑:添加缓存目录和包目录code(和它的输出路径),提供源其他人谁需要它

解决方案

我张贴的答案,而不是标志着这是一个重复的,因为从技术上说,你要求比问题/答案我提供了更多详细信息要去这里。因此,要回答的第一个你4点,见<一href="http://stackoverflow.com/questions/7701801/obtaining-the-name-of-an-android-apk-using-c-and-the-nativeactivity-class">Obtaining一个Android APK中使用C ++和NativeActivity的类的名字。

至于检查是否安装在SD卡,这应该是相当简单的。只要尝试打开该目录或在SD卡的文件(你知道应该在那里),如果失败了,你知道的SD卡不可用。请参阅<一href="http://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c-cross-platform">What's最好的方法来检查文件是否存在C语言? (跨平台)。

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/

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:

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

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);

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);

which works quite well and results in

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

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

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

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

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

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.

So My questions:

  1. Where is the Assets folder in the apk on the file system?
  2. What would be the code (c++) to retrieve the apk location or the location of the executable
  3. Can I access it directly with a normal file open method or only if I unpack it. If I can use it without unpacking, how?
  4. What would be the code (c++) to retrieve the information if the sd card is mounted?

I hope someone can help me :)

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

解决方案

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.

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 ++中没有资产管理和复制访问的Andr​​oid APK资产数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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