18

Background: I have a USB stick Android device (e.g. RikoMagic) which I mostly run in "headless" mode. Among other things, I've already figured out how to shut it down via a combination of adb and the internal "am" command:

adb shell am start -n android/com.android.server.ShutdownActivity

I'm now trying to figure out how to turn USB mass storage support on and off. I've got as far as having the USB storage activity window pop up via the following command:

adb shell am start -n com.android.systemui/.usb.UsbStorageActivity

Unfortunately I haven't been able to figure out how to send the equivalent "intent" to actually "unmount" or "unshare" the shared volume (or "mount" or "share" it for that matter). The following command does nothing:

adb shell am start -n com.android.systemui/.usb.UsbStorageActivity -a android.intent.action.MEDIA_UNSHARED file:///mnt/sdcard

What am I doing wrong? What's the magic incantation to mount/unmount the internal storage of an Android device?

Note 1: I know there's the obvious Linux "mount"/"umount" command. However, simply mounting or unmounting the volume won't trigger system functions vital to, for example, restarting or stopping apps that have been moved to the SD card. A "umount" will probably fail anyway when an app in the SD card is still active.

Note 2: Apparently there's a "setprop" command that can enable/disable USB mass storage support UNTIL the next reboot. Again, this all or nothing approach isn't what I'm looking for but the ability to toggle USB mass storage on and off during a single session, as can be done by the touch interface.

adb shell setprop sys.usb.config mass_storage
2
  • A very well researched question, wish I could upvote it twice! Thanks for your efforts and sharing those helpful and already working commands! Exemplary :) Crossing fingers someone can come up with the missing piece, as that would be useful indeed!
    – Izzy
    Jul 13, 2013 at 9:45
  • 1
    possible duplicate of Trigger "mount as disk drive" (aka "USB Mass Storage" mode) from PC
    – Dan Hulme
    Jul 13, 2013 at 13:46

2 Answers 2

10

Alas, the UsbStorageActivity doesn't use an intent to enable and disable USB mass storage, so there's no way to achieve this using am. It instead calls the functions StorageManager.enableUsbMassStorage and `StorageManager.disableUsbMassStorage to do its work. As these functions are hidden in the framework, it's not possible to write an app to do this, either. As far as I know, there's no existing command to do this.

It might be possible to write an app that offers a command-line interface and uses the bound system service MountService to call its setUsbMassStorageEnabled function, but that would be way off-topic for this site.

3
  • 1
    But what exactly happens when I tap on the "Turn on USB storage" button? Why isn't it possible to achieve the effect of this button press even when you go to the extreme of writing a whole new app? Wouldn't a more intelligent variant of the "monkey" utility do the trick? Jul 15, 2013 at 4:04
  • I've explained that in my answer: it calls the functions I mentioned, which in turn call into a system service, and the system service does all the work. For more detail you'll have to read the source. You can't even use monkey to click the button, as the code has an explicit check for that, to stop tests accidentally unmounting storage.
    – Dan Hulme
    Jul 15, 2013 at 7:38
  • You could try the service CLI tool which can call system service methods.
    – Léo Lam
    Aug 21, 2015 at 11:41
6


I tried the "StorageManager.enableUsbMassStorage" and the "StorageManager.disableUsbMassStorage" to enable and disable the mass storage and its works.
Thanks.

That methods are private method. So, If you want to use them, You shoud do it by reflection. To get that Storage Manager, you might use getSystemService(java.lang.String) method that included in the activities:

 StorageManager storage = (StorageManager)getApplicationContext().getSystemService(STORAGE_SERVICE);
 Method method = storage.getClass().getDeclaredMethod("enableUsbMassStorage");
 method.setAccessible(true); 
 Object r = method.invoke(storage);

And to disble mass storage:

StorageManager storage = (StorageManager) getApplicationContext().getSystemService(STORAGE_SERVICE);
Method method = storage.getClass().getDeclaredMethod("disbleUsbMassStorage");
method.setAccessible(true);
Object r = method.invoke(storage);
2
  • Amir Brand- I have tried to use this method but i am not able to disable my usb storage. Is it working with you ? if yes ,can you please help me out .thanks
    – Khanna
    Jun 24, 2015 at 4:43
  • Hi Khanna Sure. No problem. Please send me mail to netmir2002@gmail.com with the problem and I will try to find the solution.
    – Amir Brand
    Jul 14, 2015 at 13:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .