Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access resources (like sound, images etc) directly from native code using Android-NDK?

I want to know that how can I directly access the resources like images, sound files etc. from native code i.e. C++ files. Actually I am looking for any example that could help me to use the asset_manager_jni.h methods.

Looking for suggestions. Thanks in advance.

With Regards,

Atul Prakash Singh

like image 900
Atul Prakash Singh Avatar asked Feb 23 '23 13:02

Atul Prakash Singh


1 Answers

Well, you have access to stdio.h. So if it's in a known place (say on the SD card), you can just use that as a path. And there's lot's of tutorials on the net about hot to use stdio (fopen, fclose, etc).

The issue is that resources you bundle into the apk itself (either in res/raw, or assets), stay inside the apk after install. What's worse is that by default, they will be compressed which makes reading it not feasible. This can be avoided, and the easiest way is to rename the asset to have the .mp3 extension (or there are others). The reason for this is because by default, .mp3 is not compressed, regardless of whether or not it actually is an mp3 file). There are other extensions you can use, and ways to tell the tools not to compress your data if you don't like naming all of your assets with .mp3 at the end.

So, you have a few choices here:

  1. Download your resources from the net on your first run, put them in an unobtrusive place (it's probably best to get that path from the sdk when you do the downloading), and use that.

  2. Store your resources in the apk (remember the .mp3 extension, in the assets folder). On your first run, extract the assets to a folder you have access to (and doesn't annoy the user), and use the resources from there.

  3. (what I do) Store your resources in the apk (.mp3 again), and use the jni to read directly from the apk. Yes, the jni is a bit slow, but you shouldn't be reading from the file system all that much anyway, and certainly not at a performance critical point. Nvidia has some very helpful code you can use, you can find it here, it's in the sample code if I remember. Inside the libs folder are some good general purpose libraries you can use that matches stdio, except it also reads from the apk itself.

Hope it helps.

like image 98
Leif Andersen Avatar answered Apr 27 '23 01:04

Leif Andersen