zoukankan      html  css  js  c++  java
  • 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
    ", path, ptr->d_name);
            else if (ptr->d_type == 10)    ///link file
                printf("d_name:%s/%s
    ", path, ptr->d_name);
            else if (ptr->d_type == 4) {    ///dir
                memset(base, '', sizeof(base));
                strcpy(base, path);
                strcat(base,"/");
                strcat(base,ptr->d_name);
                LOGE("d_dir:%s/%s
    ", 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返回值

  • 相关阅读:
    动态调用WCF服务
    矩阵的坐标变换(转)
    【.NET线程--进阶(一)】--线程方法详解
    [转] Location语法规则
    [转] 深入理解vue 一些底层原理
    [转] lodash常用方法
    [转] Vue 组件间通信六种方式(完整版)
    [转] vuejs组件通信精髓归纳
    [转] 浅谈移动端设备标识码:DeviceID、IMEI、IDFA、UDID和UUID
    [转] vue自定义组件中的v-model简单解释
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/15248808.html
Copyright © 2011-2022 走看看