Refresh Android mediastore using adb

非 Y 不嫁゛ 提交于 2019-11-28 03:08:37
user2892047

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
Manas Tungare

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"
Sander

On some Samsung mobiles, you can get a full rescan like this:

am broadcast -a com.samsung.intent.action.MTP_FILE_SCAN -n com.android.providers.media/.MediaScannerReceiver

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"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!