简体   繁体   English

如何将文件添加到Android项目,然后使用NDK加载它

[英]How do I add a file to an Android project and then load it using the NDK

I am using the latest version of Android Studio (2.2.3) and I have loaded up the HelloGL2 sample project. 我使用的是最新版本的Android Studio(2.2.3),我已经加载了HelloGL2示例项目。

I now want to add a file (any type of file) to my app, and then be able to open it and read it in the c++ code using something like c's fopen etc (any direct file access api is fine) 我现在想要在我的应用程序中添加一个文件(任何类型的文件),然后能够打开它并使用c的fopen等在c ++代码中读取它(任何直接文件访问api都可以)

How do I do this? 我该怎么做呢?

There are two options, it will depend on your target. 有两种选择,它取决于您的目标。 If your file is a basic text configuration file, you can use both cases, but if your file is a 3D object such as (.obj, .max, .dae) you should use AssetManager class. 如果您的文件是基本文本配置文件,则可以使用这两种情况,但如果您的文件是(.obj,.max,.dae)等3D对象,则应使用AssetManager类。

First option: (store your files in res raw (You can use fopen())). 第一个选项:(以res raw存储您的文件(您可以使用fopen()))。

  • Create a folder called raw inside res directory (res->raw). 在res目录中创建一个名为raw的文件夹(res-> raw)。
  • Write your files in the apk private directory. 将您的文件写入apk私人目录。

In Java: 在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 to your function: 然后,调用您的函数:

        writeFileToPrivateStorage(R.raw.your_file,"your_output_file.txt");
  • Get your private path 获取您的私人路径
path=mContext.getApplicationContext().getFilesDir().toString();
  • Define your JNI funcion in Java: 用Java定义你的JNI函数:
public static native void setconfiguration(String yourpath);
  • Implement it in C/C++: 用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 assetManager, usually for opengl resources). 第二个选项(使用assetManager,通常用于opengl资源)。

The parameter, in this case, is not the path of the directory is the asset manager. 在这种情况下,参数不是目录的路径,即资产管理器。

  • Store your files in the asset directory. 将文件存储在资产目录中。
  • Define your native function in C/C++ 在C / C ++中定义您的本机函数
public static native void yourfunction(AssetManager assetManager);
  • Call in java to this function: 在java中调用此函数:
loadYourFile(m_context.getAssets());
  • Create your jni function in C/C++ 用C / C ++创建你的jni函数
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: Do not forget to add the permissions in your AndroidManifest.xml. 注意:不要忘记在AndroidManifest.xml中添加权限。

Note II: Do not forget to add: 注意二:不要忘记添加:

#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>

I hope this answer helps you. 我希望这个答案可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM