如何设置Android的JNI文件的路径? [英] How to set file path in Android JNI?

查看:429
本文介绍了如何设置Android的JNI文件的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在建设有C ++库的Andr​​oid应用程序,并且我有送文件路径的问题。

我在我的JNI函数 JNI / processing.cpp

  JNIEXPORT jfloat JNICALL Java_com_my_package_path_getMyFunction
(JNIEnv的* ENV,jobject OBJ,参数的jstring){
    字符串str = env-> GetStringUTFChars(参数,0);
    字符串文件路径=SRC /文本文件/;
    返回的getResult(STR,文件路径);
}

的getResult() JNI / src目录/库/ myclass.cpp 的成员函数。

我在 JNI / src目录/ TEXTFILES / 几个文本文件,我想这个路径发送到的getResult()函数作为参数。由于 processing.cpp JNI / 文件夹,我设置路径的src /文本文件/ ,但它不工作。 (的getResult()功能无法找到路径)

我已经试过文件路径=../../文本文件/这是从 JNI / src目录/库的相对路径/ myclass.cpp ,也不能正常工作。

不过,我所有的JNI函数(不使用文件路径)工作正常。

是否有任何人谁可以帮助?我应该使用绝对路径?然后,我怎样才能得到它呢?


解决方案

我自己找到了解决办法。

根据上述克里斯的评论,我把我的文本文件到资产文件夹中。

我试图从JNI访问资产文件夹(如<一个href=\"http://stackoverflow.com/questions/13317387/how-to-get-file-in-assets-from-android-ndk\">this),但有些没有奏效。

于是我再次读入Java文件,并存储文件到内部存储。既然我们知道了数据/数据​​/ your_app_package_name / 是内部存储路径,我可以从JNI读取文件。<​​/ P>

这里的code我如何读资产文件并存储到内置存储:

  AssetManager assetManager = getResources()getAssets()。
    的String []文件= NULL;    尝试{
        文件= assetManager.list(文件);
    }赶上(例外五){
        Log.d(MYLOG,ERROR:+ e.toString());
    }    的for(int i = 0; I&LT; files.length;我++){
        在的InputStream = NULL;
        出的OutputStream = NULL;
        FileOutputStream中fileOutStream = NULL;
        尝试{
            Log.d(MYLOG,文件名:+文件[I]);            在= assetManager.open(文件/+文件[I]);
            OUT =新的FileOutputStream(getApplicationContext()getFilesDir()+文件[I]);            档案文件=新的文件(getApplicationContext()getFilesDir(),文件的[I]);            字节[]缓冲区=新的字节[65536 * 2];
            INT读;
            而((读= in.read(缓冲))!= -1){
                out.write(缓冲,0,读);
            }
            附寄();
            在= NULL;            了out.flush();
            fileOutStream =新的FileOutputStream(文件);
            fileOutStream.write(缓冲液);
            out.close();
            出= NULL;
            Log.d(MYLOG,文件复制存储);
        }赶上(例外五){
            Log.d(MYLOG,ERROR:+ e.toString());
        }
    }

我不知道这是正确的解决方案,但它的工作好! :)

I'm building an Android app with C++ library, and I have a problem with sending file path.

I have my JNI function in jni/processing.cpp :

JNIEXPORT jfloat JNICALL Java_com_my_package_path_getMyFunction
(JNIEnv *env, jobject obj, jstring param) {
    string str = env->GetStringUTFChars(param, 0);
    string filePath = "src/textfile/";
    return getResult(str, filePath);
}

and getResult() is a member function of jni/src/library/myclass.cpp.

I have several text files under jni/src/textfiles/, and I want to send this path to getResult() function as a parameter. Since processing.cpp is in jni/ folder, I set path as src/textfile/, but it doesn't work. (getResult() function can't find the path)

I've been tried filePath = "../../textfile/" which is relative path from jni/src/library/myclass.cpp, and it also doesn't work.

However, all my other JNI functions (not using file path) are working fine.

Is there anyone who can help? Should I use absolute path? Then, how can I get it?

解决方案

I found the solution by myself.

According to Chris' comment above, I put my text files into the assets folder.

I tried to access assets folder from JNI (like this), but somewhat it didn't work.

So I read files in Java, and store files again into internal storage. Since we know the data/data/your_app_package_name/ is the internal storage path, I can read files from JNI.

Here's the code how I read files from assets and store to internal storage :

AssetManager assetManager = getResources().getAssets();
    String[] files = null;

    try {
        files = assetManager.list("Files");
    } catch (Exception e) {
        Log.d(MYLOG, "ERROR : " + e.toString());
    }

    for (int i = 0; i < files.length; i++) {
        InputStream in = null;
        OutputStream out = null;
        FileOutputStream fileOutStream = null;
        try {
            Log.d(MYLOG, "file names : " + files[i]);

            in = assetManager.open("Files/" + files[i]);
            out = new FileOutputStream(getApplicationContext().getFilesDir() + files[i]);

            File file = new File(getApplicationContext().getFilesDir(), files[i]);

            byte[] buffer = new byte[65536 * 2];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            in.close();
            in = null;

            out.flush();
            fileOutStream = new FileOutputStream(file);
            fileOutStream.write(buffer);
            out.close();
            out = null;
            Log.d(MYLOG, "File Copied in storage");
        } catch (Exception e) {
            Log.d(MYLOG, "ERROR: " + e.toString());
        }
    }

I'm not sure this is the right solution, but it worked well! :)

这篇关于如何设置Android的JNI文件的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆