Ale*_*eAP 30 browser android file
我想提出一个文件浏览器,会做两件事情:1)允许用户浏览和选择一个目录2)允许用户浏览他们的SD卡中的所有文件
我找了教程,但似乎找不到?有人可以通过解释我的代码需要做什么才能拥有一个简单的文件浏览器或者为我提供教程/源代码的链接来帮助我吗?
拜托,谢谢!
kco*_*ock 31
如果您真的对学习编写自己的内容更感兴趣,我建议您仔细阅读File类文档.那就是你要完成大部分工作的地方.
对于Android的SD卡/其他外部存储设备,您需要首先检查以确保在尝试读取外部存储设备之前使用环境类:
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
}
Run Code Online (Sandbox Code Playgroud)
一旦确定了外部存储的正确状态,一个简单的启动方法是使用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();
Run Code Online (Sandbox Code Playgroud)
然后,您可以开始使用FileFilters来缩小搜索范围:
FileFilter filterDirectoriesOnly = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
File[] sdDirectories = sd.listFiles(filterDirectoriesOnly);
Run Code Online (Sandbox Code Playgroud)
从那里开始,只需阅读文档,找到你想要用它做的事情的类型,然后你就可以将它们绑定到列表适配器等等.
希望这可以帮助!
Man*_*man 20
这是一个迟到的答案,但我最近创建了一个Android文件浏览器.https://github.com/mburman/Android-File-Explore
它真的很直白.基本上它只需要1个文件,您需要将其集成到您的应用程序中.