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.

Ken Sharp
  • 246
  • 1
  • 2
  • 15

1 Answers1

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

Ken Sharp
  • 246
  • 1
  • 2
  • 15
  • 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
  • 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. – Jared Burrows Mar 13 '14 at 15:21
  • However, this does work on Google Glass (Android 4.0.4). – Jared Burrows Mar 13 '14 at 22:28
  • This refers to the Android Media Scanner. I don't know enough about Play Music to help: I've barely looked at it, but it's a cloud service so I can't imagine it works anything like the Media Scanner. – Ken Sharp Mar 14 '14 at 23:38
  • 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 02 '19 at 01:54
  • I have not kept up with how Android works, as it do so really quite badly. No doubt many things have been broken since this was answered. A shame but there we go. – Ken Sharp Mar 03 '19 at 13:24