Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 939501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:48:04+00:00 2026-05-15T21:48:04+00:00

This started out as a general user question on Android forums. However it’s become,

  • 0

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())));    
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:48:05+00:00Added an answer on May 15, 2026 at 9:48 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 511k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You don't need window.open(). It's plain ugly and prone to… May 16, 2026 at 5:23 pm
  • Editorial Team
    Editorial Team added an answer Have you got full logging turned on? If so -… May 16, 2026 at 5:23 pm
  • Editorial Team
    Editorial Team added an answer Your method accepts a ListView and your object is of… May 16, 2026 at 5:23 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

This is a very general question: I was wondering whether it is possible to
The view below is a container for three user controls, and I started getting
I have started working on what I expect to become, by far, the largest
I've just started out trying MVC 2 and Ajax, and I'm wondering if I'm
all, I started out with what i thought was going to be a pretty
We've started to separate out our single VS solution into multiple solutions for better
I'm planning a programming project, but am an unexperienced programmer. Aside from a couple
I've got this 1 minute long movie that I want to compile into an
Since yesterday I started encountering errors related to date formats in SQL Server 2008.
I'm not sure how long they've been doing it but I just noticed google

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.