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 p开发者_开发技巧rogrammatically?
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
精彩评论