NDK遍历sdcard下面的目录(C代码实现,JNI)
view plain
- #include?<jni.h>??
- #include?<stdio.h>??
- #include?<stdlib.h>??
- #include?<string.h>??
- #include?<dirent.h>??
- #include?<time.h>??
- #include?<android/log.h>??
- ??
- ??
- #define?LOG_TAG?"Test"??
- #define?LOGI(...)??__android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)??
- #define?LOGE(...)??__android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)??
- ??
- #define?PATHMAX?1024??
- ??
- void?searchdir(char?*path);??
- ??
- int?total?=?0;??
- ??
- ??
- void?searchdir(char?*path)??
- {??
- ????DIR?*dp;??
- ????struct?dirent?*dmsg;??
- ????int?i=0;??
- ????char?addpath[PATHMAX]?=?{'\0'},?*tmpstr;??
- ????if?((dp?=?opendir(path))?!=?NULL)??
- ????{??
- ??????
- ??????while?((dmsg?=?readdir(dp))?!=?NULL)??
- ??????{??
- ??
- ????????if?(!strcmp(dmsg->d_name,?".")?||?!strcmp(dmsg->d_name,?".."))??
- ????????????continue;??
- ????????strcpy(addpath,?path);??
- ????????strcat(addpath,?"/");??
- ????????strcat(addpath,?dmsg->d_name);??
- ????????if?(dmsg->d_type?==?DT_DIR?)??
- ????????{??
- ????????????char?*temp;??
- ????????????temp=dmsg->d_name;??
- ????????????if(strchr(dmsg->d_name,?'.'))??
- ????????????{??
- ???????????????if((strcmp(strchr(dmsg->d_name,?'.'),?dmsg->d_name)==0))??
- ???????????????{??
- ?????????????????continue;??
- ???????????????}??
- ????????????}??
- ????????????LOGI("........directname:%s",dmsg->d_name);??
- ????????????searchdir(addpath);??
- ????????}??
- ??????}??
- ????}??
- ????closedir(dp);?????
- }??
- ??
- ??
- JNIEXPORT?jfloat?JNICALL?Java_com_docintest_SimpleNdkActivity_getSecond??
- ??(JNIEnv?*env,?jobject?thiz)??
- ??
- {??
- ????clock_t???tick_start,tick_end;??
- ????double?t;??
- ????char?*dirpath="/mnt/sdcard/";??
- ????LOGI("begin....................................................?function.");??
- ????tick_start=clock();??
- ????searchdir(dirpath);??
- ????tick_end=clock();??
- ????float?dtime=?(float)(tick_end-tick_start)/1000/1000;??
- ????printf(?"Total???time???used:%f???second\n?",dtime);??
- ????return?dtime;??
- ??
- }??
?
上(C代码)
(下)JAVA?
?
?
?
view plain- package?com.docintest;??
- ??
- import?android.app.Activity;??
- import?android.os.Bundle;??
- import?android.widget.TextView;??
- ??
- public?class?SimpleNdkActivity?extends?Activity?{??
- ????/**?Called?when?the?activity?is?first?created.?*/??
- ????@Override??
- ????public?void?onCreate(Bundle?savedInstanceState)?{??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.main);??
- ????????TextView?tv=(TextView)findViewById(R.id.second);??
- ????????tv.setText("Total?time:"+getSecond()+"");??
- ????}??
- ??????
- ????private??native?float??getSecond();??
- ??????
- ????static?{??
- ????????System.loadLibrary("test");??
- ????}??
- }??
?
结果:
?

?
