NDK遍历sdcard下面的目录(C代码实现,JNI)

  1. #include <jni.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <string.h>  
  5. #include <dirent.h>  
  6. #include <time.h>  
  7. #include <android/log.h>  
  8.   
  9.   
  10. #define LOG_TAG "Test"  
  11. #define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)  
  12. #define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)  
  13.   
  14. #define PATHMAX 1024  
  15.   
  16. void searchdir(char *path);  
  17.   
  18. int total = 0;  
  19.   
  20.   
  21. void searchdir(char *path)  
  22. {  
  23.     DIR *dp;  
  24.     struct dirent *dmsg;  
  25.     int i=0;  
  26.     char addpath[PATHMAX] = {'\0'}, *tmpstr;  
  27.     if ((dp = opendir(path)) != NULL)  
  28.     {  
  29.       
  30.       while ((dmsg = readdir(dp)) != NULL)  
  31.       {  
  32.   
  33.         if (!strcmp(dmsg->d_name, ".") || !strcmp(dmsg->d_name, ".."))  
  34.             continue;  
  35.         strcpy(addpath, path);  
  36.         strcat(addpath, "/");  
  37.         strcat(addpath, dmsg->d_name);  
  38.         if (dmsg->d_type == DT_DIR )  
  39.         {  
  40.             char *temp;  
  41.             temp=dmsg->d_name;  
  42.             if(strchr(dmsg->d_name, '.'))  
  43.             {  
  44.                if((strcmp(strchr(dmsg->d_name, '.'), dmsg->d_name)==0))  
  45.                {  
  46.                  continue;  
  47.                }  
  48.             }  
  49.             LOGI("........directname:%s",dmsg->d_name);  
  50.             searchdir(addpath);  
  51.         }  
  52.       }  
  53.     }  
  54.     closedir(dp);     
  55. }  
  56.   
  57.   
  58. JNIEXPORT jfloat JNICALL Java_com_docintest_SimpleNdkActivity_getSecond  
  59.   (JNIEnv *env, jobject thiz)  
  60.   
  61. {  
  62.     clock_t   tick_start,tick_end;  
  63.     double t;  
  64.     char *dirpath="/mnt/sdcard/";  
  65.     LOGI("begin.................................................... function.");  
  66.     tick_start=clock();  
  67.     searchdir(dirpath);  
  68.     tick_end=clock();  
  69.     float dtime= (float)(tick_end-tick_start)/1000/1000;  
  70.     printf( "Total   time   used:%f   second\n ",dtime);  
  71.     return dtime;  
  72.   
  73. }  


 

上(C代码)

(下)JAVA 

 

 

 

  1. package com.docintest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.TextView;  
  6.   
  7. public class SimpleNdkActivity extends Activity {  
  8.     /** Called when the activity is first created. */  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.main);  
  13.         TextView tv=(TextView)findViewById(R.id.second);  
  14.         tv.setText("Total time:"+getSecond()+"");  
  15.     }  
  16.       
  17.     private  native float  getSecond();  
  18.       
  19.     static {  
  20.         System.loadLibrary("test");  
  21.     }  
  22. }  

 

结果:

 

NDK遍历sdcard下面的目录(C代码实现,JNI)

 

NDK遍历sdcard下面的目录(C代码实现,JNI)


你可能感兴趣的:(sdcard)