jni读取assets资源文件

网友投稿 922 2022-05-30

assets目录底下的文件会被打包到一个apk文件里,这些资源在安装时他们并没被解压,使用时是直接从apk中读取的。这里介绍下怎么在jni内使用ndk自带api的接口函数读取assets资源文件,和libzip库函数的使用,可以用来读创建修改压缩文档,这里也是以读取apk安装包内的资源文件为例。

1 用ndk自带的接口函数读apk包

从2.3开始提供这些接口函数,具体看头文件assert.h android/asset_manager.h android/asset_manager_jni.h,可以参考ndk自带例子中samples/native-audio/ jni/native-audio-jni.c。

/*******************************************************************************

* Function Name : java_com_fontlose_ReadAssets_readFromAssets

* Description : 定义:public native void readFromAssets(AssetManager ass,String filename);

* Input : AssetManager对象 filename资源名

* Output : None

* Return : None

*******************************************************************************/

void Java_com_fontlose_ReadAssets_readFromAssets(JNIEnv* env,jclass tis,jobject assetManager,jstring filename)

{

LOGI("ReadAssets");

jni读取assets资源文件

AAssetManager* mgr = AAssetManager_fromJava(env, assetManager);

if(mgr==NULL)

{

LOGI(" %s","AAssetManager==NULL");

return ;

}

/*获取文件名并打开*/

jboolean iscopy;

const char *mfile = (*env)->GetStringUTFChars(env, filename, &iscopy);

AAsset* asset = AAssetManager_open(mgr, mfile,AASSET_MODE_UNKNOWN);

(*env)->ReleaseStringUTFChars(env, filename, mfile);

if(asset==NULL)

{

LOGI(" %s","asset==NULL");

return ;

}

/*获取文件大小*/

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);

/*关闭文件*/

AAsset_close(asset);

}

public native void readFromAssets(AssetManager ass,String filename);

readFromAssets(getAssets(),"log.txt");

logcat测试结果

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): 这个例子从jni读取assets内文件 12-15 15:27:33.290: INFO/ReadAssets(3570): 。。。。。。。。。。。。。。。。

2 使用libzip库读apk包

void Java_com_fontlose_ReadAssets_readFromAssetsLibzip(JNIEnv* env,jclass tis,jstring assetpath,jstring filename)

{

LOGI("ReadAssets");

int i=0;

jboolean iscopy;

const char *mpath = (*env)->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(apkArchive);

LOGI("File numFiles %i \n",numFiles);

for (i=0; 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)->GetStringUTFChars(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);

}

public native void readFromAssetsLibzip(String apkpath,String filename);

readFromAssetsLibzip(getPackageResourcePath(),"assets/log.txt");

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: INFO/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(3570): 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): 这个例子从jni读取assets内文件 12-15 15:28:03.440: INFO/ReadAssets(3570): 。。。。。。。。。。。。。。。。

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)

。。。。。。。。。。。。。。。。 这个例子从jni读取assets内文件 。。。。。。。。。。。。。。。。

Android

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:帮小姐姐打分系统的模型创建,滚雪球学 Python 第三轮第 11 篇
下一篇:8、QT基础——常用控件
相关文章