Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fopen in JNI C code get failed for file placed in assets folder in android application

I have kept some files in assets folder of android application.

Now i compile that application and get apk. [when i extract that apk my files are there in assets folder]

Now i install that apk in mobile device

and in That application has some code in c programing with JNI interface.

Now my fopen() call in c programming for that file get failed.

fopen("myfile","r");

I know while installing apk android copy assets file to /data/data/com.packagename/something

So does anyone knows that path so i can give it in fopen()

or is there any other method to open that file in C programming.


If i keep those files in sdcard folder and access like fopen("/sdcard/myfile","r"); then it works file. But i want to keep those files in assets folder only

like image 333
Jeegar Patel Avatar asked Mar 23 '16 12:03

Jeegar Patel


1 Answers

You can use the asset manager to access assets in JNI code. It's part of NDK.

http://developer.android.com/ndk/reference/group___asset.html

Something like:

EXPORT_API void LoadAsset(char* filename, jobject assetManager){
    AAssetManager* mgr = AAssetManager_fromJava(env, assetManager);
    AAsset* asset = AAssetManager_open(mgr, filename, AASSET_MODE_BUFFER);
    AAsset_read(asset, ...);
}

And then in Java something like:

LoadAsset("myfile", getContext().getAssetManager());
like image 86
kichik Avatar answered Nov 06 '22 05:11

kichik