Android Get all Song from SDCard

Hello friends today i am shearing  a new  post for get all song from sdcard with the help of Cursor and if you have any query so please comments me for this post


Now this is best example of get all song from sdcards

Note :- add uses permission on your manifest.xml file  
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Create XML file
<RelativeLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"                                                               android:layout_width="match_parent"
      android:layout_height="match_parent"                                                                             android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"                                           tools:context=".MainActivity">
<ListView
         android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

Create a class for use this all code step by step
package mail.example.com.songlist;
import android.app.Activity;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.File;
import java.io.IOException;
public class MainActivity extends Activity {
private MediaPlayer palyer;
private String[] songList;
@Override
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
         palyer = new MediaPlayer();
         ListView mListView = (ListView) findViewById(R.id.listView1);
         songList = getAllSong();
         ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,
             android.R.layout.simple_list_item_1, songList);
       mListView.setAdapter(mAdapter);
       mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long           id) {
              try {
                     playSong(songList[position]);
                     } catch (IllegalArgumentException e) {
                           e.printStackTrace();
                    } catch (IllegalStateException e) {
                           e.printStackTrace();
                   } catch (IOException e) {
                          e.printStackTrace();
                    }
                 }
           });
    }
     private String[] getAllSong() {
          final Cursor mCursor = managedQuery(
           MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
           new String[] { MediaStore.Audio.Media.DISPLAY_NAME }, null, null,
           "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
           int count = mCursor.getCount();
           String[] songs = new String[count];
           int i = 0;
           if (mCursor.moveToFirst()) {
         do {
            songs[i] = mCursor.getString(0);
              i++;
          
            } while (mCursor.moveToNext());
           }
          mCursor.close();
         return songs;
        }

/*play music song after click on list item*/
private void playSong(String path) throws IllegalArgumentException,
IllegalStateException, IOException {
      String extStorageDirectory = Environment.getExternalStorageDirectory()
      .toString();
      path = extStorageDirectory + File.separator + path;
      palyer.reset();
      palyer.setDataSource(path);
      palyer.prepare();
      palyer.start();
 }
   @Override 
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
       return true;
   }