Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Resource Files from my own APK in Android Native Environment

I'm porting to Android. My existing project has a ton of resource files that I'm porting into my Android project. I have them all in /res/raw/, and I would like to access those resources in my native library with functions such as fopen() and such. Can this be done, or do I have to go through JNI for this as well? I would really prefer not to, for ease of porting and possible speed and memory reasons.

like image 615
Kalen Avatar asked Apr 16 '10 09:04

Kalen


People also ask

How to open resource file in Android studio?

Click the target app module in the Project window (while in either the Android or Project view), and then select File > New > Android resource file. Fill in the details in the dialog: File name: Type the name for the XML file (does not require the . xml suffix).

What is Android resource directory?

In Android, a resource is a localized text string, bitmap, layout, or other small piece of noncode information that your app needs. At build time, all resources get compiled into your application. The resources locates inside res directory of your app.

What is the name of resource file in Android?

As you can see in this example, the res/ directory contains all the resources (in subdirectories): an image resource, two layout resources, mipmap/ directories for launcher icons, and a string resource file.

What are resource files?

A resource file is a text file with the extension . rc. The file can use single-byte, double-byte, or Unicode characters. The syntax and semantics for the RC preprocessor are similar to those of the Microsoft C/C++ compiler. However, RC supports a subset of the preprocessor directives, defines, and pragmas in a script.


2 Answers

If you prevent your assets from being compressed as they are added into your APK then you can use openRawResourceFd() to get a file descriptor, offset, and size and pass them to your native code where you just do an fopen and fseek. On the other hand, if you let the build system compress the files you want to access, you will need to use something like libzip to read them from the APK.

like image 50
Tim Kryger Avatar answered Sep 30 '22 22:09

Tim Kryger


Check out obb.h in the ndk.

From docs/STABLE-APIS.html in the Android NDK:

<android/obb.h>
        Direct (read-only) access to assets embedded in your .apk. or
        the Opaque Binary Blob (OBB) files, a new feature of Android X.X
        that allows one to distribute large amount of application data
        outside of the .apk (useful for game assets, for example).
like image 42
Justin Buser Avatar answered Sep 30 '22 22:09

Justin Buser