0

Using JNI I can't get access to files like this:

ifstream file("file.txt");

file.txt is in folder jni, which is the same folder as the native code files

I can only access absolute path ("/sdcard/file.txt"), but this trick is not what I'm looking for. Tried "data/data/com.example.SomeExample/jni/file.txt" but no luck.

How should I refer to a file as reletive path when working in native code?

Max
  • 3,824
  • 8
  • 41
  • 62
  • `Context.getFilesDir()` will give you the app-specific file directory. (Your Activity is a Context.) You can query that in Java and pass it into your native code, or pass your Activity to JNI and query it directly. Concatenate that path with whatever file name you want. What you don't want to do is hard-code the path, since it's not guaranteed by the app framework to have a specific value. – fadden Jul 12 '13 at 23:04
  • @fadden: Can you give me an example of querying getFilesDir() from JNI directly, and post it as an answer so I can accept it? – Max Jul 13 '13 at 22:25

1 Answers1

0

The jni folder is not added to your APK. If you want files to be bundled with your application, I'd suggest putting them in the assets folder. If the assets folder doesn't already exist, place it at the same level as the jni folder (so they are siblings).

If you take this approach you will have to use the Asset Manager. This has a native interface, found in android/asset_manager.h. To open your file use:

AAsset *a = AAssetManager_open(pAssetManager, "file.txt", AASSET_MODE_BUFFER);
if (a)
{
    const void *res = AAsset_getBuffer(a);
    //do something with the file!
    AAsset_close(a);
}

There are different flags you can pass in instead of AASSET_MODE_BUFFER.

krsteeve
  • 1,794
  • 4
  • 19
  • 29