当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《如何将文件添加到Android项目,然后使用NDK加载它》经验,为你挑选了1个好方法。

我使用的是最新版本的Android Studio(2.2.3),我已经加载了HelloGL2示例项目.

我现在想要在我的应用程序中添加一个文件(任何类型的文件),然后能够打开它并使用c的fopen等在c ++代码中读取它(任何直接文件访问api都可以)

我该怎么做呢?



1> uelordi..:

有两种选择,它取决于您的目标.如果您的文件是基本文本配置文件,则可以使用这两种情况,但如果您的文件是(.obj,.max,.dae)等3D对象,则应使用AssetManager类.

第一个选项:(以res raw格式存储文件(可以使用fopen())).

在res目录中创建一个名为raw的文件夹(res-> raw).

将您的文件写入apk私人目录.

在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();
        }                 
    }  

然后,调用您的函数:

        writeFileToPrivateStorage(R.raw.your_file,"your_output_file.txt");

获取您的私人路径

    path=mContext.getApplicationContext().getFilesDir().toString();

用Java定义你的JNI函数:

    public static native void setconfiguration(String yourpath);

用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");
        }

第二个选项(使用assetManager,通常用于opengl资源).

在这种情况下,参数不是目录的路径,即资产管理器.

将文件存储在资产目录中.

在C/C++中定义您的本机函数

     public static native void yourfunction(AssetManager assetManager);

在java中调用此函数:

     loadYourFile(m_context.getAssets());

用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);
        }

注意:不要忘记在AndroidManifest.xml中添加权限.

注意二:不要忘记添加:

#include 
#include 

我希望这个答案可以帮到你.

推荐阅读
牛尾巴2010
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有