Fopen in JNI C code cannot get file placed in resource folder in android application

I saved some files in the resource folder of the Android application.

Now I compile this application and get apk. [when I extract that apk my files are in the resource folder]

Now I install apk on my mobile device

and that the application has some code in programming with the JNI interface.

Now my fopen() call in c-programming for this file fails.

 fopen("myfile","r"); 

I know that when installing the android copy apk archives file to /data/data/com.packagename/something

So does anyone know this way, so I can give it in fopen()

or is there any other way to open this file in C programming.


If I store these files in the sdcard folder and gain access as fopen("/sdcard/myfile","r"); then it works with the file. But I want to save these files only in the resource folder

+8
java c android android-assets
source share
2 answers

You can use the asset manager to access assets in JNI code. This is part of the 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()); 
+6
source share

One way I found is to write a test line in code, for example:

 System.out.println("Working Directory = " + System.getProperty("user.dir")); 

Now, if you know where your assets are stored on Android, /data/data/com.packagename/assets/file.png , for example, then you will find out which relational or absolute path to assets. Hope this works for you, it helps me!

0
source share

All Articles