14

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.

0

1 Answer 1

14

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

8
  • 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, 2014 at 16:55
  • On CM11 this does not work. Mar 13, 2014 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, 2014 at 15:14
  • Sorry I meant to explain more. Im running CM11 Android 4.4.2 Kit Kat. I have tried /sdcard/, /sdcard/Music. My sdcard is very empty. I am trying to refresh Google Play Music. Mar 13, 2014 at 15:21
  • 2
    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, 2019 at 1:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .