2013-10-30 113 views
3

我想寫一個類,它從整個SD卡獲取所有的MP3文件。但實際上它只能將音頻文件直接放在SD卡上。所以它不搜索槽子文件夾。我想使用Mediastore,但我沒有把它列入數組列表。也許你能幫助我,非常感謝和抱歉beeing一個小白)vinzenz如何從SD卡獲取所有的音頻文件android

import java.io.File; 
import java.io.FilenameFilter; 
import java.util.ArrayList; 
import java.util.HashMap; 

import de.me.musicplayer.SongsManager.FileExtensionFilter; 

import android.database.Cursor; 
import android.net.Uri; 
import android.provider.MediaStore; 




public class SongsManager { 

final String MEDIA_PATH = new String("/sdcard/"); 
private ArrayList<HashMap<String, String>> songsList = new   ArrayList<HashMap<String, String>>(); 

// Constructor 
public SongsManager(){ 

} 

/** 
* Function to read all mp3 files from sdcard 
* and store the details in ArrayList 
* */ 
public ArrayList<HashMap<String, String>> getPlayList(){ 
    File home = new File(MEDIA_PATH); 

    if (home.listFiles(new FileExtensionFilter()).length > 0) { 
     for (File file : home.listFiles(new FileExtensionFilter())) { 
      HashMap<String, String> song = new HashMap<String, String>(); 
      song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4))); 
      song.put("songPath", file.getPath()); 

      // Adding each song to SongList 
      songsList.add(song); 
     } 
    } 
    // return songs list array 
    return songsList; 
} 

/** 
* Class to filter files which are having .mp3 extension 
* */ 
class FileExtensionFilter implements FilenameFilter { 
    public boolean accept(File dir, String name) { 
     return (name.endsWith(".mp3") || name.endsWith(".MP3")); 
    } 
} 
} 
+0

請參閱此鏈接.. http://stackoverflow.com/questions/10227895/android-songs-fetching-from-sd-card?rq=1 – Hariharan

回答

12
final String MEDIA_PATH = Environment.getExternalStorageDirectory() 
     .getPath() + "/"; 
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); 
private String mp3Pattern = ".mp3"; 

// Constructor 
public SongsManager() { 

} 

/** 
* Function to read all mp3 files and store the details in 
* ArrayList 
* */ 
public ArrayList<HashMap<String, String>> getPlayList() { 
    System.out.println(MEDIA_PATH); 
    if (MEDIA_PATH != null) { 
     File home = new File(MEDIA_PATH); 
     File[] listFiles = home.listFiles(); 
     if (listFiles != null && listFiles.length > 0) { 
      for (File file : listFiles) { 
       System.out.println(file.getAbsolutePath()); 
       if (file.isDirectory()) { 
        scanDirectory(file); 
       } else { 
        addSongToList(file); 
       } 
      } 
     } 
    } 
    // return songs list array 
    return songsList; 
} 

private void scanDirectory(File directory) { 
    if (directory != null) { 
     File[] listFiles = directory.listFiles(); 
     if (listFiles != null && listFiles.length > 0) { 
      for (File file : listFiles) { 
       if (file.isDirectory()) { 
        scanDirectory(file); 
       } else { 
        addSongToList(file); 
       } 

      } 
     } 
    } 
} 

private void addSongToList(File song) { 
    if (song.getName().endsWith(mp3Pattern)) { 
     HashMap<String, String> songMap = new HashMap<String, String>(); 
     songMap.put("songTitle", 
       song.getName().substring(0, (song.getName().length() - 4))); 
     songMap.put("songPath", song.getPath()); 

     // Adding each song to SongList 
     songsList.add(songMap); 
    } 
} 
+0

完美獲取散列表上列出的所有歌曲列表感謝buddy ..Njoy ...... !!!! –

+0

真棒代碼。簡短,寫得很好...(Y)+1 – hellodear

+0

真棒代碼,基於它的擴展名爲「.xlsx」來查找任何文件列表。 – Narendra

0
private void getallaudio() 
    { 
     String[] STAR = { "*" }; 
     controller.audioWrapper.clear(); 
     Cursor audioCursor = ((Activity) cntx).managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, STAR, null, null, null); 
     if (audioCursor != null) 
     { 
      if (audioCursor.moveToFirst()) 
      { 
       do 
       { 
        String path = audioCursor.getString(audioCursor.getColumnIndex(MediaStore.Audio.Media.DATA)); 
        audioWrapper.add(new MediaWrapper(new File(path).getName(), path, "Audio",false)); 
//     Log.i("Audio Path",path); 
       }while (audioCursor.moveToNext()); 

      } 
//   audioCursor.close(); 
     } 
    } 
1

此代碼會給你哈希地圖,內容的歌曲標題,以及歌曲的ArrayList絕對路徑

public ArrayList<HashMap<String, String>> getAudioList() { 

    ArrayList<HashMap<String, String>> mSongsList = new ArrayList<HashMap<String, String>>(); 
    Cursor mCursor = getContentResolver().query(
      MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
      new String[] { MediaStore.Audio.Media.DISPLAY_NAME, 
        MediaStore.Audio.Media.DATA }, null, null, null); 

    int count = mCursor.getCount(); 
    System.out.println("total no of songs are=" + count); 
    HashMap<String, String> songMap; 
    while (mCursor.moveToNext()) { 
     songMap = new HashMap<String, String>(); 
     songMap.put("songTitle",mCursor.getString(mCursor 
         .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME))); 
     songMap.put("songPath", mCursor.getString(mCursor 
       .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA))); 
     mSongsList.add(songMap); 
    } 
    mCursor.close(); 
    return mSongsList; 
} 
4

你也可以下載使用ContentResolver的類 我曾經做過這樣的,它的做工精細

public void getSongListFromStorage() { 
     //retrieve song info 
    ContentResolver musicResolver = getContentResolver(); 
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null); 

    if (musicCursor != null && musicCursor.moveToFirst()) { 
    //get columns 
    int titleColumn = musicCursor.getColumnIndex 
        (android.provider.MediaStore.Audio.Media.TITLE); 
    int idColumn = musicCursor.getColumnIndex 
        (android.provider.MediaStore.Audio.Media._ID); 
int artistColumn = musicCursor.getColumnIndex 
        (android.provider.MediaStore.Audio.Media.ARTIST); 
//add songs to list 
    do { 
     long thisId = musicCursor.getLong(idColumn); 
     String thisTitle = musicCursor.getString(titleColumn); 
     String thisArtist = musicCursor.getString(artistColumn); 
     songList.add(new Song(thisId, thisTitle, thisArtist)); 
     } while (musicCursor.moveToNext()); 
    } 
}