Search

Nautilus - Thumbnailer for APK files

Contents[Hide]

dropcap-gnome-apk

On Android devices, all programs are installed thru some APK archives. Under Gnome Nautilus, these APK archives are recognised as a specific mime types and are displayed with some generic text file icon.

As most of the time the name of these APK archives is not very friendly, it is difficult to associate an APK file with the Android application it contains. To help you with this association, it could be very helpful to display the application icon as it is done under Android instead of the generic icon.

Thanksfully, under Gnome Shell, Nautilus allows to generate a specific thumbnail according to the APK mime type application/vnd.android.package-archive. As APK archive are supposed to contain their Android application icon, it becomes possible to display the application icon as the Nautilus thumbnail. You can also add APK version provided by the embedded manifest.xml.

nautilus-apk-thumbnail

This article explains all the steps needed to configure Nautilus to display the APK embedded application icon as thumbnail. This allows you to handle your APK files from Nautilus as they appear on your Android device.

This procedure has been tested under Ubuntu and Ubuntu Gnome 16.04 LTS and even LUbuntu with PCManFM.
It should be applicable to many other distributions.

1. Install packages

We will use aapt tool, from Android SDK to extract data from APK files. Either you have already installed the complete SDK or you just need this tool from Ubuntu repository.

So, for the thumnailer to do its job, we need to install the following packages : gvfs-copy, unzip, aapt and netpbm tools.

Terminal
# sudo apt-get install gvfs-bin aapt unzip netpbm

2. Create the Thumbnailer

When Nautilus needs to generate a thumbnail for a file, it is calling a 'thumbnailer'. This program (or script) is in charge of generating the thumbnail in a PNG format with a given height.

When we deal with APK archives, the thumbnail can be generated from the embedded icons. In fact every APK archive is a standard ZIP archive and is most of the time embedding some icons that will be used to display the application icon on the android desktop.

Icons may be available in different formats in the package (ldpi, mdpi, hdpi, xhdpi), but most of the time official package icon is having a mdpi or hdpi resolution.

You can use the aapt tool available from android SDK to determine the application icon. Icon files are listed with the d badging option.

Terminal
# aapt d badging com.whatsapp.apk | grep application | grep icon
application-icon-120:'res/drawable-hdpi/icon.png'
application-icon-160:'res/drawable-hdpi/icon.png'
application-icon-240:'res/drawable-hdpi/icon.png'
application-icon-320:'res/drawable-xhdpi-v11/icon.png'
application: label='WhatsApp' icon='res/drawable-hdpi/icon.png'

The following script is a Nautilus 'thumbnailer' that will do the thumbnailing job using aapt :

/usr/local/sbin/apk-thumbnailer
#!/bin/bash
# ---------------------------------------------------
# Thumbnailer for APK archives files
# Display embedded icon and version number (option)
#
# Procedure :
#   http://bernaerts.dyndns.org/linux/76-gnome/284-gnome-shell-generate-apk-thumbnail-nautilus
# Depends on :
#   * unzip
#   * aapt
#   * pngtopnm, pnmtopng, pnmcrop, pnmpad, pnmscalefixed and pnmcomp (netpbm package)
#   * gvfs-copy (gvfs-bin package)
# Parameters :
#   $1 - URI of APK archive
#   $2 - full path of generated thumbnail
#   $3 - height of thumbnail in pixels
# Revision history :
#   04/08/2013, V1.0 - Creation by N. Bernaerts
#   11/08/2013, V1.1 - Use aapt to get icon file
#   05/11/2014, V2.0 - Add version display
#   06/11/2014, V2.1 - Don't use renamed aapt anymore
#   15/11/2014, V3.0 - Use URI to handle network shares
#   07/10/2017, V3.1 - Speedup with netpbm tools
# ---------------------------------------------------

# check tools availability 
command -v aapt >/dev/null 2>&1 || exit 1
command -v unzip >/dev/null 2>&1 || exit 1
command -v pnmcrop >/dev/null 2>&1 || exit 1
command -v pnmpad >/dev/null 2>&1 || exit 1
command -v pngtopnm >/dev/null 2>&1 || exit 1
command -v pnmscalefixed >/dev/null 2>&1 || exit 1
command -v pnmcomp >/dev/null 2>&1 || exit 1
command -v pnmtopng >/dev/null 2>&1 || exit 1

# set to no to remove version number on the icon
DISPLAY_VERSION="yes"

# get parameters
FILE_URI="$1"
FILE_THUMB="$2"
SIZE="$3"

# get filename extension
FILE_EXT=$(echo "$FILE_URI" | sed 's/^.*\.\(.*\)/\1/')

# generate temporary local filename
TMP_LOCAL=$(mktemp -t XXXXXXXX.${FILE_EXT})
TMP_PNM=$(mktemp -t XXXXXXXX.pnm)

# if file is a remote one
URI_TYPE="${FILE_URI:0:4}"
if [ "${URI_TYPE}" = "file" ]
then
    # convert URI to local path
    FILE_PATH=$(printf '%b' "${FILE_URI//%/\\x}")

    # extract local path
    FILE_LOCAL="${FILE_PATH:7}"
else
    # check gvfs-copy availability
    command -v gvfs-copy >/dev/null 2>&1 || exit 1

    # copy input file to temporary local file
    gvfs-copy "${FILE_URI}" "${TMP_LOCAL}"
    FILE_LOCAL="${TMP_LOCAL}"    
fi

# get application icon
ICON=$(aapt d badging "${FILE_LOCAL}" | grep "application-icon" | sed 's/^.*icon-\(.*\):.\(.*\).$/\2/' | tail -1)

# if icon exists, extract icon & generate thumbnail 
if [ "$ICON" != "" ]
then 
    # if needed, add version display options
    if [ "$DISPLAY_VERSION" = "yes" ]
    then
        # get version
        VERSION=$(aapt d badging "${FILE_LOCAL}" | grep "versionName=" | sed 's/^.*versionName=.\([0-9.]*\).*$/\1/')

        # calculate optimum font size
        FONT_SIZE=$((${SIZE} / 10 + 1))

        # generate version image
        pbmtextps -fontsize ${FONT_SIZE} "${VERSION}" | pnmcrop - | pnmpad -white -left 4 -right 4 -top 4 -bottom 4 - | pnmpad -black -left 2 -right 2 -top 2 -bottom 2 - > "${TMP_PNM}"

        # generate icon including version number
        unzip -p "${FILE_LOCAL}" "${ICON}" | pngtopnm - | pnmscalefixed -xysize ${SIZE} ${SIZE} - | pnmcomp -align center -valign bottom "${TMP_PNM}" - | pnmtopng > "${FILE_THUMB}"

    # else, no version needed
    else
        # generate plain icon without version number
        unzip -p "${FILE_LOCAL}" "${ICON}" | pngtopnm - | pnmscalefixed -xysize ${SIZE} ${SIZE} - | pnmtopng -downscale -compression 0 - > "${FILE_THUMB}"
    fi
fi

# remove temporary local file
rm ${TMP_LOCAL} ${TMP_PNM}

You can install this thumbnailer with the following commands :

Terminal
# sudo wget -O /usr/local/sbin/apk-thumbnailer https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/thumbnailer/apk/apk-thumbnailer
# sudo chmod +rx /usr/local/sbin/apk-thumbnailer

If you want the thumbnailer to display version number on the icon, you need to edit the script and set DISPLAY_VERSION="yes"

3. Declare the Thumbnailer

To allow Nautilus to use the proper thumbnailer for APK archives, you need to declare the previous script as the thumbnailer for the mime type of APK archives (application/vnd.android.package-archive).

This declaration is done via a apk.thumbnailer file placed under /usr/share/thumbnailers. This file follows a specific format, where association between the mime type and the thumbnailer is done.

A complete description of this file format is available from Gnome Developers site on the page Installing a Thumbnailer Program.

As we want to get APK icon even for file which are accessed thru a network share, we will use the file URI instead of it's plain filename. This will allow the thumbnailer script to handle the files thru GVFS.

/usr/share/thumbnailers/apk.thumbnailer
[Thumbnailer Entry]
TryExec=/usr/local/sbin/apk-thumbnailer
Exec=/usr/local/sbin/apk-thumbnailer %u %o %s
MimeType=application/vnd.android.package-archive;

You can download this thumbnailer declaration file with the following commands :

Terminal
# sudo wget -O /usr/share/thumbnailers/apk.thumbnailer https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/thumbnailer/apk/apk.thumbnailer

4. Enjoy your APK Thumbnails

You can now purge your thumbnails to allow them to re-generate and restart nautilus to reload the thumbnailer scripts.

Terminal
# rm -r $HOME/.cache/thumbnails
# nautilus -q

Please note that if you run Lubuntu, thumbnails should be removed under $HOME/.thumbnails and thumbnailer will work after next reboot.

You will now enjoy to see your APK archives displayed with their embedded icons.


Hope it helps,

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus