获取相对路径JNI项目内的文件 [英] Get relative path to file within project in JNI

查看:1973
本文介绍了获取相对路径JNI项目内的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JNI我不能访问文件是这样的:

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

ifstream file("file.txt");

file.txt的是文件夹 JNI ,这是相同的文件夹本土code文件

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

我只能访问绝对路径(/ SD卡/ file.txt的),但这一招不是我要找的。
试图数据/数据​​/ com.example.SomeExample / JNI / file.txt的,但没有运气。

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.

应如何本地code工作时,我指的是一个文件reletive路径?

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

推荐答案

JNI 文件夹不添加到您的APK。如果你想要的文件与应用程序捆绑在一起,我建议把它们在资产文件夹中。如果文件夹不存在的的资产,将其放置在同一水平 JNI 文件夹中(所以它们兄弟姐妹)。

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).

如果你采取这种方法,你将不得不使用的资产管理公司。这有一个原生界面,在android系统/ asset_manager.h找到。要打开文件,请使用:

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

有你能传递 AASSET_MODE_BUFFER

这篇关于获取相对路径JNI项目内的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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