搜索整个SD卡特定文件 [英] Search entire sdcard for a specific file

查看:92
本文介绍了搜索整个SD卡特定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何查找文件,但使用特定的路径,例如/ SD卡/图片/文件。有没有一种方法来搜索特定的文件。示例:搜索文件。然后应用程序找到它在/ SD卡/图片。然后将它抹去。

I know how to search a file, but using a specific path, example /sdcard/picture/file. Is there a way to search for that specific file. Example: search for file. Then the app locate it in /sdcard/picture. Then erase it.

任何帮助吗?谢谢(我知道如何删除一个文件,但必须写的完整路径)

Any help? Thanks (I know how to erase a file, but have to write the entire path)

推荐答案

可以解决这个问题递归从外部存储/ SD卡的根目录开始。

You can solve that problem recursively starting from the root directory of the external storage / SD card.

一些未经检验的code我的头了(方法名可能是错误的)

Some untested code out of my head (method names could be wrong)

public File findFile(File dir, String name) {
    File[] children = dir.listFiles();

    for(File child : children) {
        if(child.isDirectory()) {
           File found = findFile(child, name);
           if(found != null) return found;
        } else {
            if(name.equals(child.getName())) return child;
        }
    }

    return null;
}

如果你想找到的所有的SD卡上出现的文件名,你将不得不使用一个列表,用于收集并返回找到的所有比赛。

If you want to find all occurrences of that file name on your SD card you'll have to use a List for collecting and returning all found matches.

这篇关于搜索整个SD卡特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆