Press on the image to return to the main documentation page.
MtpDevice
Written by Andrew Graham
This library provides APIs that let you interact directly with connected cameras and other devices using the PTP (Picture Transfer Protocol) subset of the MTP (Media Transfer Protocol) specification. Your application can transfer and delete files and metadata from an MTP device. You cannot however create or modify files or folders on the MTP device as the Android API does not implement this.
This library contains three objects MtpDevice2 represents an MTP or PTP device connected on the USB host bus. MtpConstants contains constants in the MTP and PTP specifications. MtpObjectInfo encapsulates information about an object on an MTP device.
Note that the name MtpDevice2 is chosen because a non-functional MtpDevice object exists in the USB library. This library will be merged with the USB library and replace the MtpDevice in a future release of Basic4android.
This library requires Android 3.1 or later, a device with host USB capability and an OTG cable to switch the device USB port to host mode.
The MtpConstants object contains constants in the MTP and PTP specifications. These are mostly self-explanatory about the format that they represent. Associations are MPT-speak for folders and directories. Abstract objects have no representation in the underlying file system.
The MtpDevice object represents a USB device that supports the Media Transfer Protocol (MTP) or the Picture Transfer Protocol (PTP). Methods in this class get information about the device and objects stored on it and enable transferring data and deleting objects from the device.
For operations that are likely to take some time and block the main thread both synchronous and asynchronous versions are available. The synchronous versions are simpler to use and may be appropriate for testing or for simple applications for use by a few users. Production quality applications that will be used in unknown conditions by many users should be structured to use the asynchronous versions which, while more difficult to use, will provide a better user experience when MTP devices with large numbers of objects are used.
Note that the name MtpDevice2 is chosen because a non-functional MtpDevice object exists in the USB library. This library will be merged with the USB library and replace the MtpDevice in a future release of Basic4android.
Events:
DeleteFinished(TaskID As Int, Result As Boolean) ' Result is True if the delete was successful DeleteError(TaskID As Int, ExceptionMessage As String) GetFinished(TaskID As Int, Data() As Byte) ' data is a Byte array containing the Object GetError(TaskID As Int, ExceptionMessage As String) GetHandlesFinished(TaskID As Int, Data() As Int) ' data is an Int array containing the Object handles GetHandlesError(TaskID As Int, ExceptionMessage As String) ImportFinished(TaskID As Int, Result As Boolean) ' Result is True if the delete was successful ImportError(TaskID As Int, ExceptionMessage As String)
Closes all resources related to the MtpDevice object. After this is called, this object can not be used until Open(UsbDeviceConnection) is called again with a new UsbDeviceConnection
DeleteObject (objecthandleAsInt) AsBoolean
Deletes an object on the device returning True on success. This may take an arbitrary amount of time depending on the size of the data and speed of the devices. Because of this DeleteObjectAsync is available to do the deletion on a separate thread. This synchronous version is made available for convenience and use in known predictable situations. Production quality applications should use the asynchronous version.
Deletes an object on the device returning True on success. This call may block, since deleting a directory containing many files may take a long time on some devices. Because of this the deletion is done on a separate thread.
If several deletes are running concurrently they will not necessarily terminate in same order as they were started. The TaskID parameter enables different deletes to be individually identified if required. Some devices may have limitations on the number of concurrent operations supported.
When the deletion ends the DeleteFinished event is raised returning the TaskID and True if the delete succeeded. If an Exception occurs the DeleteError event is raised returning the TaskID and exception message. The Exception is saved in LastException.
GetDeviceIdAsInt
Returns the USB ID of the USB device. This returns the same value as GetDeviceId for the device's UsbDevice object
Copies the data for an object to a Byte array and returns the array or null if the read fails. This may take an arbitrary amount of time depending on the size of the data and speed of the devices. Because of this GetObjectAsync is available to do the copying on a separate thread. This synchronous version is made available for convenience and use in known predictable situations. Production quality applications should use the asynchronous version.
Copies the data for an object to a Byte array and returns the array or null if the read fails. This may take an arbitrary amount of time depending on the size of the data and speed of the devices. Because of this the import is done on a separate thread.
If several imports are running concurrently they will not necessarily terminate in same order as they were started. The TaskID parameter enables different imports to be individually identified if required. Some devices may have limitations on the number of concurrent operations supported.
When the import ends the GetFinished event is raised returning the TaskID and the Byte array or null. If an Exception occurs the GetError event is raised returning the TaskID and exception message. The Exception is saved in LastException.
Returns the list of object handles for all objects on the given storage unit, with the given format and parent. Information about each object can be accessed by passing the return of GetObjectInfo(int) to the Initialize of an MtpObjectInfo.
This may take an arbitrary amount of time depending on the size of the data and speed of the devices. Because of this GetObjectHandlesAsync is available to do the reading on a separate thread. This synchronous version is made available for convenience and use in known predictable situations. Production quality applications should use the asynchronous version.
storageId - the storage unit to query format - the format from MtpConstants of the object to return, or zero for all formats. objectHandle - the parent object to query, or zero for the storage root.
When the storage root is specified all objects on the device seem to be returned. For any other parent only the direct children of that parent seem to be returned.
Returns the list of object handles for all objects on the given storage unit, with the given format and parent. Information about each object can be accessed by passing the return of GetObjectInfo(int) to the Initialize of an MtpObjectInfo. This may take an arbitrary amount of time depending on the size of the data and speed of the devices. Because of this the reading is done on a separate thread.
If several imports are running concurrently they will not necessarily terminate in same order as they were started. The TaskID parameter enables different imports to be individually identified if required. Some devices may have limitations on the number of concurrent operations supported.
storageId - the storage unit to query format - the format from MtpConstants of the object to return, or FORMAT_ALL (zero) for all formats. objectHandle - the handle of the parent object to query, or zero for the storage root.
When the storage root is specified all objects on the device seem to be returned. For any other parent only the direct children of that parent seem to be returned.
When the import ends the GetHandlesFinished event is raised returning the TaskID and the Int array or null. If an Exception occurs the GetHandlesError event is raised returning the TaskID and exception message. The Exception is saved in LastException.
Returns the version string for the storage unit specified.
Getthumbnail (objecthandleAsInt) AsByte()
Returns the thumbnail data for an object as a byte array. The size and format of the thumbnail data can be determined using MtpObjectInfo GetThumbCompressedSize and GetThumbFormat. For typical devices the format is JPEG.
Copies the data for an object to a file in the Android device external storage. This may take an arbitrary amount of time depending on the size of the data and speed of the devices. Because of this ImportFileAsync is available to do the copying on a separate thread. This synchronous version is made available for convenience and use in known predictable situations. Production quality applications should use the asynchronous version.
Copies the data for an object to a file in the Android device external storage. This may take an arbitrary amount of time depending on the size of the data and speed of the devices. Because of this the import is done on a separate thread.
If several imports are running concurrently they will not necessarily terminate in same order as they were started. The TaskID parameter enables different imports to be individually identified if required. Some devices may have limitations on the number of concurrent operations supported.
When the import ends the ImportFinished event is raised returning the TaskID and True if the import succeeded. If an Exception occurs the ImportError event is raised returning the TaskID and exception message. The Exception is saved in LastException.
Open (connectionAsUsbDeviceConnectionWrapper) AsBoolean
Opens the MTP device. Once the device is open it takes ownership of the UsbDeviceConnection. connection must be an open UsbDeviceConnection for the device. The connection will be closed when you call Close. The connection will also be closed if this method fails. Returns True of the device was successfully opened.
Unfortunately the Android MtpDevice API has no method of determining if a device supports MTP before opening it. The behaviour of invoking this Open method on a non-MTP device varies with the device. Some may appear to open correctly but will susequently throw NullPointerExceptions on access. With other devices Open will hang the application until Android offers to force close it.
The MtpObjectInfo object encapsulates information about an object on an MTP device. This corresponds to the ObjectInfo Dataset described in section 5.3.1 of the MTP specification.
Pass the return of MtpDevice.GetObjectInfo to the Initialize method to access details of an MTP Object.
Returns the association description for the MTP object. Will be zero objects that are not of format FORMAT_ASSOCIATION.
GetAssociationTypeAsInt
Returns the association type for the MTP object. Will be zero for objects that are not of format FORMAT_ASSOCIATION. For directories the association type is typically ASSOCIATION_TYPE_GENERIC_FOLDER.
GetCompressedSizeAsInt
Returns the size of the MTP object.
GetDateCreatedAsLong
Returns the creation date of the MTP object. The value is represented as milliseconds since January 1, 1970.
GetDateModifiedAsLong
Returns the modification date of the MTP object. The value is represented as milliseconds since January 1, 1970
GetFormatAsInt
Returns the format code for the MTP object.
GetImagePixDepthAsInt
Returns the depth of the MTP object in bits per pixel. Will be zero for non-image objects.
GetImagePixHeightAsInt
Returns the height of the MTP object in pixels. Will be zero for non-image objects.
GetImagePixWidthAsInt
Returns the width of the MTP object in pixels. Will be zero for non-image objects.
GetKeywordsAsString
Returns a comma separated list of keywords for the MTP object.
GetNameAsString
Returns the name of the MTP object. This will often be the file or folder name.
GetObjectHandleAsInt
Returns the object handle for the MTP object.
GetParentAsInt
Returns the object handle for the object's parent. Will be zero if the parent is the root directory of a storage unit.
GetProtectionStatusAsInt
Returns the protection status for the MTP object. Possible values are: PROTECTION_STATUS_NONE PROTECTION_STATUS_READ_ONLY PROTECTION_STATUS_NON_TRANSFERABLE_DATA
GetSequenceNumberAsInt
Returns the sequence number for the MTP object. This field is typically not used for MTP devices, but is sometimes used to define a sequence of photos on PTP cameras.
GetStorageIdAsInt
Returns the storage ID for the MTP object's storage unit.
GetThumbCompressedSizeAsInt
Returns the size of the MTP object's thumbnail. Will be zero for objects with no thumbnail.
GetThumbFormatAsInt
Returns the format code for the MTP object's thumbnail. Will be zero for objects with no thumbnail.
GetThumbPixHeightAsInt
Returns the height of the MTP object's thumbnail in pixels. Will be zero for objects with no thumbnail.
GetThumbPixWidthAsInt
Returns the width of the MTP object's thumbnail in pixels. Will be zero for objects with no thumbnail.