简体   繁体   English

在JNI中获取项目内文件的相对路径

[英]Get relative path to file within project in JNI

Using JNI I can't get access to files like this: 使用JNI,我无法访问以下文件:

ifstream file("file.txt");

file.txt is in folder jni , which is the same folder as the native code files file.txt位于文件夹jni ,该文件夹与本机代码文件位于同一文件夹中

I can only access absolute path ( "/sdcard/file.txt" ), but this trick is not what I'm looking for. 我只能访问绝对路径( "/sdcard/file.txt" ),但是此技巧不是我想要的。 Tried "data/data/com.example.SomeExample/jni/file.txt" but no luck. 尝试过"data/data/com.example.SomeExample/jni/file.txt"但没有运气。

How should I refer to a file as reletive path when working in native code? 使用本机代码工作时,应如何将文件称为重复路径?

The jni folder is not added to your APK. jni文件夹未添加到您的APK。 If you want files to be bundled with your application, I'd suggest putting them in the assets folder. 如果您希望文件与您的应用程序捆绑在一起,建议将它们放在assets文件夹中。 If the assets folder doesn't already exist, place it at the same level as the jni folder (so they are siblings). 如果assets文件夹尚不存在,请将其放置在与jni文件夹相同的级别(因此它们是兄弟姐妹)。

If you take this approach you will have to use the Asset Manager. 如果采用这种方法,则必须使用资产管理器。 This has a native interface, found in android/asset_manager.h. 它具有一个本机界面,位于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 . 您可以传递不同的标志,而不是AASSET_MODE_BUFFER

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

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