2

I often manage my files in Termux with scripts that use the mv command to move or rename them. I want to manually trigger MediaProvider to scan my internal stroage (/storage/emulated/0) or a given directory in it with command line, on a phone running Nougat or Oreo.

When I use a GUI file manager application, all moves/renames are immediately reflected in other MediaScanner-dependent apps like Gallery, but changes done in a terminal don't, until a reboot. So I have to manually call MediaScanner to update my changes in the filesystem.

Before KitKat, this worked well on /storage/sdcard0 (a separate partition named glow):

am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///sdcard

The above command doesn't work now (/storage/emulated/0), obviously.

I also tried this, but it doesn't seem to be working, either.

am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:///sdcard

Just tested the commands again, they seem to be working on removable storage like /storage/sdcard1, /storage/usba or /storage/{volume id} (it gets re-scanned), but not my phone's internal (where /sdcard points to, or /storage/emulated/0).

Anyone have some insights? Rebooting my phone always causes a rescan but that's inconvenient in some cases.

Note: the commands are run as root, so no concerns about permissions.

8
  • Trigger a broadcast that the SD card was mounted? That implicitly triggers the media scanner as it needs to check for possible additions you made to the card. That's the first command you've mentioned. I wonder why this should no longer work. Have you tried? // Another idea: on some devices I found a corresponding entry in developer settings, letting you trigger a rescan. Worth taking a look. – Izzy Jul 28 '18 at 18:45
  • @Izzy Not SD card, but internal storage. I guess it's internal storage that causes the problem (not scanned actually). – iBug Aug 24 '18 at 16:26
  • I don't know how to do this from adb, but I think you can go to Settings - Apps - All Apps, then tap the 3 dot menu and select Show System, then scroll down to Media Storage and open it, tap Storage, then Clear Data and after it completes reboot. That works on Moto devices with Nougat and Oreo, it might not be what your looking for but it should net the same result. – acejavelin Aug 24 '18 at 17:31
  • Even though it's internal storage (you really mean /data, or rather the internal SD?) – might be worth a try to broadcast storage mounted (take the internal SD then). Didn't say unmount/mount, just broadcasting. – Izzy Aug 27 '18 at 5:39
  • @Izzy To clarify, I meant /sdcard which points to /storage/emulated/0 because I find that it behaves differently between /storage/emulated/0 and /storage/sdcard1//storage/usba. – iBug Aug 27 '18 at 7:15
3

Note: It is possible the following commands do not work out for you with just USB debugging / shell (uID 2000) user. In that case, you must have root access to make them work.


Before KitKat, this worked well:

am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///sdcard

The above command doesn't work now, obviously.

This works perfectly well on my stock Android 7.1.1 (Nexus 6) having only Internal Storage. But starting since Oreo implicit broadcasts are not allowed, so you have to send an explicit broadcast to Media Storage. This can be done using the argument -n followed by component name or simply by -p followed by package name. In Android 9.0 (OnePlus 6), here's what worked for me:

am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///sdcard/ -p com.android.providers.media

I believe the above command would work for Android Oreo as well.

Note: shell (user 2000) cannot run that command, so you must elevate privilege to system (1000) or root (0). In short, run the command as root or it won't work.


I also tried this, but it doesn't seem to be working, either.

am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:///sdcard

Because that intent expects a FILE as data, not a directory (I understand everything is a file in Linux, but that's not the case for an intent). Provide the full path of a file and it would work. Furthermore, as stated above, for Oreo and above, make the broadcast explicit.

If you have multiple files and you intend to use this intent only, you need to get all the file names and pass them on to the intent recursively (per Aritra Roy on Stack Overflow). It may be possible to pass an array but I don't know how to do it with adb.

Furthermore, my Android behaved rather erratically for file names containing [].

2
  • In Android 10 (Huawei Mate 20 Pro), got error of Security exception: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=28279, uid=2000 when tried to run adb shell am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///sdcard/ -p com.android.providers.media – pvd Sep 25 '20 at 9:03
  • @pvd sorry for the late reply. Yes, I see that I also get the same error on OnePlus 6 running Android 10. However, running the same command with root access works fine. I have added a note in my answer to stress the need of root access, should the command fails to work. Thanks for bringing it to my notice. – Firelord Sep 28 '20 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.