12

Is it possible to trigger the media scanner to update its DB via the command line?

I have an SSH server installed on my Android device and I copy files to/from over the same link. It would be great if I could also trigger a re-scan for the media scanner.

I am aware that there are applications on the market to do this via the UI but this is not what I want.

Any help fully appreciated.

12

The SSH server doesn't set all the necessary environment variables so it is easier to use a shell script.

#!/system/bin/sh

export ANDROID_ASSETS=/system/app
export ANDROID_CACHE=/cache
export ANDROID_DATA=/data
export ASEC_MOUNTPOINT=/mnt/asec
export ANDROID_ROOT=/system
export BOOTCLASSPATH=/system/framework/core.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar:/system/framework/filterfw.jar
export EXTERNAL_STORAGE=/mnt/sdcard
export LD_LIBRARY_PATH=/vendor/lib:/system/lib

am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///mnt/sdcard

When successful:

# rescan-media
Broadcasting: Intent { act=android.intent.action.MEDIA_MOUNTED dat=file:///mnt/sdcard }
Broadcast completed: result=0

This works on Cyanogenmod 9.1. Depending on your system the environment may be slightly different.

Note: In later versions of Cyanogenmod / Android the SD card may be mounted at /storage/sdcard0. Make sure you know where your card is mounted or this will never work. Diskinfo may help here.

Thanks to Dan Hulme for pushing me in the right direction.

References:

http://developer.android.com/reference/android/content/Intent.html
http://forum.xda-developers.com/showthread.php?t=1752749
http://intwoplacesatonce.com/
http://kennystechtalk.blogspot.co.uk/2013/08/start-android-media-scanner-from.html

  • On Paranoid Android (CM10 / JB 4.1) this causes an immediate reboot. Not sure if there is a problem with the ROM or something has changed. Will investigate. – Ken Sharp Feb 26 '14 at 16:55
  • On CM11 this does not work. – Jared Burrows Mar 13 '14 at 15:07
  • @JaredBurrows Have you used the correct path? In CM10 this has changed from /mnt/sdcard to /storage/sdcard0. – Ken Sharp Mar 13 '14 at 15:14
  • 1
    @Ken Many thanks and upvote. Finally my samba/ssh solution is all I need. – robert Sep 9 '15 at 13:33
  • 1
    This apparently no longer works on Android 8.1. Even the developer tools GUI, which uses the same broadcast, no longer works. – user149408 Mar 2 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.