How to add a file to an Android project and then upload it using NDK

I am using the latest version of Android Studio (2.2.3) and I downloaded the sample HelloGL2 project.

Now I want to add a file (any type of file) to my application, and then open it and read it in C ++ code using something like c fopen, etc. (any direct access to api files is ok)

How to do it?

+5
source share
1 answer

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); // write fos.close(); is.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } 

Then call your function:

  writeFileToPrivateStorage(R.raw.your_file,"your_output_file.txt"); 
  • Get a personal way
  path = mContext.getApplicationContext (). getFilesDir (). toString ();
  • Define the JNI function in Java:
  public static native void setconfiguration (String yourpath);
  • Embed it in C / C ++:
  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.

+7
source

Source: https://habr.com/ru/post/1262872/


All Articles