Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
410 views
Welcome To Ask or Share your Answers For Others

1 Answer

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?!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...