There are two options, it will depend on your goal. If your file is a basic text configuration file, you can use both cases, but if your file is a 3D object, for example (.obj, .max, .dae), you should use the AssetManager class.
First option: (save your files in res raw (you can use fopen ())).
- Create a folder named raw inside the res directory (res-> raw).
- Write your files in the private apk directory.
In Java:
public void writeFileToPrivateStorage(int fromFile, String toFile) { InputStream is = mContext.getResources().openRawResource(fromFile); int bytes_read; byte[] buffer = new byte[4096]; try { FileOutputStream fos = mContext.openFileOutput(toFile, Context.MODE_PRIVATE); while ((bytes_read = is.read(buffer)) != -1) fos.write(buffer, 0, bytes_read);
Then call your function:
writeFileToPrivateStorage(R.raw.your_file,"your_output_file.txt");
path = mContext.getApplicationContext (). getFilesDir (). toString ();
- Define the JNI function in Java:
public static native void setconfiguration (String yourpath);
JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_setconfiguration (JNIEnv * env, jobject obj, jstring path)
{
// convert your string into std :: string.
const char * nativeString = env-> GetStringUTFChars (config_path, 0);
// make here your fopen.
fopen (nativeString, "r");
}
Second option (use resourceManager, usually for opengl resources).
The parameter, in this case, is not a directory address, it is an asset manager.
- Save the files in the resource directory.
- Define your own function in C / C ++
public static native void yourfunction (AssetManager assetManager);
- A call in java for this function:
loadYourFile (m_context.getAssets ());
- Create your jni function in C / C ++
JNIEXPORT void Java_com_android_gl2jni_GL2JNILib_ (JNIEnv * env, jobject obj, jobject java_asset_manager)
{
AAssetManager * mgr = AAssetManager_fromJava (env, java_asset_manager);
AAsset * asset = AAssetManager_open (mgr, (const char *) js, AASSET_MODE_UNKNOWN);
if (NULL == asset) {
__android_log_print (ANDROID_LOG_ERROR, NF_LOG_TAG, "_ASSET_NOT_FOUND_");
return JNI_FALSE;
}
long size = AAsset_getLength (asset);
char * buffer = (char *) malloc (sizeof (char) * size);
AAsset_read (asset, buffer, size);
__android_log_print (ANDROID_LOG_ERROR, NF_LOG_TAG, buffer);
AAsset_close (asset);
}
Note. Remember to add permissions to your AndroidManifest.xml.
Note II: Remember to add:
#include <android/asset_manager.h> #include <android/asset_manager_jni.h>
Hope this answer helps you.