How to update Android Media Database

My application shows a list of songs on the SD card, and it is possible to delete a song from the SD card.

Even when the song is deleted, the song is still on my list of apps.

How can I update the Android multimedia database and show the updated database?

+7
source share
4 answers

Android has a cache that tracks media files.

Try the following:

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

This will start the MediaScanner service 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> 
+20
source

For Android before ICS, send an ACTION_MEDIA_MOUNTED broadcast:

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

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

+4
source

Gallery Updates, 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()))); } 
+4
source

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

Disclaimer: Broadcast Intent

Unfortunately, we could not find a way to update the media storage for Android 4. Not tested on Android 3.

The error also occurs in version 2.3.3 of Intel. Do not check real devices.

+1
source

All Articles