Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android assets, C, JNI

How do you access an Android asset, such as a .txt file, from C with the 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.

like image 399
SK9 Avatar asked Jan 24 '11 08:01

SK9


1 Answers

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.

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:

  • 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 by AssetManager.open(). It takes a little code but it works great.
like image 79
olivierg Avatar answered Oct 04 '22 12:10

olivierg