MTP in Android detailed

Source: Internet
Author: User
Tags file transfer protocol

MTP in Android detailed

Recently for a long time did not take notes, and today mainly learn about MTP-related knowledge.

MTP is full of media Transfer Protocol (Media Transfer Protocol), it is a set of Microsoft's proposed medium file Transfer protocol. Android supports MTP starting from 3.0.

A Background knowledge introduction

I believe most readers of programmers magazine have used MTP more or less. As early as the popularity of smartphones, digital cameras and MP3 players, such as the use of MTP predecessor PTP (picture Transfer Protocol) for media file transfer. At that time, you could see these devices in my computer as long as they were connected to the Windows operating system via a USB cable. After that, users can use them as a USB flash drive, such as directory, file browsing and copying.

Since the smart device can be used as a USB flash drive with MTP, how is it different from our usual USB mass storage (USB Mass Storage, or UMS)?

    • In UMS mode, the granularity of the PC operating storage device is the device block (FAT block), not the file system. What do you mean? Here is a simple example. When the Android phone is attached to the PC via UMS, the PC has absolute control over the SDcard. SDcard. This way, the phone will not be able to access sdcard at the same time. The result of this approach is that the camera or music program will not be able to operate because there is no external storage space (note that some manufacturers ' phones have been modified to allow the camera to record part of the video to internal storage for a short time). This is also an obvious feature in earlier versions of Android. Also, because the PC may break its file system when operating SDcard, this will cause the sdcard to be re-attached to the phone and not be recognized.
    • If the Android phone's sdcard is attached to the PC in MTP mode, the control of the SDcard is still part of the phone. Only the smart phone has built a virtual file system to the PC via MTP protocol. When a PC operates a file, it initiates a request to the smartphone via the standard MTP protocol. In addition, Android integrates MTP functionality into the Mediaprovider, which has the advantage that media data will be updated to the media database in a timely manner when PC operations (such as copying or deleting) media files. In UMS mode, when SDcard is mounted back to the phone, Android also takes a long time to rescan the media files to update the media database.

There are many benefits to MTP, such as the ability to determine whether a PC-copied media file is supported by the target phone, or even trigger the corresponding transcoding program to convert it to a phone-supported format. However, compared with UMS, MTP also has shortcomings:

    • Transfer large files at a slower speed.
    • MTP cannot directly modify the file itself. Can only be copied to local modifications, and then copied back.
    • In addition to Windows, the MTP support for both Linux and MacOS is not perfect.

Below we will introduce the MTP protocol.

1.1 MTP Protocol Introduction

According to the agreement, the users of MTP include two parts, namely initiator and responder. 1-1 is shown below:

Figure 1-1 Initiator and responder illustration

The figure 1-1 shows that:

    • Initiator: Mainly refers to the USB Host, such as PC, Notebook and so on. The agreement stipulates that all MTP operations can only be initiated by Initator.
    • Responder: Typically, devices such as digital cameras, smartphones, and other storage media files. The role of responder in MTP is to handle requests initiated by Initator. At the same time, it sends an event to notify initiator based on changes in its state.

Note: Later we will unify the PC on behalf of the Initiator,android mobile phone on behalf of responder.

As with many protocols, MTP also has its own stack of protocols, as shown in 1-2:

Figure 1-2 MTP Protocol stack

As shown in Figure 1-2, the MTP stack is from bottom to top, respectively:

    • Pyshical layer (physical layer): the physical layer is used to transmit data in the MTP protocol. There are currently three physical layers available for MTP use. They are USB: The main feature is the transfer of files, synchronization of media files speed, and can work while charging, which is currently used the most one way; IP: An IP-based MTP (MTP/IP) will match and discover devices through UPnP. It is the most ideal transmission mode in the home network, BLUETOOTH:MTP/BT is the most power-saving, but also the slowest transmission mode, less useful.
    • Transport layer: MTP, data transfer format follows PTP Protocol
    • Command layer: Implements the various commands in the MTP protocol.

As mentioned above, MTP uses command-response mode to work (Initator send commands to responder processing, responser feedback processing results), the main features of this approach are:

    • All MTP commands are delivered on both sides of the device in the form of a package (packet).
    • The initiator must receive the result of the previous message (whether successful or timed out) before the next message can be sent.

Below we will take the PC via MTP to open a file as an example, described in sequence which involves several major MTP commands:

  • When the device is first connected to the PC, initiator (that is, the PC) first sends a request called Getdeviceinfo to obtain information about the device, including the extent of the PTP version supported by the device, expressed as a percent sign (default is 100), The supported MTP commands (Operation supported), the supported event types, and so on.
  • The PC-side then sends the opensession command to create a session that remains until the device disconnects from the PC. Thereafter all commands (except the Getdeviceinfo command) must survive this session to be sent. The session is identified by SessionID in the MTP protocol, which is a 32-bit unsigned integer that is selected by the PC and passed to the phone.
  • PC side If you want to do file operation, you must locate the target file from the root directory. Due to the particularity of Windows, the phone's internal memory card is displayed as a drive letter in the Windows system. Note that if you have two memory cards inside your phone (such as an internal memory card and an external SD card), you will see two drive characters in Windows. The PC side needs to return the Storageid of a drive letter via the Getstorageids command. In MTP, Storageid is a 32-bit unsigned integer, and each Storageid represents a logical drive letter.
  • The PC can use the Getstorageinfo operation to obtain information about the storage device, such as the remaining storage space, file system type, access rights, and so on, according to the Storageid number of the previous step.
  • The PC then obtains the object Handles of the files and subdirectories under this drive by the Getobjecthandles command (an object handle represents a file or directory. This value is generated by responder and is guaranteed to be unique. With Object handle,pc, you can manipulate these files or directories, for example, continue to get information about sub-files and subdirectories in a directory through Getobjecthandles.
  • If you need to copy a file to your phone now, the PC will pass the Sendobjectinfo command to the phone with the file information (such as filename, file size). The phone needs to check if the target directory has sufficient space and corresponding permissions.
  • If everything works, the PC will pass the data to the phone via SendObject. The responder that actually writes the file to the device storage space is the phone. An MTP implemented by Android also updates the information to the media database after the media file has been transferred.
  • In addition, the PC can also use the Setobjectpropvalue command to set various attribute values of the file, such as audio bitrate (bitrate), sample rate (sample rates), number of Channels (channel), etc.

The above describes a simple case for MTP use. As for the various MTP commands, readers may wish to read reference 1, "MTP specification V1.0.pdf". The protocol has a very precise description of the various commands, such as table 1-1, table 1-2, the Getdeviceinfo command, and the return value definition. The parameter type, the direction of delivery is explained in detail (it must be said, compared to Linux, Microsoft's Development/technical documentation is done quite well).

Table 1-1 getdeviceinfo Command definition

Operation Code


Getdeviceinfo the number of the corresponding command is 0x1001


DeviceInfo DataSet

Device information data set returned by mobile phone

Data Direction


Data transmission direction is mobile phone to PC

Responsecode Options

OK, parameter_not_supported

The return value of the phone to the PC

Table 1-2 shows the definition of the returned data set for Getdeviceinfo.

Table 1-2 Getdeviceinfo Returns the definition of a dataset

Dataset Field

Field Order

Size (bytes)



Standard Version




The level of support for the PTP protocol, expressed in%, by default is 100

MTP Vendor Extension ID




Mobile phone support for PTP Vendor extension protocol, default is 0xFFFFFFFF

MTP Version




The version of the MTP standard supported by the phone, expressed in%

MTP Extensions




Supported MTP expansion sets for mobile phones

Functional Mode




Allowed mode of mobile phone

Operations supported



Operation Code Array

All operations supported by the phone in the current feature mode

Event supported



Event Code Array

All events that can be generated by the phone in the current function mode

Device Properties Supported



Device Property Code Array

All device properties supported by the phone in the current feature mode

Capture Formats



Object Format Code Array

The phone can generate its own file format, not including the copy to the phone file format

Playback Formats



Object Format Code Array

All format types that the phone can parse and understand





Identification of a human-readable handset manufacturer





Human-readable phone models

Device Version




Software or firmware version of the phone

Serial number




Unique serial number that can indicate the MTP function of mobile phone

1.2 OS support and certification for MTP

Since the MTP agreement is presented by Microsoft, it is natural that Windows has spared no effort in its support. In the current Windows operating system, MTP and multimedia frameworks are tightly coupled and have become an important part of the Windows Media Framework. such as WMP10 (Windows Media Player 10) and WMP11 are built-in to the MTP feature, where WMP11 also adds support for playlist and album art.

In addition to the MTP protocol and strong support for the Windows operating system, Microsoft has managed devices that use MTP protocols. All devices that are nominally supported by MTP protocol must pass Microsoft's Test wlk (Windows Logo Kit). The WLK test passes a device that can get a logo. For more information about the WLK test, please refer to The above link can also be downloaded to Wpdmon, which is the most commonly used test tool in MTP development, which shows the commands, data and return values that are sent when the MTP operation is performed by all PCs and phones. Figure 1-3 for the author to test the MTP function of an Android phone with Wpdmon intercepted information:

Figure 1-3 Wpdmon Tool use

Let's look at the implementation of MTP in the Android platform.

Two of the MTP in Android

Android has been integrating MTP functionality from 3.0, with three main reasons:

    • To support UMS, the phone must have an SD card, because SD cards are often formatted with Windows-supported partitions. If you want to attach the internal storage space to Windows via UMS, the internal storage space needs to be in a specific partition format. This is simply not feasible for some phones. Because the internal storage space itself may be a device, they use a uniform partitioning format. You cannot add a specific partition format to a storage device because you need to use UMS.
    • After the UMS is mounted to the PC, the PC operating system has absolute control. At this point, the Android system will not be able to manipulate these devices. This is unacceptable for an increasingly advanced version of Android, according to the former Wenju camera example.
    • Another undeniable fact is that the Windows operating system still occupies an extremely high market share among the ordinary working people. This is probably also know that Linux, MacOS support for MTP is not enough, Android also to integrate it is an important reason for it.
2.1 Code architecture for MTP in Android

To use the MTP feature, you first need to enable the USB connection mode in settings as shown in Mtp,1-4:

Figure 1-4 MTP settings in Settings

Figure 1-4 shows the "USB connection mode" setting in the reference machine (Android 4.1 version). This action actually triggers the USB drive to make the corresponding changes. This article is not intended to discuss the process in which the reader can refer to the init.platform-name.usb.rc file in the phone to see the mode settings of the USB in the Android system. From the current release of several models of Android 4.0 and subsequent versions of the model, MTP/PTP has a significant alternative to ums trend.

As mentioned earlier, the MTP in Android and the existing Mediaprovider modules are tightly coupled to better reflect the "Media Transfer" feature. Its main structure is shown in 1-5:

Figure 1-5 Android MTP Frame composition

As shown in Figure 1-5, the Android MTP architecture is from bottom to top, respectively:

    • The C + + layer consists of several main objects, such as Mtprequestpacke, which reads data from the USB drive, and constructs the command format and its parameters, mtpdatapacket the packets that are responsible for structuring the phone to be returned to the PC, Mtpresponsepacket is responsible for the response of the structured phone to be returned to the PC. Mtpserver is responsible for parsing commands from the PC and invoking the appropriate interface functions for processing.
    • The Java layer includes objects such as Usbreceiver, Mtpservice, Mtpserver, and so on. Where usbreceiver is used to monitor USB events to determine when to start or stop mtpservice. Mtpservice is responsible for initiating mtpserver and loading the storage device information to the database. Mtpserver is responsible for using the JNI interface to start/stop mtpserver in the C + + layer and to handle storage additions and deletions. Mediaprovider is responsible for querying and updating the database. Although the Mtpdatabase name is called database, the actual function is used to convert the data format between Mediaprovider and Mtpserver. For example, the information passed by MTP (such as file size, file path, etc.) is converted to the format required by Mediaprovider to facilitate its updating of the database.

Let's look at the MTP workflow.

2.2 MTP Process Analysis

Let's take a look at the MTP module start-up process, as shown in 1-6:

Figure 1-6 MTP Main module start-up process

The figure 1-6 shows that:

    • When the phone is connected to a USB cable, Usbreceiver receives a usb_state broadcast event from the system. It then needs to query the USB link state from the Usbmanager, the setup information of MTP and the setup information of PTP. When the user is set to use MTP mode, Usbreceiver will start mtpservice through the StartService function.
    • Mtpservice starts, and Mtpdatabase objects and Mtpserver objects are created in their onstartcommand.
    • Usbreceiver also by insert a special URI (value "content://media/none/mtp_connected"), Triggers the Bindservice function that Mdiaprovder calls Mtpservice. In this way, Mediaprovider and Mtpservice have established close ties.

Mtpserver is the core module for MTP protocol processing in the Android platform, which launches a single thread for receiving PC-side commands, as shown in code 1-7:

Figure 1-7 Mtpserver run function code snippet

As shown in Figure 1-7, Mtpserver constantly reads the request from the file descriptor and calls HandleRequest for processing. Finally, the processing results are returned to the peer.

From this code reader, it is found that the coupling between the Android MTP command layer and the physical layer is low, which also facilitates the future implementation of MTP/IP functionality.

Next we look at the processing flow of the PC-side send Sendobjectinfo, 1-8:

Figure 1-8 Sendobjectinfo processing flowchart

As shown in Figure 1-8, the sendobjectinfo process is outlined in the following steps:

    • PC sends Sendobjectinfo command to Mtpserver. Mtpserver need to check the remaining space on the storage device and the maximum file size that can be supported. If everything works, it will add a data item to the media database via the Mediaprovider insert function.
    • The PC then passes the contents of the file to Mtpserver via SendObject. The mtpserver creates the file and writes the data to the file.
    • When the file data is sent, Mtpserver calls Endsendobject. The EndObject will trigger Mediascanner to scan the media file. Of course, after the scan, the file carries the media information (if it is a MP3 file, the album information, singers, genres, length and other content) into the media database.

By describing the Sendobjectinfo, we can also see that Android takes full advantage of the features of its platform itself, really the media Transfer protocol and media files scanned just right to combine, thereby exerting the maximum effectiveness of MTP.

Three summary

This article mainly introduces the MTP in Android. Although the MTP protocol is provided by Microsoft, it is so widely used for historical reasons that Android also provides the most basic MTP implementations.

Of course, if you want to be really practical and through the Microsoft certification, handset manufacturers also need to do further development on this basis. Combined with the author's own experience, foreign big players such as Sony, Samsung, Nokia and other support for MTP is quite in place. In comparison, the domestic handset manufacturers start a little later, need to devote more energy to surpass. In addition, with the popularization of wireless technology, the implementation of MTP based on IP will also be of great use to users. I hope that we can work together at an early date to let users from the USB data cable free.

MTP in Android detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.