If there aren't any sizes that are big enough, this code can return a thumbnail with the wrong aspect ratio.  I think Mike said that if the thumbnail aspect ratio and the photo aspect ratio do not match then the camera firmware crashes.

So maybe you need to do a thumbnailSizes.filter() pass before sorting to remove options that are the wrong shape.