Android FAQ: JNI reading assets resource files (with source code)

Source: Internet
Author: User

(To) http://blog.csdn.net/fontlose/article/details/6686161

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:

<pre name="code" class="cpp">public native void  readFromAssets(AssetManager ass,String filename);  

 

Readfromassets (getassets (), "log.txt ");

Logcat test results

12-15 15:27:33. 290: INFO/readassets (3570): readassets 12-15 15:27:33. 290: INFO/readassets (3570): file size: 138 12-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:

<pre name="code" class="cpp"><pre name="code" class="cpp">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): readassets 12-15 15:28:03. 440: INFO/readassets (3570): file numfiles 14 12-15 15:28:03. 440: INFO/readassets (3570): file 0: assets/log 2.txt size1: 138 size2: 55 12-15 15:28:03. 440: INFO/readassets (3570): File 1: assets/log.txt size1: 138 size2: 55 12-15 15:28:03. 440: INFO/readassets (3570): file 2: Res/layout/main. XML size1: 956 size2: 337 12-15 15:28:03. 440: I Nfo/readassets (3570): file 3: androidmanifest. XML size1: 1348 size2: 531 12-15 15:28:03. 440: INFO/readassets (3570): file 4: resources. ARSC size1: 1480 size2: 1480 12-15 15:28:03. 440: INFO/readassets (3570): File 5: Res/drawable-hdpi/icon.png size1: 3966 size2: 3966 12-15 15:28:03. 440: INFO/readassets (3570): file 6: Res/drawable-ldpi/icon.png size1: 1537 size2: 1537 12-15 15:28:03. 440: INFO/readassets (3 570): file 7: Res/drawable-mdpi/icon.png size1: 2200 size2: 2200 12-15 15:28:03. 440: INFO/readassets (3570): file 8: classes. dex size1: 3468 size2: 1680 12-15 15:28:03. 440: INFO/readassets (3570): file 9: lib/armeabi/libzip. so size1: 217246 size2: 46140 12-15 15:28:03. 440: INFO/readassets (3570): file 10: lib/armeabi/libreadres. so size1: 3820 size2: 1779 12-15 15:28:03. 440: INFO/readassets (3570): file 11: META-INF/manifest. mf size1: 852 size2: 443 12-15 15:28:03. 440: INFO/readassets (3570): file 12: META-INF/cert. SF size1: 905 size2: 487 12-15 15:28:03. 440: INFO/readassets (3570): file 13: META-INF/cert. RSA size1: 776 size2: 606 12-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    := readres  LOCAL_SRC_FILES := readres.c   LOCAL_C_INCLUDES+= /opt/android-ndk-r5/platforms/android-9/arch-arm/usr/include  LOCAL_LDLIBS    += -L/opt/android-ndk-r5/platforms/android-9/arch-arm/usr/lib/ -llog  LOCAL_LDLIBS    += -landroid  LOCAL_LDLIBS    += -lz   LOCAL_LDLIBS    += -L$(LOCAL_PATH) -lzip  include $(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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.