n~droid generation

Taking ndroid for a ride…

SdFolderStructure – Getting folder structure from android sdCard device

Getting folder structure from android sdCard device.


Your application uses the device’s sdCard to read/write to files.

You wish to make sure your application deploys well on different devices.


In order to get the absolute path to a device’s sdCard, we must find a path that contains the LOST.DIR,

Since it is almost the only way I know of, to verify we have the sdCard path in different devices.


I will demonstrate this in the following class, and get the full folders list from the sdCard.

This is to complete another post Extend EditTextPreference to AutoComplete – n AutoCompletePreference

Here how:

This class is called SdFolderStructure:

For this task we will:

  • Declare File, File Array and a FileFilter.
  • Declare final String LOSTDIR, sdABSPath.
  • Declare SharedPreferences preferences.
  • Check if path has been defined by user in SharedPreferences.
  • If not, get a folders list from our sdCard.
  • But before that we will find the absolute path to the device’s sdCard.

So you class should be defined like this:

The constructor:

  1. Will check if path exists in SharedPreferences.
  2. If NOT, it will get the list of folders from absolute sdCard Path.

The next method get the folders from the sdCard absolute Path through a filter that verifies it is a folder and not anything else:

Note the commented line that is strikethrough…

Alot of programmers use this but it can/ may return an error!

Now comes the methos that takes sdCard absolute path & makes sure it is the correct for different android versions:

The next methos cleans up absolute path and returns a single word folder name:

And the last that is used by getLastFolderInPath  for number of found directories in sdCard:


Hope you find this usefull….

Please contribute either by Donating with PayPal or Flatter Me a Little


Full code can be found here: SdFolderStructure.java

Development & Design: http://nwg.co.il
  • Google+
  • LinkedIn