How can I refresh MediaStore on Android?

AndroidMediastore

Android Problem Overview


This started out as a general user question on Android forums. However it's become, by necessity, a programming question. Here's my problem.

Android has a service - MediaScanner - which runs in the background any time (I believe) the SD card is un-mounted and re-mounted. This service collects data on all the media files on the card, and provides a SQLite DB which can be queried by music applications. Most music applications use this service as it saves on battery-drain associated with scanning the SD card.

Since I started using android, I've consistently had a problem whereby M3U playlists synchronised to the device remain in this SQLite DB even after being deleted from the SD Card. It's gotten to the point where I now have a collection of about 40 playlists showing up in any music app I use, despite there only being around 10 m3u files on the card. The remaining playlists do not play, and are empty. I can remove them manually by deleting them from the music app, but I'm sick of doing this. There has to be a better way to remove these ghost playlists.

There are two apps on the Android Market - SDRescan and Music Scanner, which supposedly do exactly this but neither of them work.

I set about writing my own app to refresh or delete the MediaStore database and start from scratch, but I'm not getting very far. I've got an android app which runs the following code :

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
        Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 

I've found a few examples of this code online as a way to scan the SD Card but I'm not having any luck with it whatsoever. Any tips?

FULL CODE:

package com.roryok.MediaRescan;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;

public class MediaRescan extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
                Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 
        setContentView(R.layout.main);
    }
    
    //Rescan the sdcard after copy the file
    private void rescanSdcard() throws Exception{     
      Intent scanIntent = new Intent(Intent.ACTION_MEDIA_MOUNTED, 
                Uri.parse("file://" + Environment.getExternalStorageDirectory()));   
      IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
      intentFilter.addDataScheme("file");     
      sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
                Uri.parse("file://" + Environment.getExternalStorageDirectory())));    
    }
}

Android Solutions


Solution 1 - Android

Here is an easy to use 'single file based' solution:

Adding a file:

Whenever you add a file, inform MediaStore's Content Provider using:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(newMediaFile)));

Deleting a file:

Similarly, when you delete a file, inform MediaStore's Content Provider using:

getContentResolver().delete(uri, null, null)  // (Credit goes to [DDSports][1])

Solution 2 - Android

Ok, I've done it.

Rather than rescan the card, the app iterates through all the playlists in mediastore and checks the length of the _data field. I discovered that for all the lists with no associated M3U file, this field was always empty. Then it was just a case of finding the source code for the original android music app, finding the delete method and using that to delete any playlists with a length of 0. I've renamed the app PlaylistPurge (since it doesn't 'rescan' anymore) and am posting the code below.

I'll probably also publish this somewhere, either on the Market or on my own site, http://roryok.com

package com.roryok.PlaylistPurge;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class PlaylistPurge extends ListActivity {
	
	private List<String> list = new ArrayList<String>();
	private final String [] STAR= {"*"};
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        ListAdapter adapter = createAdapter();
        setListAdapter(adapter);
    }
 
    /**
     * Creates and returns a list adapter for the current list activity
     * @return
     */
    protected ListAdapter createAdapter()
    {
    	// return play-lists
    	Uri playlist_uri= MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;    
	    Cursor cursor= managedQuery(playlist_uri, STAR, null,null,null);
	    cursor.moveToFirst();
	    for(int r= 0; r<cursor.getCount(); r++, cursor.moveToNext()){
	    	int i = cursor.getInt(0);
        	int l = cursor.getString(1).length();
        	if(l>0){
    			// keep any playlists with a valid data field, and let me know
        		list.add("Keeping : " + cursor.getString(2) + " : id(" + i + ")");
        	}else{
        		// delete any play-lists with a data length of '0'
        		Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, i);
        		getContentResolver().delete(uri, null, null);
        		list.add("Deleted : " + cursor.getString(2) + " : id(" + i + ")");
        	}
	    }	    
	    cursor.close();
    	// publish list of retained / deleted playlists
    	ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
 
    	return adapter;
    }
}

UPDATE:

Here's a link to a post on my blog about the app http://roryok.com/blog/index.php/2010/07/23/clearing-out-deleted-playlists-in-android/

UPDATE 2: Tuesday, April 9, 2013

I've gotten a lot of traffic to my blog from this post, and a huge number of emails from people thanking me for it. Glad it helped out! Any potential users should know that my crappy app currently crashes as soon as you run it, but actually does what it's supposed to do! I've always intended to go back and fix this crashing behaviour and put it on the market, hopefully 2013 will be the year I do that.

Solution 3 - Android

You can request a rescan of specific files using the following code.

NOTE: The MIME TYPE passed is important. I noticed changes made to MP3 ID3 tags did not refresh properly in the SQLite if I used "*/*", however using "audio/mp3" worked

MediaScannerConnection.scanFile(
    context, 
    new String[]{ pathToFile1, pathToFile2 }, 
    new String[]{ "audio/mp3", "*/*" }, 
    new MediaScannerConnectionClient()
    {
        public void onMediaScannerConnected()
        {
        }
        public void onScanCompleted(String path, Uri uri)
        {
        }
    });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionroryokView Question on Stackoverflow
Solution 1 - AndroidPascalView Answer on Stackoverflow
Solution 2 - AndroidroryokView Answer on Stackoverflow
Solution 3 - AndroidMichaelView Answer on Stackoverflow