Android的资产,C,JNI [英] Android assets, C, JNI
问题描述
如何访问一个Android的资产,如.txt文件,从C用JNI?
How do you access an Android asset, such as a .txt file, from C with the JNI?
我想文件:///android_asset/myFile.txt,并在本地MYFILE.TXT与MYFILE.TXT在与C实现文件的JNI文件夹重复
I'm trying "file:///android_asset/myFile.txt", and locally "myFile.txt" with a duplicate of myFile.txt in the jni folder with the C implementation file.
推荐答案
与资产的事情是,你不能直接访问它们的文件。这是因为在资产从对APK直接读取。他们并没有解压缩到安装时给定的文件夹。
The thing with assets is that you can't access them directly as files. This is because the assets are read directly from the APK. They're not unzipped to a given folder upon installation.
这是Android 2.3开始,有一个C API来访问资产。看看<安卓/ asset_manager.h>
和 assetManager
字段&LT ;安卓/ native_activity.h>
。我从来没有用这个,虽然,我不知道,你可以使用这个资产管理API,如果你不依赖于原生的活动。不管怎样,这不会在Android 2.2及以下的工作。
Starting from Android 2.3, there is a C API to access assets. Have a look at <android/asset_manager.h>
and the assetManager
field in <android/native_activity.h>
. I've never used this though, and I'm not sure that you can use this asset manager API if you don't rely on a native activity. And anyway, this won't work on Android 2.2 and below.
所以,我看到三个选项:
So I see three options:
- 您可以提取的资产注入一些目录,但是这将需要额外的空间
- 您可以(bunlde和)使用类似libzip读取来自APK资产的纯C。
- 或者,避免捆绑额外的图书馆,我个人的preference是读取C数据,使用JNI,从Java
的InputStream
对象返回AssetManager.open()
。这需要一点code,但它的伟大工程。
- you could extract the assets into some directory but this will take extra space
- you could (bunlde and) use something like libzip to read the assets from the APK in pure C.
- or, to avoid bundling an extra library, my personal preference is to read data in C, using JNI, from the Java
InputStream
object returned byAssetManager.open()
. It takes a little code but it works great.
这篇关于Android的资产,C,JNI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!