Files under the Assets Directory will be packaged into an APK file. These resources are not extracted during installation and are directly read from the APK during use. This section describes how to use the interface functions provided by ndk in JNI to read assets resource files and libzip library functions, which can be used to read creation, modification, and compression documents, here, we also take the resource file in the APK installation package as an example.
1. Read the APK package using the interface functions provided by ndk. These interface functions are provided starting from 2.3. For details, refer to the header file assert. h Android/asset_manager.h Android/asset_manager_jni.h, you can refer to the ndk built-in example samples/Native-Audio/JNI/native-audio-jni.c.
/*************************************** **************************************** * Function Name: java_com_fontlose_readassets_readfromassets * Description: Definition: Public native void readfromassets (assetmanager ass, string filename); * input: assetmanager object filename resource name * output: none * return: none *************************************** **************************************** /void java_com_fontlose_readassets_readfromassets (jnienv * ENV, jclass tis, jobject assetmanager, jstring filename) {Logi ("readassets"); aassetmanager * Mgr = aassetmanager_fromjava (ENV, assetmanager); If (MGR = NULL) {Logi ("% s", "aassetmanager = NULL"); return;}/* get the file name and open */jboolean iscopy; const char * mfile = (* env) -> getstringutfchars (ENV, filename, & iscopy); aasset * asset = aassetmanager_open (MGR, mfile, callback); (* env)-> releasestringutfchars (ENV, filename, mfile ); if (Asset = NULL) {Logi ("% s", "asset = NULL"); return ;} /* Get file size */off_t buffersize = aasset_getlength (asset); Logi ("file size: % d \ n", buffersize); char * buffer = (char *) malloc (buffersize + 1); buffer [buffersize] = 0; int numbytesread = aasset_read (asset, buffer, buffersize); Logi (": % s", buffer ); free (buffer);/* close the file */aasset_close (asset );}
Use the following definitions and usage in applications:
public native void readFromAssets(AssetManager ass,String filename);
Readfromassets (getassets (), "log.txt ");
Logcat test results
12-15 15:27:33. 290: INFO/readassets (3570): ReadAssets12-15 15:27:33. 290: INFO/readassets (3570): file size: 13812-15 15:27:33. 290: INFO/readassets (3570 )::................ 12-15 15:27:33. 290: INFO/readassets (3570): In this example, the files in assets are read from JNI at 15:27:33 am from 12-15. 290: INFO/readassets (3570 ):................
2. Use the libzip library to read the APK package
Libzip uses the C library to read and create the modified archive. For more information, see the Android-ndk-assets.zip project created in the old version. You can compile it in the ndk and compile the libzip file. so, use libzip. so and zip. h. Create a project and use libzip to read other compressed files in the APK package, such as androidmanifest. XML layout.
/*************************************** **************************************** * Function Name: java_com_fontlose_readassets_readfromassetslibzip * Description: Definition: Public native void readfromassetslibzip (string apkpath, string filename); * input: apkpath filename resource name * output: none * return: none *************************************** **************************************** /void java_com_fontlo Se_readassets_readfromassetslibzip (jnienv * ENV, jclass tis, jstring assetpath, jstring filename) {Logi ("readassets"); int I = 0; jboolean iscopy; const char * mpath =) -> getstringutfchars (ENV, assetpath, & iscopy); struct zip * apkarchive = zip_open (mpath, 0, null); (* env)-> releasestringutfchars (ENV, filename, mpath); struct zip_stat fstat; zip_stat_init (& fstat); int numfiles = zip_get_num_files (apkar Chive); Logi ("file numfiles % I \ n", numfiles); for (I = 0; I <numfiles; I ++) {const char * name = zip_get_name (apkarchive, I, 0); If (name = NULL) {LogE ("error reading zip file name at index % I: % s ", zip_strerror (apkarchive); return;} zip_stat (apkarchive, name, 0, & fstat); Logi ("file % I: % s size1: % d size2: % d ", i, fstat. name, fstat. size, fstat. comp_size);} const char * fname = (* env)-> getstringutfcha RS (ENV, filename, & iscopy); struct zip_file * file = zip_fopen (apkarchive, fname, 0); If (! File) {LogE ("error opening % s from APK", fname); return;} zip_stat (apkarchive, fname, 0, & fstat); (* env) -> releasestringutfchars (ENV, filename, fname); char * buffer = (char *) malloc (fstat. size + 1); buffer [fstat. size] = 0; int numbytesread = zip_fread (file, buffer, fstat. size); Logi (": % s \ n", buffer); free (buffer); zip_fclose (File); zip_close (apkarchive );}
Use the following definitions and usage in applications:
public native void readFromAssetsLibzip(String apkpath,String filename);
Readfromassetslibzip (getpackageresourcepath (), "assets/log.txt ");
Logcat test results
12-15 15:28:03. 430: INFO/readassets (3570): ReadAssets12-15 15:28:03. 440: INFO/readassets (3570): file numfiles 1412-15 15:28:03. 440: INFO/readassets (3570): file 0: assets/log 2.txt size1: 138 size2: 5512-15 15:28:03. 440: INFO/readassets (3570): File 1: assets/log.txt size1: 138 size2: 5512-15 15:28:03. 440: INFO/readassets (3570): file 2: Res/layout/main. XML size1: 956 size2: 33712-15 15:28:03. 440: INFO/R Eadassets (3570): file 3: androidmanifest. XML size1: 1348 size2: 53112-15 15:28:03. 440: INFO/readassets (3570): file 4: resources. ARSC size1: 1480 size2: 148012-15 15:28:03. 440: INFO/readassets (3570): File 5: Res/drawable-hdpi/icon.png size1: 3966 size2: 396612-15 15:28:03. 440: INFO/readassets (3570): file 6: Res/drawable-ldpi/icon.png size1: 1537 size2: 153712-15 15:28:03. 440: INFO/readassets (3570): fil E 7: Res/drawable-mdpi/icon.png size1: 2200 size2: 220012-15 15:28:03. 440: INFO/readassets (3570): file 8: classes. dex size1: 3468 size2: 168012-15 15:28:03. 440: INFO/readassets (3570): file 9: lib/armeabi/libzip. so size1: 217246 size2: 4614012-15 15:28:03. 440: INFO/readassets (3570): file 10: lib/armeabi/libreadres. so size1: 3820 size2: 177912-15 15:28:03. 440: INFO/readassets (3570): file 11: META-INF/man Ifest. mf size1: 852 size2: 44312-15 15:28:03. 440: INFO/readassets (3570): file 12: META-INF/cert. SF size1: 905 size2: 48712-15 15:28:03. 440: INFO/readassets (3570): file 13: META-INF/cert. RSA size1: 776 size2: 60612-15 15:28:03. 440: INFO/readassets (3570 )::................ 12-15 15:28:03. 440: INFO/readassets (3570): In this example, the files in assets are read from JNI at 15:28:03 am from 12-15. 440: INFO/readassets (3570 ):................
The above example uses Android. mk as follows:
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := readresLOCAL_SRC_FILES := readres.c LOCAL_C_INCLUDES+= /opt/android-ndk-r5/platforms/android-9/arch-arm/usr/includeLOCAL_LDLIBS += -L/opt/android-ndk-r5/platforms/android-9/arch-arm/usr/lib/ -llogLOCAL_LDLIBS += -landroidLOCAL_LDLIBS += -lz LOCAL_LDLIBS += -L$(LOCAL_PATH) -lzipinclude $(BUILD_SHARED_LIBRARY)
The log.txt content is as follows:
................ This example reads files in assets from JNI ................
JNI project and test project source code I have uploaded to my resources interested in children's shoes can look down
Http://d.download.csdn.net/down/3518697/fontlose