Android NDK: How to read assets from another thread in C++

357 views
Skip to first unread message

Karol Pysniak

unread,
Aug 18, 2013, 6:43:51 PM8/18/13
to andro...@googlegroups.com

I'm writing code in C++ for Android. The main code is written in Java, but I would like to start another thread invoked from JNI call and access assets from C++. However, when I pass AAssetsManager from getAssets(), it works only within scope of JNI function call. When other thread tries to use, I get an invalid pointer. I also made sure that the object AAssetsManager is not garbage collected by writing it to a static variable. Do you have any advice on how I could make it work?

Thanks

Tim Mensch

unread,
Aug 19, 2013, 6:09:56 AM8/19/13
to andro...@googlegroups.com
On 8/18/2013 4:43 PM, Karol Pysniak wrote:

I'm writing code in C++ for Android. The main code is written in Java, but I would like to start another thread invoked from JNI call and access assets from C++. However, when I pass AAssetsManager from getAssets(), it works only within scope of JNI function call. When other thread tries to use, I get an invalid pointer. I also made sure that the object AAssetsManager is not garbage collected by writing it to a static variable. Do you have any advice on how I could make it work?


Get the file path to the .APK, and use a C-based zip file reader to access the APK directly. zlib comes with a "contrib" library "minizip" that works.

If you're set on using AssetManager, then you may not be calling NewGlobalRef() on your AssetManager reference. (And if you're creating a new thread using pthreads, you also need to call AttachCurrentThread() at some point).

Tim

gadget

unread,
Aug 20, 2013, 2:10:05 PM8/20/13
to andro...@googlegroups.com
Sounds like you're trying to use java objects from a C thread that knows nothing of the underlying vm. My recommendation: start your C thread, attach it (like Tim said), then using the new JNIEnv* get the asset manager via JNI calls.
Reply all
Reply to author
Forward
0 new messages