2012-08-17 164 views
5

我正在製作一個mp3播放器應用程序,播放內部SD卡內任何地方的.mp3文件。如何從android內部和外部SD卡播放mp3文件?

我已經使用以下代碼來獲取存在於內部存儲中的.mp3文件。

ArrayList<File> inFiles = new ArrayList<File>(); 
File list[] = file.listFiles(); 
//Log.i("DIR", "PATH" +file.getPath()); 
for (int i = 0; i < list.length; i++) 
{ 
    // myList.add(list[i].getName()); 
    File temp_file = new File(file.getAbsolutePath(),list[i].getName()); 
    //Log.i("DIR", "PATH" +temp_file.getAbsolutePath()); 
    if (temp_file.listFiles() != null) 
    { 
     //Log.i("inside", "call fn"); 
     listfiles(temp_file); 

    } 
    else 
    { 
     if (list[i].getName().toLowerCase().contains(".mp3")) 
     { 
      inFiles.add(list[i]); 
     //Log.e("Music", list[i].getName()); 
     } 
    } 
} 

我該如何獲得外部SD卡的.mp3文件?

回答

1

嘗試使用這樣的:

File root = Environment.getExternalStorageDirectory(); 

,將讓你的外部存儲的根,只要有一個。然後,您可以過濾掉不.MP3

而且文件考慮尋找這個:List all of one file type on Android device?

+1

感謝您的回覆! 「Environment.getExternalStorageDirectory();」 獲取內部存儲的路徑,但我想從外部移動的SD卡中獲取.mp3文件。 – Aadi 2012-08-18 04:00:43

1

要到位於文件的引用:SD卡/音樂/ Song.mp3的:

File file = Environment.getExternalStorageDirectory()+"/music/song.mp3"; 
2

可以使用線的驗證碼

File root = Environment.getExternalStorageDirectory(); 

現在,你內部

0123你可以做同樣的事情得到了外部SD卡的根目錄
ArrayList<File> inFiles = new ArrayList<File>(); 
File list[] = root.listFiles(); // here use the root object of File class to the list of files and directory from the external storage 
//Log.i("DIR", "PATH" +file.getPath()); 
for (int i = 0; i < list.length; i++) 
{ 
    // myList.add(list[i].getName()); 
    File temp_file = new File(file.getAbsolutePath(),list[i].getName()); 
    //Log.i("DIR", "PATH" +temp_file.getAbsolutePath()); 
    if (temp_file.listFiles() != null) 
    { 
     //Log.i("inside", "call fn"); 
     listfiles(temp_file); 

    } 
    else 
    { 
     if (list[i].getName().toLowerCase().contains(".mp3")) 
     { 
      inFiles.add(list[i]); 
     //Log.e("Music", list[i].getName()); 
     } 
    } 
} 
+0

感謝您的回覆! 「Environment.getExternalStorageDirectory();」 獲取內部存儲的路徑,但我想從外部移動的SD卡中獲取.mp3文件。 – Aadi 2012-08-18 04:02:03

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

    enter code here 

import android.os.Environment; 

public class SongsManager { 


    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 from sdcard 
    * 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); 
      } 
     } 
    /** 
    * 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

應該說明解決方案,而不是隻登頂代碼 – 2015-02-10 04:57:18

+0

以上代碼是爲了......如何在Android中從外部SD卡播放mp3文件? – 2015-03-23 06:32:30

0

少許修改圖沙·古普塔的代碼。 這對我來說很好。我有一個音樂播放器的完整代碼

import android.os.Environment; 
import java.io.File; 
import java.io.FilenameFilter; 
import java.util.ArrayList; 
import java.util.HashMap; 
public class SongsManager { 


     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 from sdcard 
     * 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); 
      } 
     } 
     /** 
     * 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")); 
      } 
     } 
    }