Load Assets from APK on Android in NDK

Previously, for those assets like model, textures I will use ‘adb push’ command to upload them into a separate folder /data, and load them in native code. It is not good way, because I need to keep the data and program match with two deferent steps.  Actually, we could put the assets into the .apk file and them load the asset files from the .apk file.

 

APK Is ZIP file

APK is some like a ZIP file. So we need libzip to help us reading resources from the APK file. Luckily, someone already did something or us. You could download a android ndk version from here.

 

Where is APK File Path

Before you could load anything, you need to find a way to figure out where you current .APK package is. This could be done with the Java code, just as following:

String apkFilePath = "";
PackageManager packMgmr = context.getPackageManager();
try 
{
    ApplicationInfo appInfo = packMgmr.getApplicationInfo("com.easygame", 0);
    apkFilePath = appInfo.sourceDir;
} 
catch (NameNotFoundException e) 
{
    e.printStackTrace();
    throw new RuntimeException("Unable to locate assets, aborting...");
}

 

Wrap the  ZIP File Read API with a Memory File
If you check the downloaded ZIP File Read API, you will find that there is no function something like zip_fseek, only zip_fopen and zip_fread. Oh, you can not seek in file!!!
There are some solutions that you could adapt.  
a) Replace the fseek with fread, read them but discard them to seek somewhere;
b) Continue search some other libraries on the Internet that could provide seek function;
c) Read the libzip and relative code and document carefully, and write your own version;
d) Load the whole file into the memory and seek it in the memory;
……

Of course, I taken the solution d) that was the easiest one. What you need to do is load the whole file content with customize class file  open function, do memory copy with the read function and offset the memory address while do seeking.

 

Reference
http://androgeek.info/?p=275

转载于:https://www.cnblogs.com/open-coder/archive/2012/12/24/2831590.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值