Android编程:从何处开始创建简单的文件浏览器?

时间:2010-11-05 18:26:06

标签: android file browser

我想制作一个可以做两件事的文件浏览器: 1)允许用户浏览并选择目录 2)允许用户浏览其SD卡上的所有文件

我已经找了教程,但似乎找不到? 有人可以通过解释我的代码需要做什么才能拥有一个简单的文件浏览器或者为我提供教程/源代码的链接来帮助我吗?

请,谢谢!

3 个答案:

答案 0 :(得分:30)

如果你真的对学习自己的写作更感兴趣,我建议你仔细阅读File课程文档。这就是你要完成大部分工作的地方。

对于Android的SD卡/其他外部存储设备,您需要首先检查以确保在尝试使用Environment类之前安装并可用外部存储设备:

String extState = Environment.getExternalStorageState();
//you may also want to add (...|| Environment.MEDIA_MOUNTED_READ_ONLY)
//if you are only interested in reading the filesystem
if(!extState.equals(Environment.MEDIA_MOUNTED)) {
    //handle error here
}
else {
    //do your file work here
}

一旦确定了外部存储的正确状态,一个简单的启动方法是使用File的listFiles()方法,如下所示:

//there is also getRootDirectory(), getDataDirectory(), etc. in the docs
File sd = Environment.getExternalStorageDirectory();
//This will return an array with all the Files (directories and files)
//in the external storage folder
File[] sdDirList = sd.listFiles();

然后,您可以开始使用FileFilters来缩小搜索范围:

FileFilter filterDirectoriesOnly = new FileFilter() {
    public boolean accept(File file) {
        return file.isDirectory();
    }
};
File[] sdDirectories = sd.listFiles(filterDirectoriesOnly);

从那以后,只需阅读文档,找到你想要用它做的事情的类型,然后你就可以将它们绑定到列表适配器等等。

希望这有帮助!

答案 1 :(得分:20)

这是一个迟到的答案,但我最近创建了一个Android文件浏览器。 https://github.com/mburman/Android-File-Explore

它真的很直白。基本上它只需要1个文件,您需要将其集成到您的应用程序中。

答案 2 :(得分:8)

看看OI File Manager,这是一个开源的Android文件管理器。您可以获取源代码here

相关问题