techniqez techniqez - 4 months ago 21
Java Question

Throwing null pointer exception in Android

I'm trying to fetch an audio file from the audio directory, for that I'm using below code. I'm not able to figure out why this condition

if (home.listFiles(new FileExtensionFilter()).length > 0)
is throwing an error. Code is given below.

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


I need to store audio file in ArrayList using hashMap. I tried this example to store file in List
http://android-er.blogspot.in/2012/02/list-audio-media-in-mediastoreaudiomedi.html . Using this example I tried a lot to store the list in ArrayList as my reqirement need that. But didn't succeed.
I need help. I have to store Audio file in Arraylist.

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


}