Android Programming: Where To Start For Creating A Simple File Browser?
Asked Answered
T

3

30

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!

Torray answered 5/11, 2010 at 18:26 Comment(0)
C
31

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!

Cirque answered 5/11, 2010 at 18:51 Comment(4)
Oh by the way, I have a working file save and file read script that I've tested and it works, I wanted the file browser I'm creating to just return the Uri of the file you clicked on/directory you selected.Torray
In that case, just add a click handler that returns the corresponding File's path (e.g. sdDirectories[1].getAbsolutePath()).Cirque
Thank you so much, I'm going to start making it soon! If I run into trouble, would it be ok if I ask you for help, or would you rather not? :)Torray
Sure, doesn't bother me. I don't believe there's any sort of private messaging on here, of course, but if you want to just post a question, you're welcome to comment onto this with a link to it.Cirque
P
20

This is a late answer but I worked on creating an android file explorer recently. https://github.com/mburman/Android-File-Explore

Its really straightforward. Essentially its just 1 file that you would need to integrate into your application.

Projectile answered 19/7, 2011 at 23:26 Comment(2)
Thanks for the help but I already built on kcoppock's code and completed it a while back, but kudos though, looks like a nice app :)Torray
Awesome man. Saved the day. Just needed some improvements and it was all done.Encompass
A
8

Take a look at OI File Manager, which is an open-source Android file manager. You can get the source code here.

Autonomic answered 5/11, 2010 at 18:32 Comment(5)
I would actually recommend using OI File Manager, rather than copying it. In my apps, I just add a button that starts OI File Manager to select a file, and offers a download from the Market if it's not installed already.Swinger
That's what I've done for the moment with ES File Explorer, I'm interested in making my own really.Torray
How do you ensure that your users have OI File Manager installed though? Copying the source ensures it gets packaged up into your .apk... just broadcasting an intent is crossing your fingers and hoping.Phosphene
@VygintasB, fixed both links.Autonomic
@RyanBerger ThanksDislocation

© 2022 - 2024 — McMap. All rights reserved.