title: Getting audio files from storage
body: <p>I am trying to get all the audio files from device both internal and external storage .so i am using mediastorage and cursor .But I am not able to get any proper example of song retrieval. </p>
  
  <p>I used this code:</p>
  
  <pre><code>public class MainActivity extends ListActivity {
  Cursor cursor;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      String[] columns = { MediaStore.Audio.Media._ID };
      int[] displayViews = new int[] {android.R.id.text1};
      ContentResolver cr = this.getContentResolver();
      cursor = cr.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,columns, null, null, null);
      setListAdapter(new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,cursor,columns,displayViews,1));
  }
  
  public static ArrayList&lt;SongDto&gt; getMusicInfos(Context context) {
  
      ArrayList&lt;SongDto&gt; musicInfos = new ArrayList&lt;SongDto&gt;();
  
      Cursor cursor = context.getContentResolver().query(
              MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
              MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
      if (cursor == null) {
          return null;
      }
  
  
      for (int i = 0; i &lt; cursor.getCount(); i++) {
          cursor.moveToNext();
  
  
          int isMusic = cursor.getInt(cursor
                  .getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));
  
          if (isMusic != 0) {
              SongDto music = new SongDto();
  
  
              music.path = cursor.getString(cursor
                      .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
  
              if (!new File(music.path).exists()) {
                  continue;
              }
  
  
              music.songId = cursor.getLong(cursor
                      .getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
  
              music.songTitle = cursor.getString(cursor
                      .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
  
  
              music.songTitle = cursor.getString(cursor
                      .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
  
  
              music.album = cursor.getString(cursor
                      .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
  
  
              music.songArtist = cursor.getString(cursor
                      .getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
  
              music.duration = cursor
                      .getLong(cursor
                              .getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
  
              MediaMetadataRetriever mmr = new MediaMetadataRetriever();
              mmr.setDataSource(music.path);
              mmr.release();
  
              musicInfos.add(music);
          }
      }
  
      return musicInfos;
  }
  }
  </code></pre>
  
  <p>and  </p>
  
  <pre><code>public class SongDto {
  
  public long songId;
  public String songTitle;
  public String songArtist;
  public String path;
  public short genre;
  public long duration;
  public String album;
  public Bitmap albumArt;
  
  public String toString() {
      return String.format("songId: %d, Title: %s, Artist: %s, Path: %s, Genere: %d, Duration %s",
              songId, songTitle, songArtist, path, genre, duration);
  }
  }
  </code></pre>
  
  <p>and i am getting a run time error I fixed the SimpleCursorAdapter as it is deprecated how to get the song and what wrong I am doing in setting the adapter,any example or hint will be helpful.</p>