Android应用往往会有很多资源文件需要使用,这些资源文件一般会放在assets目录编进apk中,当apk中使用了so库的时候,由于编进apk的资源文件没有固定的目录,因此不能在C层通过设置路径的方式读取;这里提供两种C层读取资源文件的方式,仅供参考。
# 方法一(通过绝对路径访问)
由于在apk的安装过程中assets中的文件并没有从apk包中解压出来,所以在JNI的C++代码中不能按照原始的路径直接进行访问,一种常用的方法为将assets中的文件复制到sdcard的目录下,然后传递绝对路径给JNI中的C++代码中进行访问。
#### 将assets中的文件复制到sdcard的目录下 工具类
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
``` package com.zili.rtk.activity; /** * copy the files and folders of assets to sdCard to ensure that we can read files in JNI part * @AuThor Qinghao Hu * @date 2015/9/22 * @version 1.0 * @Email [email]qinghao.hu@nlpr.ia.ac.cn[/email] */ import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.content.res.AssetManager; import android.util.Log; public class AssetCopyer {
private static String TAG= "AssetCopyer" ; /** * copy all the files and folders to the destination * @Param context application context * @param destination the destination path */ public static void copyAllAssets(Context context,String destination) {
copyAssetsToDst(context, &
|