8

The mediaserver in my stock and rooted Android 6.0.1 (running in Nexus 6) doesn't detect new files created or moved to into /sdcard using adb. It doesn't detect until I do a soft/full reboot which I find unacceptable to perform.

I'm able to manually run mediaserver using command-line to begin file scanning again# and I want this to be done automatically and immediately whenever a new file is created under /sdcard. At the moment, I have Tasker run the mediaserver whenever USB cable is unplugged.*

The obvious choice here seems to be an automation app of which I tried Tasker and Automate, and both amazingly failed to detect new files, particularly under /sdcard/Music, when created through adb. However, if an app created files in any directory under /sdcard those two apps immediately detected the changes. I also tried pushing files to /storage/emulated/0/ instead of /sdcard/ and made corresponding changes in those two apps' triggers.That step didn't benefit me.

It is to be noted that I observed this problem in stock Android 5.1.1 and a CM13 snapshot build as well.

So, how do I achieve the goal?

Update

I'm aware of the possibility of creating an alias in the OS running in my PC which would execute a script providing a way to push the file as well as run the mediaserver. However, it is something I would like to avoid as much as possible.

* I was using it but later switched to a script executed from PC using an alias. However, I do not want to depend upon any of the two methods. My fingers are comfortable with typing adb push instead of adb_push or some other but single word.

Update 2

  • I do not intend to restrict myself to a particular directory, such as Download and neither pushing files into the latter actually works.
  • I would like to stress that I'm not asking for how to run mediaserver. That I already know. All I'm looking for is: Whenever I do adb push FILE /sdcard/FILE the mediaserver should automatically and immediately kick in and scans the file. Regardless of any perceivable performance issue, I want it to kick in every single time.

Update 3

Okay, I may be using an incorrect term. Perhaps mediaserver and media scanner are terms for two different things in Android. In the case of confusion, please stick with "media scanner" for both the current revision and the bounty note.

Update 4

# Courtesy of androidyue for this helpful resource, any of the following command in preformatted text is what I can use in my device to make media scanner scan the file.

# FILE_PATH refers to the location of pushed file in Android. E.g. /sdcard/Music/lolcat.mp3

am broadcast -a android.intent.action.BOOT_COMPLETED -n com.android.providers.media/.MediaScannerReceiver
am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file://FILE_PATH   # guaranteed to work. See https://goo.gl/lH0CLT for proof.
am broadcast -a android.intent.action.MEDIA_MOUNTED -d file://FILE_PATH 

Update 5

Per the suggestion from Manubhargav, I tried Sarkie's MTPFix available here. It didn't work, both in stock Android 6.0.1 and CM13. I saw the following entry when I enabled the service for that app.

04-07 09:38:14.771  3383  3383 I MTPService: Watching /storage/emulated/0

I later pushed a sound file into /storage/emulated/0/ and nothing showed up in my music player. Logcat also didn't display anything useful either.

By the way, this app uses FileObserver, so I'm not surprised if it couldn't detect files created using adb, since it is the same issue with the automation apps I tried.


Note: Please post an answer only when you've tested it yourself. Thank you!

3
  • I found this, it's beyond my scope of knowledge (the answers point back to some MediaScannerConnection tool in developer tools) but it might help... stackoverflow.com/questions/15147536/…
    – acejavelin
    Mar 31, 2016 at 15:08
  • Note to readers: please don't suggest me to compile adb. That I'm not going to do even if you provide details for it.
    – Firelord
    Apr 4, 2016 at 8:37
  • Sarkie's MTP fix(APK to be installed on device) helps mediaserver scan the files copied under MTP. It scans recursively External Storage Directory and then adding to Media Scanner once a file has been created. It might help with ADB too, worth a try. Apr 6, 2016 at 17:34

2 Answers 2

2

Okay, it seems the answer is trivial. My fingers don't have to lose the comfort of typing adb push, yet I would be able to cause media scanner scan my pushed file, automatically and immediately. Here's my script tweaked to my needs:

(Note: root access is not required in Android)

#!/bin/bash

function push_file () {
    target="${@: -1}";
    file_path="${@: -2:1}";
    [[ "$prog_bar" == "-p" ]] && adb "$@" || $call_adb push -p "$file_path" "$target";
}

function broadcast () {
    file_name="$(basename "$file_path")";
    [[ `echo "${target: -1}"` != "/" ]] && unset file_name;
    [[ `echo "${target:0:1}"` != "/"  ]] && target="/${target}";
    $call_adb shell 'am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d '"\"file://$target$file_name\""'' > /dev/null;
}

if [[ "$1" == "-s" ]]; then
    if [[ "$3" == "push" ]]; then
        prog_bar="$4";
        call_adb="adb -s $2";
        push_file "$@" && broadcast;
    else
        adb "$@";
    fi
elif [[ "$1" == "push" ]]; then
        prog_bar="$2";
        call_adb="adb";
        push_file "$@" && broadcast;
else
    adb "$@";
fi

I then setup an alias named adb in .bashrc like this:

alias adb='bash FILE'  # FILE refers the file path of the said script

This was possible thanks to the very helpful article: Handling positional parameters at Bash Hackers Wiki.

Note: the script more or less requires a *nix based OS in PC with version 4.x of Bash installed and support for .bashrc. If you intend to use the script, you're encouraged to edit it to suit your working style.

0

I'm not sure how the media scanner works exactly, I think that the media scanner constantly updates the Download folder inside internal storage...

if something doesn't show up inside another folder, i copy it to the download folder and it triggers media scanner...

Try using Download folder inside Internal Storage as the absolute path

5
  • Nope. Doesn't work. BTW, did you yourself try this approach using ADB?
    – Firelord
    Apr 2, 2016 at 6:12
  • adb shell am broadcast android.intent.action.MEDIA_MOUNTED .........From stackoverflow.com/a/19435985/6116090 Apr 2, 2016 at 7:02
  • I think you misunderstand my question. I've virtually no problem in running the mediaserver manually. My question is about doing things automatically without any intervention from me. It means, as soon as I do adb push FILE /sdcard/DIR/ mediaserver should kick in and scan the file.
    – Firelord
    Apr 2, 2016 at 7:05
  • Have you tried making a batch file to automate the script.. Windows is commandlist.bat and Linux is commandlist.sh Apr 2, 2016 at 7:07
  • Yes. I'm currently using a simple script and I don't want to depend upon it. I've even addressed this in my question. Please check the content below Update in my question.
    – Firelord
    Apr 2, 2016 at 7:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .