Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 836
AlexPriceAP Avatar asked Nov 05 '10 18:11

AlexPriceAP


People also ask

What is a file browser on Android?

The Device File Explorer allows you to view, copy, and delete files on an Android device. This is useful when examining files that are created by your app or if you want to transfer files to and from a device.

How do I open file manager in Android programmatically?

Intent intent = new Intent(Intent. ACTION_GET_CONTENT); intent. setType("*/*"); Intent i = Intent. createChooser(intent, "View Default File Manager"); startActivityForResult(i, CHOOSE_FILE_REQUESTCODE);

How do I open a browser file on Android?

Simply launch the Chrome browser app on your phone or tablet. This opens all the contents of your SD storage on the Chrome browser app. Simply tap on any folder in order to navigate to its content. Once you identify the file that you want to open, tap on it to launch.


2 Answers

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!

like image 92
Kevin Coppock Avatar answered Sep 23 '22 20:09

Kevin Coppock


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.

like image 31
Manish Burman Avatar answered Sep 21 '22 20:09

Manish Burman