4

I need to acces the sdcard and return some files of different formats. The location will be input by the user. How can I do this programmatically?

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62

1 Answers1

6

Simondid, I believe this is what you are looking for.

Accessing the SDCard: reading a specific file from sdcard in android

Keep in mind checking the media availability: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

Creating a file filter: http://www.devdaily.com/blog/post/java/how-implement-java-filefilter-list-files-directory

Example of a mp3 file filter, Create the following filter class:

import java.io.*;

/**
 * A class that implements the Java FileFilter interface.
 * It will filter and grab only mp3
 */
public class Mp3FileFilter implements FileFilter
{
  private final String[] okFileExtensions = 
    new String[] {"mp3"};

  public boolean accept(File file)
  {
    for (String extension : okFileExtensions)
    {
      if (file.getName().toLowerCase().endsWith(extension))
      {
        return true;
      }
    }
    return false;
  }
}

Then based on the earlier post of accessing the sdcard you would use the filter like this:

File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard, "path/to/the/directory/with/mp3");

//THIS IS YOUR LIST OF MP3's
File[] mp3List = dir.listFiles(new Mp3FileFilter());

NOTE: The code is rough you probably want to make sure the sdcard is available as mentioned above

Community
  • 1
  • 1
EpicOfChaos
  • 822
  • 11
  • 23
  • ty for the awsome answere but im having a hard time getting the filter to work i need it to test to media / music files just mp3 for now pleas help –  Aug 30 '11 at 15:35
  • i need the filter to return all mp3 files from a given folder –  Aug 30 '11 at 18:05
  • simon, you can check for the filetype by using `String.contains(".mp3");` on the filepath of the given directory/file. – Codeman Aug 30 '11 at 19:36
  • pheonixblade ty for the tip but i solved it this why FileFilter filterDirectoriesOnly = new FileFilter() { public boolean accept(File file) { return file.getName().endsWith("mp3"); } }; Log.i("state", "filter don"); File[] sdDirectories = yourFile.listFiles(filterDirectoriesOnly); –  Aug 30 '11 at 20:22