Question

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.

Was it helpful?

Solution

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

OTHER TIPS

You may want to have a look at "Expansion Files" see APK Expansion Files.

As far is i understand this, this gives you a system that automatically puts resource files (your expansion file) on the "shared storage location".

So i think that this would solve your problem of duplicated data.

There is also a short introduction post on the android-developers blog.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top