File hierarchy with Android native AAssetManager

99封情书 提交于 2021-02-10 06:26:32

问题


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:


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.



来源:https://stackoverflow.com/questions/46225251/file-hierarchy-with-android-native-aassetmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!