0

I'm trying to make an application that finds all mp3 songs and this code is what I got, but I can't test this code because the is crashing and dont have any error, what i'm doing wrong?

My class to find the songs:

   public class SongsManager {
    private ArrayList<Song> songsList;

    public ArrayList<Song> getMp3Songs(Context ctx) {
        Uri allSongsUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
        Cursor cursor =  ctx.getContentResolver().query(allSongsUri, null, null, null, selection);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    Song song = new Song(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID)),
                            cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)),
                            cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)),
                            cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
                    songsList.add(song);
                } while (cursor.moveToNext());
            }
            cursor.close();
        }
        return songsList;
    }
}

My activity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SongsManager songsManager = new SongsManager();
        ArrayList<Song> songList = songsManager.getMp3Songs(this);
        ArrayAdapter<Song> adapterSongList = new ArrayAdapter<Song>(this, android.R.layout.simple_list_item_1, songList);
        ListView listView = (ListView) findViewById(R.id.musicList);
        listView.setAdapter(adapterSongList);
    }
}

My song class:

public class Song {
    private long id;
    private String title;
    private String artist;
    private String path;
        public Song(long songID, String songTitle, String songArtist, String songPath) {
        id = songID;
        title = songTitle;
        artist = songArtist;
        path = songPath;
    }
    public long getID(){
        return id;
    }
    public String getTitle(){
        return title;
    }
    public String getArtist(){
        return artist;
    }
    public String getPath(){
        return path;
    }
    public void setTitle(String title_param){
        title = title_param;
    }
    public void setArtist(String artist_param){
        title = artist_param;
    }
    public void setPath(String path_param){
        path = path_param;
    }
    public String toString(){
        return getTitle();
    }
}
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Felipe Custódio
  • 102
  • 4
  • 13
  • "because the is crashing" -- use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Mar 05 '17 at 22:43
  • some error here: Cursor cursor = ctx.getContentResolver().query(allSongsUri, null, null, null, selection); – Felipe Custódio Mar 05 '17 at 23:41

1 Answers1

0

You have applied the wrong syntax. The "selection" is incorrect as this is where the sort order goes.

for example:

         private final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
         public Cursor getAllTracks(Context context) {
    // gets all tracks

    ContentResolver cr = context.getContentResolver();
    final String[] columns = {track_id, track_no, artist, track_name,
            album, duration, path, year, composer};
    return cr.query(uri, columns, null, null, null);
} 

From the documentation:

    query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

Query the given URI, returning a Cursor over the result set.

Theo
  • 2,012
  • 1
  • 16
  • 29