javaandroidmediaandroid-mediascanner

Android Media Scan takes too long


I'm using the code below to scan all media/mp3 files on the device, but it takes too long when there is a lot of music on the device.

I was wondering if there is a faster way to get and show all musics or not?

Note: the code doesn't cause crash or hang up or any other thing...

class GetLocals extends AsyncTask<Void,Void,Void> {

  @Override
  protected void onPreExecute() {
    super.onPreExecute();
    SongCover_lc.clear();
    SongID_lc.clear();
    Singer_lc.clear();
    Path_lc.clear();
    AlbumName_lc.clear();
    SongName_lc.clear(); 
  }

  @Override
  protected Void doInBackground(Void... voids) {

    try {
      String[] STAR = {"*"};
      Cursor cursor;
      Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
      String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

      cursor = context.getContentResolver().query(uri, STAR, selection, null, null);

      if (cursor != null) {
        if (cursor.moveToFirst()) {
          do {
            String songName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            String albumName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
            String singer = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
            String songid = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

            MediaMetadataRetriever mmr = new MediaMetadataRetriever();
            mmr.setDataSource(path);
            byte[] artBytes = mmr.getEmbeddedPicture();
            if (artBytes != null) {
              InputStream is = new ByteArrayInputStream(mmr.getEmbeddedPicture());
              Bitmap bm = BitmapFactory.decodeStream(is);
              SongCover_lc.add(bm);
            } else {
              SongCover_lc.add(BitmapFactory.decodeResource(getResources(), R.color.white));
            }

            SongName_lc.add(songName);
            AlbumName_lc.add(albumName);
            Path_lc.add(path);
            Singer_lc.add(singer);
            SongID_lc.add(songid);

          } while (cursor.moveToNext());
        }
      }
    } catch (Exception e){
      System.out.println("CAAAAAAAAAAATCH");
    }
    return null;
  }

  @Override
  protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    lazyAdapter_localSongs = new LazyAdapter_LocalSongs(activity,SongName_lc,Singer_lc,SongCover_lc,AlbumName_lc,Path_lc);
    listviewloclas.setAdapter(lazyAdapter_localSongs);
  }
}

Solution

  • > I was wondering if there is a faster way to get 
    > and show all musics ...
    

    Replace your array-based adapter with a CursorAdapter that will load the songs on demand.

    So instead of pre-loading 1000 songs into arrays before your listview/grid becomes usable you just load the 10 songs that are visible at a time.