8

My application shows the list of songs in sdcard, and there is an option to delete the song from SD card.

Even when the song is deleted the song still comes in my applications list.

How can I update the android media database and show the updated database?

Michael Currie
  • 13,721
  • 9
  • 42
  • 58
abhishek
  • 1,434
  • 7
  • 39
  • 71

4 Answers4

21

Android has a cache of sorts that keeps track of media files.

Try this:

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

It makes the MediaScanner service run again, which should remove the deleted song from the device's cache.

You also need to add this permission to your AndroidManifest.xml:

<intent-filter>
  <action android:name="android.intent.action.MEDIA_MOUNTED" />
  <data android:scheme="file" /> 
</intent-filter>
Marc Bernstein
  • 11,423
  • 5
  • 34
  • 32
  • thanks for reply but now i am getting a blank screen and log cat shows 03-10 02:24:08.222: ERROR/MediaScannerService(401): Failed to delete file /data/data/com.android.providers.media/pause_scan i think the code never reaches the onPostExecute of async task when i place your code in doInBackground – abhishek Mar 09 '11 at 20:56
  • Ok the blank screen may be because my connection type was Disk Drive. But still the problem pertains i am getting the list of the songs and it is not updated – abhishek Mar 09 '11 at 21:24
  • Okk i got it... i couldnt have made this thing more ugly. while doing my stuff i commented the sendBroadcast stuff :) but still i have to run application again to see the effects – abhishek Mar 09 '11 at 21:31
  • the activity must be finish and then start again in order to show the updated list – abhishek Mar 09 '11 at 21:59
  • Where should needs to add intent-filter action in Activity Class in manifest. Because I did not added any receiver ? – Sunil Kumar Mar 16 '15 at 08:07
4

For Android before ICS, send a ACTION_MEDIA_MOUNTED broadcast :

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

For ICS and Jelly Bean, you can use the MediaScannerConnection API to scan media files

Bao Le
  • 16,643
  • 9
  • 65
  • 68
4

Gallery refresh including Android KITKAT

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File("file://"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
}
else
{
       sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
} 
AtanuCSE
  • 8,832
  • 14
  • 74
  • 112
1

The below was tested only on emulators. Solution works on 2.2 and 2.3, but not on 4. On 4th Android emulator this action does nothing with message in LogCat:

Permission Denial: broadcasting Intent

Unfortunately did not found the way to update media storage for Android 4 Did not test on Android 3.

The error happens also in the 2.3.3 Intel image. Have not check on real devices.

Alexey Vassiliev
  • 2,349
  • 2
  • 17
  • 8