android开发jni开发遍历文件夹下的文件以及目录
//kotlin层
external fun listFiles(filePath:String): String
//native层
#include <jni.h>
#include <string>
#include <dirent.h>
#include <android/log.h>
#ifndef LOG_TAG
#define LOG_TAG "HELLO_JNI"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG ,__VA_ARGS__) // 定义LOGD类型
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG ,__VA_ARGS__) // 定义LOGI类型
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG_TAG ,__VA_ARGS__) // 定义LOGW类型
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG ,__VA_ARGS__) // 定义LOGE类型
#define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,LOG_TAG ,__VA_ARGS__) // 定义LOGF类型
#endif
extern "C"
JNIEXPORT jstring JNICALL
Java_com_suyf_ndkdev_MainActivity_listFiles(JNIEnv *env, jobject thiz, jstring file_path) {
const char *path = env->GetStringUTFChars(file_path, 0);
DIR *dirPtr = opendir(path);
struct dirent *ptr;
char base[1000];
if (dirPtr == NULL) {
LOGE("opendir fail.....................");
exit(-1);
}
while ((ptr = readdir(dirPtr)) != NULL) {
if (strcmp(ptr->d_name, ".") == 0 ||
strcmp(ptr->d_name, "..") == 0) ///current dir OR parrent dir
continue;
else if (ptr->d_type == 8) ///file
LOGE("d_name:%s/%s\n", path, ptr->d_name);
else if (ptr->d_type == 10) ///link file
printf("d_name:%s/%s\n", path, ptr->d_name);
else if (ptr->d_type == 4) { ///dir
memset(base, '\0', sizeof(base));
strcpy(base, path);
strcat(base,"/");
strcat(base,ptr->d_name);
LOGE("d_dir:%s/%s\n", path, ptr->d_name);
jstring newDir = env->NewStringUTF(base);
Java_com_suyf_ndkdev_MainActivity_listFiles(env,thiz,newDir);
}
}
closedir(dirPtr);
return file_path;
}
错误记录:
Fatal signal 5 (SIGTRAP) code 1 (TRAP_BRKPT)
native层函数缺少return返回值
分类:
android-jni
标签:
android-jni
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
2020-09-09 android开发文字转bitmap的实现