版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/afei__/article/details/81334364
Java 层只给 JNI 层传递文件夹路径,在 JNI 中完成文件夹里的文件操作,通常都避免不了需要使用到 文件夹遍历 这个功能。那么,在 NDK 开发中怎么进行文件夹的遍历呢?
其实这就和 C++ 的文件夹遍历方式差不多。NDK 也给我们提供了 <dirent.h> 头文件,通过这个头文件中的相关方法,我们就可以完成这个功能了。
native-lib.cpp代码如下:
#include <jni.h>
#include <string.h>
#include <iostream>
#include <dirent.h>
#include "log.hpp"
extern "C" {
using namespace std;
void showAllFiles(string dir_name) {
// check the parameter
if (dir_name.empty()) {
LOGE("dir_name is null !");
return;
}
DIR *dir = opendir(dir_name.c_str());
// check is dir ?
if (NULL == dir) {
LOGE("Can not open dir. Check path or permission!");
return;
}
struct dirent *file;
// read all the files in dir
while ((file = readdir(dir)) != NULL) {
// skip "." and ".."
if (strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0) {
LOGV("ignore . and ..");
continue;
}
if (file->d_type == DT_DIR) {
string filePath = dir_name + "/" + file->d_name;
showAllFiles(filePath); // 递归执行
} else {
// 如果需要把路径保存到集合中去,就在这里执行 add 的操作
LOGI("filePath: %s/%s", dir_name.c_str(), file->d_name);
}
}
closedir(dir);
}
JNIEXPORT void JNICALL
Java_com_afei_jnidemo_MainActivity_showDir(JNIEnv *env, jobject instance, jstring dirPath_) {
const char *dirPath = env->GetStringUTFChars(dirPath_, 0);
showAllFiles(string(dirPath));
env->ReleaseStringUTFChars(dirPath_, dirPath);
}
}
MainActivirt 调用示例:
package com.afei.jnidemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
static {
System.loadLibrary("native-lib");
}
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showDir("/sdcard/sensetime/detect/batch");
}
public native void showDir(String dirPath);
}
运行结果:
08-01 17:09:32.919 13820-13820/com.afei.jnidemo V/FaceAPI: ignore . and ..
ignore . and ..
08-01 17:09:32.919 13820-13820/com.afei.jnidemo I/FaceAPI: filePath: /sdcard/sensetime/detect/batch/2017.12.13-11.05.33_0.19583142.jpg
filePath: /sdcard/sensetime/detect/batch/2017.12.13-11.25.31_0.6432047.jpg
filePath: /sdcard/sensetime/detect/batch/2017.12.13-11.20.19_0.7755328.jpg
filePath: /sdcard/sensetime/detect/batch/2017.12.13-11.08.23_0.085364416.jpg
filePath: /sdcard/sensetime/detect/batch/2017.12.11-11.20.06_0.9677857.jpg
filePath: /sdcard/sensetime/detect/batch/2017.12.13-11.17.25_0.96264386.jpg
filePath: /sdcard/sensetime/detect/batch/2017.12.13-11.25.30_0.9945698.jpg
08-01 17:09:32.919 13820-13820/com.afei.jnidemo V/FaceAPI: ignore . and ..
ignore . and ..
08-01 17:09:32.919 13820-13820/com.afei.jnidemo I/FaceAPI: filePath: /sdcard/sensetime/detect/batch/q/2017.12.13-11.28.16_0.17593002.jpg
filePath: /sdcard/sensetime/detect/batch/q/2017.12.12-11.30.46_0.6113686.jpg
filePath: /sdcard/sensetime/detect/batch/q/2017.12.13-11.46.55_0.9884704.jpg
filePath: /sdcard/sensetime/detect/batch/q/2017.12.13-11.30.08_0.12916178.jpg
filePath: /sdcard/sensetime/detect/batch/q/2017.12.13-11.41.12_0.54208493.jpg
08-01 17:09:32.919 13820-13820/com.afei.jnidemo V/FaceAPI: ignore . and ..
ignore . and ..
08-01 17:09:32.919 13820-13820/com.afei.jnidemo I/FaceAPI: filePath: /sdcard/sensetime/detect/batch/w/2017.12.12-11.28.26_0.48372716.jpg
filePath: /sdcard/sensetime/detect/batch/w/2017.12.11-11.14.04_0.04731889.jpg
filePath: /sdcard/sensetime/detect/batch/w/2017.12.12-11.21.32_0.456345.jpg
filePath: /sdcard/sensetime/detect/batch/w/2017.12.13-11.46.51_0.23463911.jpg
filePath: /sdcard/sensetime/detect/batch/w/2017.12.13-11.05.48_0.028296137.jpg
filePath: /sdcard/sensetime/detect/batch/w/IMG_20170207_165920.JPG
08-01 17:09:32.919 13820-13820/com.afei.jnidemo V/FaceAPI: ignore . and ..
ignore . and ..
08-01 17:09:32.919 13820-13820/com.afei.jnidemo I/FaceAPI: filePath: /sdcard/sensetime/detect/batch/e/2017.12.12-11.18.55_0.1639892.jpg
filePath: /sdcard/sensetime/detect/batch/e/2017.12.12-11.18.53_0.011175653.jpg
filePath: /sdcard/sensetime/detect/batch/e/2017.12.13-11.46.53_0.324961.jpg
其它:
log.hpp 的介绍在这个链接:NDK 开发之 Android log 输出的工具类封装
NDK 学习系列:Android NDK 从入门到精通(汇总篇)