Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have shaders kept in assets folder. name of the shader (file name) : "vertex.vs" path : assets/shaders/vertex.vs

I want to access this file from a C++ file from NDK without calling Java or JNI whatever it is. From reading various resources I managed to understand that i have to use the header

#include <android/asset_manager.h>

After that I create pointers and open it.

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

But it doesn't do anything. And what's with this read function.

AAsset_read(mAsset,pBuffer,bytesToRead);

Where is the data read? How to define the pBuffer ? 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)?

like image 947
Rahul Naik Avatar asked Mar 16 '18 14:03

Rahul Naik


1 Answers

You must initialize mAssetManager to begin with, we usually get it from Java via a JNI call, see e.g. this answer. You can obtain this Java object in your C++ code like this, but this still needs JNIEnv.

If you really really want to extract an asset from your APK with no JNI interaction, it not impossible. The trick is to find your APK file and trust that it is a ZIP file under the hood.

like image 105
Alex Cohn Avatar answered Sep 23 '22 12:09

Alex Cohn