Android 编程:从哪里开始创建一个简单的文件浏览器? [英] Android Programming: Where To Start For Creating A Simple File Browser?

查看:32
本文介绍了Android 编程:从哪里开始创建一个简单的文件浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

请和谢谢!

推荐答案

如果您实际上对学习编写自己的程序更感兴趣,我建议您仔细阅读 File 类文档.您将在那里完成大部分工作.

If you're actually more interested in learning to write your own, I'd suggest taking a good long read through the File class documentation. That's where you're going to be doing most of the work.

对于 Android 的 SD 卡/其他外部存储,您需要先检查以确保外部存储已安装且可用,然后再尝试读取它,使用 环境类:

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
}

一旦您确定了外部存储的正确状态,一个简单的开始方法是使用 File 的 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.

希望这有帮助!

这篇关于Android 编程:从哪里开始创建一个简单的文件浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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