5

我正在开发一个 mp3 播放器应用程序,它可以播放内部 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 文件?

4

5 回答 5

2

您可以使用以下代码获取外部 sdcard 的根目录

File root = Environment.getExternalStorageDirectory();

现在您可以执行与内部操作相同的操作

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());
        }
    }
}
于 2012-08-17T12:27:42.213 回答
1

尝试使用这个:

File root = Environment.getExternalStorageDirectory();

这将为您提供外部存储的根,前提是有一个。然后您可以过滤掉不是 .mp3 的文件

还可以考虑看看这个:List all of one file type on Android device?

于 2012-08-17T12:16:44.060 回答
1

要获取对位于以下位置的文件的引用:sdcard/music/song.mp3:

File file = Environment.getExternalStorageDirectory()+"/music/song.mp3";
于 2012-08-17T12:16:48.117 回答
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"));
        }
    }
}
于 2015-02-10T04:47:23.903 回答
0

对 Tushar Gupta 代码的轻微修改。这对我来说很好。我有完整的音乐播放器代码

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"));
            }
        }
    }
于 2017-01-25T15:13:40.290 回答