Refresh Android mediastore using adb

2019-01-16 04:26发布

I'm using adb to sync music on an android phone. Essentially, I rm the existing music directory and push replacement music files.

I'd like to be able to use adb to force a rescan, so that the google music player (and other apps) works properly with the new songs and playlists.

According to How can I refresh MediaStore on Android? you can force a rescan by broadcasting an appropriate intent.

adb provides 'shell am broadcast', which would seem to allow me to force a rescan from adb.

Alternatively I could run a rescan app or reboot, but I'd like to trigger the rescan from adb

What adb command should I issue? The music files and playlists are all in /sdcard/music.

3条回答
你好瞎i
2楼-- · 2019-01-16 04:54

The MEDIA_MOUNTED intent is no longer permitted (post KitKat) for non-system apps; try this instead.

It’s not recursive, though, and has to be run on the exact_file_name, so it’s not a good replacement.

adb shell am broadcast \
    -a android.intent.action.MEDIA_SCANNER_SCAN_FILE \
    -d file:///mnt/sdcard/Music/<exact_file_name>

If you need to rescan recursively, you can use this command (fix paths accordingly):

adb shell "find /mnt/sdcard/Music/ -exec am broadcast \
    -a android.intent.action.MEDIA_SCANNER_SCAN_FILE \
    -d file://{} \\;"

Or like this (if above won't work for you):

adb shell "find /mnt/sdcard/Music/ | while read f; do \
    am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE \
    -d \"file://${f}\"; done"
查看更多
倾城 Initia
3楼-- · 2019-01-16 04:58

If you have rooted your phone, you can use this script I’ve written, which has the advantage of keeping track of which files have already been updated:

#!/system/bin/env busybox ash

MUSIC_LIBRARY=/sdcard/MusicLibrary

LAST_UPDATE="$(stat -c %Y "$MUSIC_LIBRARY/.last-update")"

find "$MUSIC_LIBRARY" -type f ! -iname ".last-update" | (
  while read f; do
    if ! test "$LAST_UPDATE" -ge "$(stat -c %Y "$f")"; then
      am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d "file://$f"
      touch "$f"
    else
      echo "Not updated: \`$f'"
    fi
  done
)

touch "$MUSIC_LIBRARY/.last-update"
查看更多
Explosion°爆炸
4楼-- · 2019-01-16 05:17

The rescan apps use a media mount intent to kick off the media scanner. You can use am broadcast to send the same intent.

The command is:

adb shell am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///sdcard
查看更多
登录 后发表回答