I'm developing a music player. I want to get all .mp3 format files from android device.
But this code is not getting any files. My .mp3 files are in 'sdcard/music' folder. If i change the MEDIA_PATH = new String("sdcard/music");
like this it's getting files only from that music folder. But i need to get all .mp3 files from each and every folder in my external/internal memorycard. Please help me.
this is my code.
final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory());
public void Generate_Database(){
File home = new File(MEDIA_PATH);
if (home.listFiles(new FileExtensionFilter()).length > 0) {
for (File file : home.listFiles(new FileExtensionFilter())) {
String title = file.getName().substring(0, (file.getName().length() - 4));
String path = file.getPath();
mediaInfo.setDataSource(path);
String albumName = "unknown",artist = "unknown",genere = "unknown",duration = "unknown",composer = "unknown";
if(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM) == null)
albumName = "unknown";
else{
albumName = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
}
if(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST) == null)
artist = "unknown";
else{
artist = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
}
if(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE) == null)
genere = "unknown";
else{
genere = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
}
if(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION) == null)
duration = "unknown";
else{
duration = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
}
if(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPOSER) == null)
composer = "unknown";
else{
composer = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPOSER);
}
//Toast.makeText(getApplicationContext(), title+path+ albumName+artist+ genere+ duration+ composer, Toast.LENGTH_LONG).show();
ds.createEntry2(title, path, albumName, artist, genere, duration, composer);
}
}
this is used to extract the .mp3 files
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}