Search This Blog

Start the Android media scanner from the command line

If you're like me, and let's hope for your sake that you're not, you find it much easier to run an SSH server on your Android device than using something like adb or USB Mass Storage or any of the other crap out there.

With SSH I can simply and easily check logcat, netstat, issue commands, copy files and generally bugger about.

Sometimes you have moved quite a lot of files around and you need your media scanner to launch to update its database. This can be done via the CLI.

Connecting over SSH doesn't necessarily ensure that you have the correct environment set up for the media scanner to understand your command. You can either add the relevant entries to your .profile or .bashrc files (depending on your setup) or you could 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

This works with Cyanogenmod 9.1. If this fails for any reason you may need to look into your environment. If you have a second mount you should edit the script accordingly.

On successful completion you should see:

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

References:
http://developer.android.com/reference/android/content/Intent.html
http://forum.xda-developers.com/showthread.php?t=1752749
http://intwoplacesatonce.com/2013/04/pushing-media-to-androids-gallery-using-adb/
VigLink badge My profile on StackExchange