Android Gallery app – Pictures order

A few weeks ago I bought a new Android phone, the Samsung Galaxy S2 to be precise. On my old cell I took a lot of pictures and I wanted to transfer them to the new phone. In order to do so, I backed up the camera folder /mnt/sdcard/DCIM/Camera to my computer and afterwards I copied it to the new phone. So far so good.
However, as soon as I looked at the pictures in the Gallery app it occurred to me that they were not chronologically ordered anymore. I found out that the Gallery app is sorting the pictures by the time stamps of the files. During the copy process all the timestamps of the photos have been lost.
The pictures are named as IMG_YYYYMMDD_HHmmSS.jpg and the videos are stored as VID_YYYYMMDD_HHmmSS.m4v. To solve the problem I have written a shell script which is “touching” the files depending on the file names to correct the time stamps.

#!/system/bin/sh
#
# 31.10.2011
#
# This script gets all pictures and 'touchs' them so the order of file
# creation is correct!
# The Android Gallery App orders the pictures by file creation time!
#
#################################################################
DIR="/mnt/sdcard/DCIM/Camera"
AWK="busybox awk"
SORT="busybox sort"
touch_files()
{
    if ! ( test -d $1 )
        then echo $1; return;
    fi
    cd $1
    echo; echo `pwd`: 		# Display Directory name
    FILES=`ls | $AWK '{print substr($0,4,length($0))}' | $SORT`
    for i in $FILES
    do
        echo $i; 		# Display File name
        touch *$i; 		# Touch for new date
        sleep 5; 		# Wait...
    done
}
touch_files $DIR

You can download the full script here. You need busybox installed to use this script. To install busybox you need to root your phone. Also you can not execute a script from the sdcard, so you have to copy it to an folder where you have executive rights, as /data/local/tmp/.

adb push reorderImages.sh /data/local/tmp
adb shell chmod 777 /data/local/tmp/reorderImages.sh
adb shell
cd /data/local/tmp
./reorderImages.sh

After executing the script you may have to reboot the phone. Afterwards your Gallery app should show the pictures in the correct order!

One thought on “Android Gallery app – Pictures order

Leave a Reply

Your email address will not be published. Required fields are marked *