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

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

问题描述

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

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



请谢谢!

解决方案

如果您实际上更有兴趣学习自己写,我建议您通过文件类文档阅读好阅读。这就是你要做大部分工作的地方。



对于Android的SD卡/其他外部存储设备,您需要首先检查请确保在尝试阅读外部存储装置并可用之前,请使用环境 class:

  String extState = Environment.getExternalStorageState(); 
//你可能还想添加(... || Environment.MEDIA_MOUNTED_READ_ONLY)
//如果你只想读取文件系统
if(!extState.equals(Environment) MEDIA_MOUNTED)){
// handle error here
}
else {
//你的文件在这里工作
}

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

  //文档中还有getRootDirectory(),getDataDirectory()等)
File sd = Environment。 getExternalStorageDirectory();
//这将返回一个包含外部存储文件夹中的所有文件(目录和文件)
//的数组
File [] sdDirList = sd.listFiles();

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

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

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



希望这有帮助!


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?

Please and thanks!

解决方案

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.

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
}

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();

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.

Hope this helps!

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

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