mtppics: a script for copying pictures off of Android phones

Way back in the old days, Android phones could be mounted like any other FAT-32 device (think thumb drives) on a Linux computer. Life was good. Somewhere along the line, Google changed this so the phones are instead exposed at Media Transport Protocol (MTP) devices. In some ways, this is an improvement. For one, the “USB mass storage” option required exclusive access; you could access files on your phone from your computer or from your phone, but not both.

However, using KDE’s Dolphin file browser was often an exercise in futility. File transfers would almost always fail, and maybe if you disconnected and reconnected the phone, it would work better. Or maybe it wouldn’t. As it turns out, this appears to mostly be a Samsung problem and is WONTFIX’ed by KDE developers.

I initially worked around this problem by writing a quick script that mounted the phone to a known location using simple-mtpfs, a FUSE driver for MTP. This allowed the contents of the phone to appear as a normal filesystem; my wife and I could copy pictures off with Dolphin or any other tool we wanted.

This worked pretty well, until it didn’t. I’m not sure why, or when exactly, but at some point it had stopped working. It mounted okay, but using Dolphin to access the files still crashed the MTP stack. I normally use the terminal to do the file copy anyway, so it didn’t bother me, but my wife is much more GUI-oriented.

So a few nights ago, I sat down to come up with another option. The real goal was just to be able to copy pictures from the phone to our file server, not to browse around arbitrarily. This meant that I could go for an automated solution. Initially, I thought it would be best to use MTP directly, but that turned out not to be a good idea. The PyMTP library provides a good interface to MTP, but it turns out the files are exposed as a single set of objects, not a hierarchy.

So I could have continued with that route, but it would have resulted in me basically writing my own FUSE driver, so I decided that was not how I wanted to spend the rest of the evening (and many of the next evenings). In the interests of having something usable, I continued to use simple-mtpfs, but I put all of the heavy lifting into the script.

The result is mtppics, which will copy pictures from a directory on the phone to a local directory. With a little bit of trickery and the date command, you can handle a variety of date-based target directories. It’s still very much designed for how my wife and I have our pictures archived, but I’m open to making it more flexible. I’ve tried to make it fairly robust to failure (I even used a trap statement!), but there are probably opportunities for improvement there, too. Give it a try if it would be of use to you and let me know how to improve it (or submit a patch, if you’re into that sort of thing).

Leave a Reply

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