Android - Making mediaserver immediately scan a file created through ADB

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.

Tags:

Automation

Adb