在Android中抛出空指针异常

时间:2013-11-16 19:00:46

标签: java android audio arraylist audio-player android-music-player

我正在尝试从音频目录中获取音频文件,因为我正在使用下面的代码。我无法弄清楚为什么这种情况if (home.listFiles(new FileExtensionFilter()).length > 0)会引发错误。代码如下。

Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
final String MEDIA_PATH = allsongsuri.toString();
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

public ArrayList<HashMap<String, String>> getPlayList(){
    System.out.println(" -- "+MEDIA_PATH);
    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 songsList;
}

class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
    return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}

我需要使用hashMap将音频文件存储在ArrayList中。我试过这个例子来存储List中的文件     http://android-er.blogspot.in/2012/02/list-audio-media-in-mediastoreaudiomedi.html。使用这个例子,我尝试了很多将列表存储在ArrayList中,因为我需要它。但没有成功。     我需要帮助。我必须将音频文件存储在Arraylist中。

6 个答案:

答案 0 :(得分:0)

必须home.listFiles返回null

public File[] listFiles (FileFilter filter)上的Android developer reference

  

获取此文件表示的目录中的文件列表。然后通过FileFilter过滤此列表,并将匹配文件作为文件数组返回。 如果此文件不是目录,则返回null。如果filter为null,则所有文件都匹配。

因此,您需要确保MEDIA_PATH是正确的目录。

答案 1 :(得分:0)

如果找不到文件,“listFiles()”方法可以返回null。我建议先检查一下它是否为空,只有当它不是null时才会继续。例如:

public ArrayList<HashMap<String, String>> getPlayList(){
    System.out.println(" -- "+MEDIA_PATH);
    File home = new File(MEDIA_PATH);

    if (home.listFiles(new FileExtensionFilter()) != null){
        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 songsList;
}

你可能也想放入一个else语句,以确保返回时songsList不为null。

答案 2 :(得分:0)

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 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);
        }
    }


}

答案 3 :(得分:0)

参考Anup和Destrom的答案,我只想把代码放在文件名过滤器

    public class SongsManager {
    // SDCard Path
//  final String MEDIA_PATH = new String("/sdcard/sound_recorder"); 

    final String MEDIA_PATH =Environment.getExternalStorageDirectory()+"/sound_recorder";
    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(){

        Log.d("testsd",MEDIA_PATH);
        File home = new File(MEDIA_PATH);

    //  if (home.listFiles(new FileExtensionFilter()).length > 0) //don't use this to avoid null pointer exception !
        if (home.listFiles(new FileExtensionFilter())!=null) {
            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") || name.endsWith(".3gpp") || name.endsWith(".wav")); //add more extensions here
        }
    }
}

答案 4 :(得分:0)

感谢lote .....在没有sdcard的情况下使用moto g进行填充.....它模拟了物理SD卡....仍然无法正常工作。

但是你的代码与另一个拥有物理sdcard的motorola完美配合

我的意思是当sdcard模拟dontwork ..i尝试所有MEDIA_PATHS可能

我需要你的建议......谢谢。 // final String MEDIA_PATH = Environment.getExternalStorageDirectory()。getPath()+“/”;     // private final String MEDIA_PATH = new String(“/ sdcard / res / raw /”);     // final String MEDIA_PATH = Environment.getExternalStorageDirectory()。getAbsolutePath()+“/”;     // final String MEDIA_PATH = new String(“/ sdcard /”);     // final String MEDIA_PATH = Environment.getExternalStorageDirectory()。getPath()+“/”; // final String MEDIA_PATH =“”;     // final String MEDIA_PATH = new String(“/ sdcard / musica /”);     // final String MEDIA_PATH = Environment.getExternalStorageDirectory()。getPath()+“/”;     // final String MEDIAPATH = Environment.getExternalStorageDirectory()。getAbsolutePath()+“/ Musica /”;     // final String MEDIAPATH = new String(“/ sdcard /”);     // final String MEDIA_PATH = Environment.getExternalStorageDirectory()。getPath()+“/”;     final String MEDIA_PATH = Environment.getExternalStorageDirectory()。getAbsolutePath()+“/”;

答案 5 :(得分:0)

确保您不会忘记manifest.xml中的使用权限READ EXTERNAL STORAGE