Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Turn Off USB Storage on Android Devices

On many android devices, when the device is plugged into the USB port of a computer or even on some USB charging devices, the phone goes into USB Storage mode. When the device is in this mode, android apps cannot access the sdcard. Is there any way (1) to detect when the device is in this mode and (2) to programmatically turn off USB storage, at least temporarily, so my android app can access the sdcard?

I've seen other SO questions and the answers are not really sufficient (e.g., Android: Detecting USB).

like image 931
speedplane Avatar asked Sep 13 '11 04:09

speedplane


2 Answers

A1: If current state of Sd card is SHARED it means that it has connected to PC in MSC mode, you can check this case as following:

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_SHARED.equals(state)) {
    // Sd card has connected to PC in MSC mode
}

A2: You can force to turn off usb mass storage connection by calling:

MountService.setUsbMassStorageEnabled(false);

or

StorageManager.disableUsbMassStorage();

but unfortunately, both these API are not public accessible?!

like image 106
tonykwok Avatar answered Nov 20 '22 00:11

tonykwok


You can detect it (you have the link), but, AFAIK, you can't mount/unmount USB storage at least not with the public SDK APIs. In Honeycomb (3.0 and above), USB mass storage is no longer used to access the device's external storage, so your app and a computer can access it simultaneously.

like image 42
Nikolay Elenkov Avatar answered Nov 20 '22 01:11

Nikolay Elenkov