Android music player not reindexing after song addition

android, java

Solution

You need to notify the `MediaScanner` that a new file has been added and should be indexed so it shows up in the `MediaStore`.

The simplest way is to send a broadcast once the file has been downloaded:

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);

http://developer.android.com/reference/android/content/Intent.html#ACTION_MEDIA_SCANNER_SCAN_FILE

The assumes the "other music players" use the MediaStore, but it's a good bet that they do.

Problem

I've been trying to create a simple MP3 downloader for Android. All it does is connect to the URL, downloads the track and guesses the ID3 information based on the name. The problem with my application is that once a song is downloaded, it does not instantly show up in the Google Music app, nor in the default music player application. I even tried some other ones without luck, they all do not reindex it. The only thing I noticed was that when I copied/moved the song from the Music folder on my SD card to the root of the SD card, it reindexed the entire library properly. This gave me the idea that it might not be alerting any file listeners. My question is: how would I be able to save a song to the Music folder, and having the music players update their indices? Thank you in advance.

Original source