如何使用NDK中的Asset Manager API读取原始数据?

How to use Asset Manager API in NDK to read raw data?

我将着色器保存在资产文件夹中。 着色器的名称(文件名):"vertex.vs" 路径:assets/shaders/vertex.vs

我想从 NDK 的 C++ 文件访问此文件,而不调用 Java 或 JNI,无论它是什么。 通过阅读各种资源,我设法理解我必须使用 header

#include <android/asset_manager.h>

之后我创建指针并打开它。

const char* mPath = "shaders/vertex.vs";
AAssetManager* mAssetManager;
AAsset* mAsset;
mAsset = AAssetManager_open(mAssetManager, mPath,AASSET_MODE_UNKNOWN);
int foo = AAsset_getLength(mAsset);
LOGD( "This is a number: %d", foo );
AAsset_close(mAsset);

但它什么也没做。 这个读取函数有什么用。

AAsset_read(mAsset,pBuffer,bytesToRead);

从哪里读取数据?如何定义 pBuffer ? 有人可以分享一个关于如何从原始文件读取数据以及如何访问它的简单示例吗(如在 logcat 中显示)?

您必须首先初始化 mAssetManager,我们通常通过 JNI 调用从 Java 获取它,参见例如this answer. You can obtain this Java object in your C++ code like this,但这还是需要JNIEnv.

如果您真的很想在没有 JNI 交互的情况下从您的 APK 中提取资产,这并非不可能。诀窍是 find your APK file 并相信它是一个 ZIP 文件。

Can someone share a simple example on how to read the data from a raw file and how to access it(Like showing it in logcat)?

Here 示例项目,从 res/raw 解压资源文件并将其放入可从本机代码访问的 FilesDir 位置(例如 /data/user/0/com.example.myapp/files) .

还有 there 描述此方法算法的 activity 图。