Android原生AAssetManager的文件层次结构 [英] File hierarchy with Android native AAssetManager

查看:176
本文介绍了Android原生AAssetManager的文件层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何通过本机代码在Android中建立Assets文件夹的文件层次结构.我正在使用 AAssetManager_openDir ,但是 AAssetDir_getNextFileName 不会返回任何目录名称,因此基本上我无法深入层次结构,因为我无法获取子文件夹的名称.有什么方法可以使用 AAssetManager 解决此问题,还是应该在Java中实现?

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?

资产看起来像

a/
  file1
  file2
b/
  file3
  c/
    file4

C ++代码

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


我发现 AAssetDir_getNextFileName 实现确实从输出中过滤了目录.链接到源代码

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

推荐答案

您可以通过三个步骤来实现您的目标,每个步骤都需要进行一些编程.最重要的是,您的资产全部存在于APK文件中,并且您的应用程序可以找到和读取APK文件,其格式为ZIP存档.

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. 找到您的APK文件名.
  2. 打开此文件以zip存档的形式读取.
  3. 迭代 assets/< whatever> 的zip内容.
  1. Find your APK file name.
  2. Open this file for read as zip archive.
  3. Iterate the zip contents for assets/<whatever>.

对于1),您可以使用Java ,或者,如果必须使用c ++,则可以过滤/proc/self/fd .您可能会发现一些系统APK文件(框架),但您的APK将是唯一一个非系统文件.

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.

对于2),请参见 使用zlib列出android中apk文件的目录结构 .请注意, zlib 是公共NDK库的一部分.

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

这篇关于Android原生AAssetManager的文件层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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