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.

  • 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
2

Before KitKat, this worked well:

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

The above command doesn't work now, obviously.

I beg to differ with this view. This works perfectly well on my stock Android 7.1.1 (Nexus 6) having only Internal Storage.

It is limited to system applications, so you can run it as user 1000 or as root.

adb shell su -c am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///sdcard/   # A blogger suggested to replace /sdcard/ with /mnt/sdcard/ for better results. But former already works for me.

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.

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 [].

If both intents are failing on Android 7.1.x, then something is definitely wrong with your ROM.

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.