机器人编程:从哪里开始创建一个简单的文件浏览器?创建一个、机器人、浏览器、简单

2023-09-12 02:42:29 作者:ヌ酷还可爱ˇ

我想作一个文件浏览器,会做两件事情: 1)允许用户浏览和选择一个目录 2)允许用户浏览他们的SD卡中的所有文件

I would like to make a file browser that will do two things: 1) Allow the user to browse and select a directory 2) Allow the user to browse all files on their sdcard

我看的教程,但似乎无法找到任何? 是否有人可以帮助我,无论怎么解释我是什么code需要做才能有一个简单的文件浏览器或我提供一个链接到一个教程/源$ C ​​$ C?

I've looked for tutorials but can't seem to find any? Can someone please help me by either explaining how what my code would need to do in order to have a simple file browser or providing me with a link to a tutorial/source code?

请和谢谢!

推荐答案

如果你其实更感兴趣的是学习写你自己的,我建议你服用了良好的通过的Environment 类:

In the case of SD cards/other external storage for Android, you'll want to first check to ensure that the external storage is mounted and available before trying to read it, using the Environment class:

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
}

一旦你已经确定了外部存储的正常状态,一个简单的方式开始是使用文件的listFiles()方法,像这样:

Once you've determined the proper state of the external storage, a simple way to start is to use File's listFiles() method, like so:

//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来缩小搜索结果:

You can then start using FileFilters to narrow down your results:

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

从那里,只是通读文档,找到你想用它做的事情的类型,然后你可以绑到这些名单适配器等工作。

From there on, just read through the docs to find the type of thing you're looking to do with it, and then you can work on tying these into list adapters, etc.

希望这有助于!