MTP and USB on the Android Nexus 4


I ran across MTP within minutes of turning on my new Nexus 4. MTP is the default storage mode for this Android device. Instead of unmounting the SD card on the phone and handing control to a PC, the phone and the PC share access to storage at the same time. On Ubuntu 12.04 I didn't find accessing the device to be very straighforward so I decided to share what I put together.

 1 #!/bin/bash
 2 
 3 if [ `lsusb |grep Google |wc -l` == 0 ]; then
 4     echo "plug in the device"
 5     exit 1
 6 fi
 7 
 8 if [ `dpkg -l mtpfs |wc -l` == "0" ]; then
 9     sudo apt-get update && sudo apt-get install mtpfs
10 fi
11 
12 rules_file=/etc/udev/rules.d/60-android.rules
13 if [ ! -f $rules_file ]; then
14     vp=`lsusb|grep Google|cut -d " " -f 6`
15     vendor=`echo $vp | cut -d ':' -f 1`
16     product=`echo $vp | cut -d ':' -f 2`
17     rule="SUBSYSTEM==\"usb\", ATTR{idVendor}==\"$vendor\", ATTR{idProduct}==\"$product\", MODE=\"0600\", GROUP=\"plugdev\" OWNER=\"$USER\""
18     sudo bash -c "echo $rule > $rules_file"
19 fi  
20 
21 mount_point=/media/nexus
22 if [ ! -d $mount_point ]; then
23     sudo mkdir $mount_point
24     sudo chmod 775 $mount_point
25 fi  
26 sudo mtpfs -o allow_other $mount_point
comments powered by Disqus