Retrieving all the mp3 ,m4a and ogg files from SD Card in Android

Posted By : Deepanshu Harbola | 30-Sep-2015

There is always a need of audio files(.mp3, .ogg, .m4a, etc..) when you are developing a Music app or developing a Music Editing tool in android.

In this blog post we are going to know - How  to retrieve all the audio files from SD-Card in android?

The basic concept behind this is-

  1. Retrieve all the files from SD-Card.
  2. Filter audio files from retrieving data.
  3. Save audio files in a list.
  4. Display listed data.

1- Retrieve all the files from SD_Card.

Here I use chkDir(File file) method to retrieve files.

 public void chkDir(File file) {

        File[] listFile = file.listFiles();

        if (listFile != null) {
            for (File aListFile : listFile) {

 

Above line of code will retrieve all the files and sub files or folders, so we also have to check all the sub folders to retrieve the data from those. For that we will recurcively call the chkDir(File file) method.

 if (aListFile.isDirectory()) {
     chkDir(aListFile);
    }

 

2- Filter audio files from retrieving data-

Ok so we have all the files, now do our work filter all the audio files only from all the retrived files.

 if (aListFile.getName().endsWith(".mp3") || aListFile.getName().endsWith(".ogg")
                      || aListFile.getName().endsWith(".m4a")) // Can include more strings for more extensions.
          {

 

3-  Save audio files in a list-

To save the audio file in a list we have to create an refrence of List class and add all the audio files on it.

 myList.add(aListFile.getName());

 

4- Display listed data-

To display all the listed data we have to create ListView in layout and create an adapter to put all the listed values in it.

 myList = new ArrayList<>();
 songList = (ListView) findViewById(R.id.songsView);
 chkDir(Environment.getExternalStorageDirectory());
 ArrayAdapter<string> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,android.R.id.text1,myList);
 songList.setAdapter(adapter);

 

Here is the final code-

 public class MainActivity extends AppCompatActivity {
    ArrayList<string> myList;
    ListView songList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myList = new ArrayList<>();
        songList = (ListView) findViewById(R.id.songsView);
        chkDir(Environment.getExternalStorageDirectory());
        ArrayAdapter<string> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,android.R.id.text1,myList);
        songList.setAdapter(adapter);

    }

    public void chkDir(File file) {

        File[] listFile = file.listFiles();

        if (listFile != null) {
            for (File aListFile : listFile) {

                if (aListFile.isDirectory()) {
                    chkDir(aListFile);
                } else {
                    if (aListFile.getName().endsWith(".mp3") || aListFile.getName().endsWith(".ogg")
                            || aListFile.getName().endsWith(".m4a")) // Can include more strings for more extensions and check it.
                    {
                        myList.add(aListFile.getName());
                    }
                }
            }
        }
    }

}

Hope this post will help you..!!

Thanks.

About Author

Author Image
Deepanshu Harbola

Deepanshu is an Android application developer with experience in Titanium Framework . Deepanshu likes to play basketball ,volleyball and listens to music in his free time.

Request for Proposal

Name is required

Comment is required

Sending message..