#include <jni.h> #include <string> #include <android/log.h> #define LOG_TAG "MyCTestLog" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) #include <iostream> #include <unistd.h> #include <dirent.h>
void List(const char *path, int level, vector<string> &strvec) { struct dirent* ent = NULL; DIR *pDir; pDir = opendir(path); if (pDir == NULL) { return; } while (NULL != (ent = readdir(pDir))) { if (ent->d_type == 8) { //file strvec.push_back(ent->d_name); } else { if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { continue; } //directory string _path(path); string _dirName(ent->d_name); string fullDirPath = _path + "/" + _dirName; List(fullDirPath.c_str(), level + 1, strvec); } } } extern "C" JNIEXPORT void JNICALL Java_com_example_ndkapplication_MainActivity_listFileFromJni2(JNIEnv *env, jobject thiz) { // TODO: implement listFileFromJni2() vector<string> files; List("/sdcard/Alarms",0,files); for (vector<string>::iterator iterator=files.begin();iterator!=files.end();++iterator) { LOGD("file name=%s", (*iterator).c_str()); } }
log打印如下:
2020-07-20 10:46:35.811 7397-7397/com.example.ndkapplication D/MyCTestLog: file name=1
2020-07-20 10:46:35.811 7397-7397/com.example.ndkapplication D/MyCTestLog: file name=3
2020-07-20 10:46:35.811 7397-7397/com.example.ndkapplication D/MyCTestLog: file name=3
2020-07-20 10:46:35.811 7397-7397/com.example.ndkapplication D/MyCTestLog: file name=6