File hierarchy with Android native AAssetManager

1.6k Views Asked by At

Issue

I wonder how to make file hierarchy of assets folder in Android from native code. I'm using AAssetManager_openDir but AAssetDir_getNextFileName doesn't return any directory names so basically I have no way to dive deeper into hierarchy because I can't obtain names of subfolders. Is there any way to resolve this issue with AAssetManager or should I implement this in Java?

Example

Assets looks like

a/
  file1
  file2
b/
  file3
  c/
    file4

C++ code

AAssetDir* assetDir = AAssetManager_openDir(env, "");
// filename is NULL since there is only folders at the root folder
const char* filename = AAssetDir_getNextFileName(assetDir);
AAssetDir_close(assetDir);
AAssetDir* assetDir = AAssetManager_openDir(env, "a");
// filename is "file1"
const char* filename = AAssetDir_getNextFileName(assetDir);
// filename is "file2"
filename = AAssetDir_getNextFileName(assetDir);
AAssetDir_close(assetDir);

EDIT:

I found out that AAssetDir_getNextFileName implementation literally filters directories from output. Link to source code

1

There are 1 best solutions below

1
On

You can achieve your goal in three steps, each requires some programming. The bottom line is, your assets all live in the APK file, and your app can find and read the APK file, and its format is a ZIP archive.

  1. Find your APK file name.
  2. Open this file for read as zip archive.
  3. Iterate the zip contents for assets/<whatever>.

For 1), you can use Java or if you must stay in c++, filter /proc/self/fd. You may find some system APK files (framework), but your APK will be the only one that is not system.

For 2), see use zlib to list directory structure of apk file in android. Note that zlib is part of public NDK libraries.