How To Use MTP-connected Devices

by Doug Gordon » Thu, 03 Mar 2011 01:48:04 GMT

Sponsored Links
 My app currently requires the user to manually copy certain files from 
their PC to their USB-mounted Android device where my app can open them 
and do its thing. But I'm already running into issues with users' 
devices that want to mount in this "MTP" mode, and I hear that the Xoom 
*only* supports MTP, etc.

So how can MTP be used to transfer files to the device that don't really 
fit into the "Media files" category, and how do I locate the directory 
from my app? What do getExternalStorageDirectory and getExternalFilesDir 
return for a device like the Xoom?

Doug Gordon
GHCS Software


Re: How To Use MTP-connected Devices

by FrankG » Thu, 03 Mar 2011 04:59:35 GMT

 Hi Doug,

I can imagine that this will be more an more a problem for
like yours : You cannot know in which usb mode the target device is
as this is highly vendor/manufacturer specific. You would also run
into trouble
if the user will use pictbridge. And you also not able to trigger a
mode change
because the interface to this is also specific for the manufacturer.

Would be interessted to know whether XOOM only supports MTP, but --
how far I understood Dianne in the past -- it must be at least MTP
otherwise Android Market will not work ?

Good luck !



Sponsored Links

Other Threads

1. Insertion Mount on state_Pending.

We have a problem with froyo in which the MMC does not get
automatically mounted upon insertion.

This is what i could understand from the code:
in DirectVolume.cpp on the function handleDiskAdded, if the number of
partitions is not 0, the volume state gets set to "State_Pending" ,
and the VolumeDiskInserted broadcast is sent.This triggers the onEvent
method on MountService, which starts a new thread that sends the
command "volume mount"

the "volume mount" command, gets handled in Volume.cpp function
mountVol() which checks if the state is "State_Idle" if not, returns
BUSY, which in turn gets translated to code 405 "OpFailedStorageBusy"
which is what we see on MountService. This happens because we have
just set the state to "State_Pending".

If we force "State_Idle" on handleDiskAdded, instead of going into
state_Pending, it fixes our problem.

A possible fix for this is setting a flag in the onEvent method on
MountService when the disk is inserted, and starting the thread that
sends the mount command when we receive the state-change to idle, and
flag is set.

In short, we should try to mount when the state has changed to idle,
not on insertion.

Here is the patch for this:

index 6ceeb95..0adaf19
--- a/services/java/com/android/server/
+++ b/services/java/com/android/server/
@@ -136,6 +136,8 @@ class MountService extends IMountService.Stub
     private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
     private static final int MAX_UNMOUNT_RETRIES = 4;

+    /*used to send the mount command after state is idle.*/
+    boolean insertionMountPending = false;
     class UnmountCallBack {
         String path;
         int retries;
@@ -495,6 +497,26 @@ class MountService extends IMountService.Stub
              * Format: "NNN Volume <label> <path> state changed
              * from <old_#> (<old_str>) to <new_#> (<new_str>)"
+            if ((Integer.parseInt(cooked[10]) == VolumeState.Idle) &&
+                 insertionMountPending == true) {
+                /* If the state moves to idle after a insertion
+                 * try to mount the device "Insertion mount"
+                 */
+                final String path = cooked[3];
+                insertionMountPending = false;
+                new Thread() {
+                    public void run() {
+                        try {
+                            int rc;
+                            if ((rc = doMountVolume(path)) !=
StorageResultCode.OperationSucceeded) {
+                                Slog.w(TAG, String.format("Insertion
mount failed (%d)", rc));
+                            }
+                        } catch (Exception ex) {
+                            Slog.w(TAG, "Failed to mount media on
insertion", ex);
+                        }
+                    }
+                }.start();
+           }
                     cooked[2], cooked[3],
@@ -526,18 +548,11 @@ class MountService extends IMountService.Stub

             if (code == VoldResponseCode.VolumeDiskInserted) {
-                new Thread() {
-                    public void run() {
-                        try {
-                            int rc;
-                            if ((rc = doMountVolume(path)) !=
StorageResultCode.OperationSucceeded) {
-                                Slog.w(TAG, String.format("Insertion
mount failed (%d)", rc));
-                            }
-                        } catch (Exception ex) {
-                            Slog.w(TAG, "Failed to mount media on
insertion", ex);
-                        }
-                    }
-                }.start();
+               /* Instead of tring to mount here, wait for
+                * the state to be Idle before atempting the
+                * insertion mount, else "insertion mount" may fail.
+                */
+               insertionMountPending = true;
             } else if (code == VoldResponseCode.VolumeDiskRemoved) {
                  * This event gets trumped if we're already in


2. bertanya upgrade os

misi gua baru masuk ke komunitas ini jadi kalo ada salah-salah dikit
tolong dimaklumi ya :)

oke langsung kepada judul diskusi,jadi hp saya adalah htc hero dan
versi os nya itu masih 1,5.tolong bantu saya untuk mengupgrade os

"Indonesian Android Community [id-android]" 

3. Accessing Calendars Synched from MS Outlook

4. Freelance game designers / illustrators for game development team

5. Emulator bug drawing lines on GLSurfaceView

6. App download too less

7. R: how does Android Market stop malicious attacks among competitors of apps?