Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to prevent my music player app to scan the directories for audio files everytime the app launches. How can I do that?

I want to prevent my music player app from scanning the directories for audio files everytime the app launches. How can I do that?

I have been using the following code to scan the audio files.

public void getSongList() {

  ContentResolver contentResolver=getContentResolver();
  Uri musicUri=android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  Cursor musicCursor = contentResolver.query(musicUri, null, null, null, null);
  if(musicCursor!=null && musicCursor.moveToFirst()) {
    //get columns
    int titleColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.TITLE);
    int idColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media._ID);
    int artistColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.ARTIST);

    int albumIDColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);

    //add songs to list
    do {
       long thisId = musicCursor.getLong(idColumn);
       String thisTitle = musicCursor.getString(titleColumn);
       String thisArtist = musicCursor.getString(artistColumn);
       long thisAlbumID=musicCursor.getLong(albumIDColumn);
       Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
       Bitmap albumArtBitMap=null;
       Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, thisAlbumID);
       try {
         albumArtBitMap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), albumArtUri);
         Matrix m = new Matrix();
         m.setRectToRect(new RectF(0, 0, albumArtBitMap.getWidth(), albumArtBitMap.getHeight()), new RectF(0, 0, 300, 300), Matrix.ScaleToFit.CENTER);
         albumArtBitMap = Bitmap.createBitmap(albumArtBitMap, 0, 0, albumArtBitMap.getWidth(), albumArtBitMap.getHeight(), m, true);
       } catch (IOException e) {
         e.printStackTrace();
       }
       songList.add(new Song(thisId, thisTitle, thisArtist,albumArtBitMap));
    }
    while (musicCursor.moveToNext());
  }
}

I want the app to only scan when there are new files. Because If I scan the whole SD card every time then it'll take too much time for starting the app. Please Help me with that

like image 317
Purvil Bambharolia Avatar asked Oct 15 '16 04:10

Purvil Bambharolia


2 Answers

No need to keep all the songs in local app list.

To show the mp3list you can use content provider cursor list adapter query with limit (on scroll query page by page )

To search use directly the contentprovider query method.

Only keep a playslist on you local database pointing to mp3 uri.

this link might helps you : How to update listview whose data was queried from database through SimpleCursorAdapter?

like image 188
Rajesh Gopu Avatar answered Oct 21 '22 01:10

Rajesh Gopu


Every time you start the app see how much space you have on your device.

File path = Environment.getDataDirectory();
megaBytesAvailable(path)

public static float megaBytesAvailable(File file) {
        StatFs stat = new StatFs(file.getPath());
        long bytesAvailable = (long)stat.getBlockSizeLong() * (long)stat.getAvailableBlocksLong();
        return bytesAvailable / (1024.f * 1024.f);
    }

Save it to your app's cache as a variable and compare it every time you start the app, if it's greater then you know you need to scan.

if(comparedVariable < megaBytesAvailable(music_directory_path)){
    getSongList();
    //Save it again to compare next time if more storage is used
    comparedVariable = megaBytesAvailable(music_directory_path);
    //Save it to SharedPrefs for next boot up comparison
}
like image 43
Stephen Avatar answered Oct 21 '22 02:10

Stephen