user1279988
user1279988

Reputation: 773

android ndk wrapped c++ file get android system file path

I have some code written in c++ by android JNI.

in one export function

"extern "C" JNIEXPORT int JNICALL Java_com_android_mymapsforge_create(JNIEnv *env, jobject obj)
{
        ...

    ifstream infile("C:\\Users\\li\\Documents\\Visual Studio 2010\\Projects\\importANN\\Debug\\nodes.csv");
    string line;
    int index = 0;

    while( getline( infile, line ))
    {
            ....
        }

}

when i using those original c++ code, i test under vs2010. Either i assign the infile() parameters with a fixed file path, or using some c++ get_file_path function, it's a thing related windows system, there is no problem. both work.

But now I want to using these code under JNI using by android. So the file path system change to android and some function need be adjusted.

My problem arise: because the code is using at last by my android java code under a android app, so the "C:\Users\li\Documents\Visual Studio 2010\Projects\importANN\Debug\nodes.csv", no longer take effect, it may be change to "/mnt/sdcard/somefolder/nodes.csv"

but i have experience when i in android app give a fixed file path it tell my some error, so under android jave envi, i using below code to get file path, similar code here:

String mpath ;
mpath = Environment.getExternalStorageDirectory().getPath();
mapView.setMapFile(new File(mpath+"/"+"berlin.map"));

(omit the filename , i just want to tell the method i use)

But these android code must can not use in my c++ wrapping function Java_com_android_mymapsforge_create,

So my question is :

how to cope with the file(android system) path things under the c++ export function? For example, i put the "nodes.csv" under my sdcard root folder, and want to do some processing under the JNI exported c++ function?

Upvotes: 1

Views: 2776

Answers (1)

Aleksei Potapkin
Aleksei Potapkin

Reputation: 1096

The easiest way is to pass the path as an argument of the function.

extern "C" JNIEXPORT int 
JNICALL Java_com_android_mymapsforge_create(JNIEnv *env, jobject, jstring path)

Then you can turn jstring path to char* using JNI converting jstring to char *.

The second way is to call Android Environment.getExternalStorageDirectory().getPath() from JNI function like described here - Call a static java method of another package from native code.

Upvotes: 1

Related Questions