递归扫描SD卡Android NDK

Recursivly scan sd card android ndk

本文关键字:Android NDK SD 扫描 递归      更新时间:2023-10-16

我在Android NDK中确实是新手,我感谢任何帮助。如何使用检查扩展名中的C 中扫描递归文件夹?我知道在Java很容易做。在Java中,我使用此:

public void scan(File root) {
        File[] list = root.listFiles(tracksFilter);
        for (File f : list) {
            String path;
            if (f.isDirectory()) {
                scan(f);
            } else if(path.endWith(".mp3"){
                 doMP3(f);
            } else if(path.endWith(".png"){
                 doPNG(f);
            }
        }
    }

值得记住的是,本机代码不会总是出于多种原因而导致绩效提高。在Java代码和等效的本机代码之间进行速度比较可能是有益的。结果可能会让您感到惊讶:)

也就是说,以下C 代码应该使您朝正确的方向前进。

...
#include <dirent.h>
#include <string>
#include <iostream>
....
static const string curDir = ".";
static const string parDir = "..";
....
void iterateDir(string path)
{
    DIR *dir;
    struct dirent *drnt;
    dir = opendir(path.c_str());
    while ((drnt = readdir(dir)) != NULL)
    {
        string name(drnt->d_name);
        unsigned char = drnt->d_type;
        if (name != curDir && name != parDir && name.length() >= 4)
        {
            if (type == DT_DIR) {
                string newPath = path + name + "/";
                iterateDir(newPath);
            }
            else if (name.find(".mp3") == (name.length() - 4)) {
                doMP3(path + name);
            }
            else if (name.find(".png") == (name.length() - 4)) {
                doPNG(path + name);
            }
        }
}