Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update the Android media database

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?

like image 320
abhishek Avatar asked Mar 09 '11 18:03

abhishek


4 Answers

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>
like image 141
Marc Bernstein Avatar answered Oct 06 '22 00:10

Marc Bernstein


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

like image 35
Bao Le Avatar answered Oct 06 '22 01:10

Bao Le


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())));
} 
like image 29
AtanuCSE Avatar answered Oct 06 '22 01:10

AtanuCSE


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.

like image 43
Alexey Vassiliev Avatar answered Oct 05 '22 23:10

Alexey Vassiliev