fopen file from resource package?

5,294 views
Skip to first unread message

deki

unread,
Aug 30, 2009, 6:17:18 PM8/30/09
to android-ndk
Hi list,

How can I get / generate a path for use in fopen in native code to
open a file say in res/drawable?

I realize it might not be possible entirely in native to generate that
path, even If I have to pass the path from java to c++ that would be
fine.

Thanks a bunch!

D

Dianne Hackborn

unread,
Aug 30, 2009, 7:39:25 PM8/30/09
to andro...@googlegroups.com
You can't; those files are packed into a single .zip and can't be individually open.
--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

RzR www.rzr.online.fr

unread,
Aug 31, 2009, 2:18:36 PM8/31/09
to andro...@googlegroups.com
On Mon, Aug 31, 2009 at 1:39 AM, Dianne Hackborn<hac...@android.com> wrote:
> You can't; those files are packed into a single .zip and can't be
> individually open.

One workaround is to put (and rename) the files into lib folder ...
I did this in some JNI application ...

Regards

--
Related Obsession : http://rzr.online.fr/q/jvm

Anders Johansson

unread,
Oct 9, 2009, 8:34:24 AM10/9/09
to android-ndk
I'm not sure about Resources, but you can do it with Assets through a
hack...

1. In Java, get your apk file path through
PackageManager.getApplicationInfo(...)
2. In java, get offset and size of asset in the apk through
Context.getAssets().openFd().getStartOffset()/getLength()
3. In JNI/C, open your apk file, seek to offset and read size bytes
into a buffer
4. ???
5. Profit!

I leave no guarantees on forward compatibility and/or portability, but
it works... :)

regards
Anders

Dianne Hackborn

unread,
Oct 9, 2009, 12:50:55 PM10/9/09
to andro...@googlegroups.com
Um be sure you close the fd.  And really, just pass the fd along with the start offset and and offset through to JNI.  I guarantee that will work, it is how the API is intended to be used (regardless of native code).  Assuming that the start offset and end offset are absolute positions in your .apk is not guaranteed to work in the future.  (For example, what happens if one day we decide that when an app is installed we unpack its .apk into individual files?)

Steve

unread,
Oct 11, 2009, 8:28:38 AM10/11/09
to android-ndk
Hi there, from reading this thread, I am thinking the best way for me
to acess all my assets, from native code, would be to bundle all my
assets into a single WAD/DAT file, and then include this as an asset
for the Java side to see. There are then a couple of Java side API
calls that I can make, to get the files start address in memory, and
also ths size, I can therefore just pass these two values across to
the native code and access it. Is this correct?

Would it be at all possible for someone to provide me with the Java
side calls to do this? I am looking through the various resource
functions and am not sure which one I need, and making sure if any
file handles are opened making sure they are closed etc.

Finally, roughly on topic, I also want to be able to write out a save/
settings file somewhere, could anyone give me in a nudge in the right
direction for what I should be doing here? I'd like to write out this
file from the native side ideally, but if that simply isn't possible I
could put the data I want to write out in to memory, and then get the
Java code to write this data out, presumably?

Many thanks for all your help,

Steve
> hack...@android.com
Reply all
Reply to author
Forward
0 new messages