Skip to content

Most visited

Recently visited

navigation
Added in API level 12

MtpDevice

public final class MtpDevice
extends Object

java.lang.Object
   ↳ android.mtp.MtpDevice


This class represents an MTP or PTP device connected on the USB host bus. An application can instantiate an object of this type, by referencing an attached UsbDevice and then use methods in this class to get information about the device and objects stored on it, as well as open the connection and transfer data.

Summary

Public constructors

MtpDevice(UsbDevice device)

MtpClient constructor

Public methods

void close()

Closes all resources related to the MtpDevice object.

boolean deleteObject(int objectHandle)

Deletes an object on the device.

int getDeviceId()

Returns the USB ID of the USB device.

MtpDeviceInfo getDeviceInfo()

Returns the MtpDeviceInfo for this device

String getDeviceName()

Returns the name of the USB device This returns the same value as getDeviceName() for the device's UsbDevice

byte[] getObject(int objectHandle, int objectSize)

Returns the data for an object as a byte array.

int[] getObjectHandles(int storageId, int format, int objectHandle)

Returns the list of object handles for all objects on the given storage unit, with the given format and parent.

MtpObjectInfo getObjectInfo(int objectHandle)

Retrieves the MtpObjectInfo for an object.

long getParent(int objectHandle)

Retrieves the object handle for the parent of an object on the device.

long getPartialObject(int objectHandle, long offset, long size, byte[] buffer)

Obtains object bytes in the specified range and writes it to an array.

long getPartialObject64(int objectHandle, long offset, long size, byte[] buffer)

Obtains object bytes in the specified range and writes it to an array.

long getStorageId(int objectHandle)

Retrieves the ID of the storage unit containing the given object on the device.

int[] getStorageIds()

Returns the list of IDs for all storage units on this device Information about each storage unit can be accessed via getStorageInfo(int).

MtpStorageInfo getStorageInfo(int storageId)

Retrieves the MtpStorageInfo for a storage unit.

byte[] getThumbnail(int objectHandle)

Returns the thumbnail data for an object as a byte array.

boolean importFile(int objectHandle, String destPath)

Copies the data for an object to a file in external storage.

boolean importFile(int objectHandle, ParcelFileDescriptor descriptor)

Copies the data for an object to a file descriptor.

boolean open(UsbDeviceConnection connection)

Opens the MTP device.

MtpEvent readEvent(CancellationSignal signal)

Reads an event from the device.

boolean sendObject(int objectHandle, long size, ParcelFileDescriptor descriptor)

Copies the data for an object from a file descriptor.

MtpObjectInfo sendObjectInfo(MtpObjectInfo info)

Uploads an object metadata for a new entry.

String toString()

Returns a string representation of the object.

Protected methods

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Inherited methods

From class java.lang.Object

Public constructors

MtpDevice

Added in API level 12
MtpDevice (UsbDevice device)

MtpClient constructor

Parameters
device UsbDevice: the UsbDevice for the MTP or PTP device

Public methods

close

Added in API level 12
void close ()

Closes all resources related to the MtpDevice object. After this is called, the object can not be used until open(UsbDeviceConnection) is called again with a new UsbDeviceConnection.

deleteObject

Added in API level 12
boolean deleteObject (int objectHandle)

Deletes an object on the device. This call may block, since deleting a directory containing many files may take a long time on some devices.

Parameters
objectHandle int: handle of the object to delete
Returns
boolean true if the deletion succeeds

getDeviceId

Added in API level 12
int getDeviceId ()

Returns the USB ID of the USB device. This returns the same value as getDeviceId() for the device's UsbDevice

Returns
int the device ID

getDeviceInfo

Added in API level 12
MtpDeviceInfo getDeviceInfo ()

Returns the MtpDeviceInfo for this device

Returns
MtpDeviceInfo the device info

getDeviceName

Added in API level 12
String getDeviceName ()

Returns the name of the USB device This returns the same value as getDeviceName() for the device's UsbDevice

Returns
String the device name

getObject

Added in API level 12
byte[] getObject (int objectHandle, 
                int objectSize)

Returns the data for an object as a byte array. This call may block for an arbitrary amount of time depending on the size of the data and speed of the devices.

Parameters
objectHandle int: handle of the object to read
objectSize int: the size of the object (this should match getCompressedSize())
Returns
byte[] the object's data, or null if reading fails

getObjectHandles

Added in API level 12
int[] getObjectHandles (int storageId, 
                int format, 
                int objectHandle)

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 via getObjectInfo(int).

Parameters
storageId int: the storage unit to query
format int: the format of the object to return, or zero for all formats
objectHandle int: the parent object to query, -1 for the storage root, or zero for all objects
Returns
int[] the object handles

getObjectInfo

Added in API level 12
MtpObjectInfo getObjectInfo (int objectHandle)

Retrieves the MtpObjectInfo for an object.

Parameters
objectHandle int: the handle of the object
Returns
MtpObjectInfo the MtpObjectInfo

getParent

Added in API level 12
long getParent (int objectHandle)

Retrieves the object handle for the parent of an object on the device.

Parameters
objectHandle int: handle of the object to query
Returns
long the parent's handle, or zero if it is in the root of the storage

getPartialObject

Added in API level 24
long getPartialObject (int objectHandle, 
                long offset, 
                long size, 
                byte[] buffer)

Obtains object bytes in the specified range and writes it to an array. This call may block for an arbitrary amount of time depending on the size of the data and speed of the devices.

Parameters
objectHandle int: handle of the object to read
offset long: Start index of reading range. It must be a non-negative value at most 0xffffffff.
size long: Size of reading range. It must be a non-negative value at most Integer.MAX_VALUE or 0xffffffff. If 0xffffffff is specified, the method obtains the full bytes of object.
buffer byte: Array to write data.
Returns
long Size of bytes that are actually read.
Throws
IOException

getPartialObject64

Added in API level 24
long getPartialObject64 (int objectHandle, 
                long offset, 
                long size, 
                byte[] buffer)

Obtains object bytes in the specified range and writes it to an array. This call may block for an arbitrary amount of time depending on the size of the data and speed of the devices. This is a vender-extended operation supported by Android that enables us to pass unsigned 64-bit offset. Check if the MTP device supports the operation by using getOperationsSupported().

Parameters
objectHandle int: handle of the object to read
offset long: Start index of reading range. It must be a non-negative value.
size long: Size of reading range. It must be a non-negative value at most Integer.MAX_VALUE.
buffer byte: Array to write data.
Returns
long Size of bytes that are actually read.
Throws
IOException

See also:

getStorageId

Added in API level 12
long getStorageId (int objectHandle)

Retrieves the ID of the storage unit containing the given object on the device.

Parameters
objectHandle int: handle of the object to query
Returns
long the object's storage unit ID

getStorageIds

Added in API level 12
int[] getStorageIds ()

Returns the list of IDs for all storage units on this device Information about each storage unit can be accessed via getStorageInfo(int).

Returns
int[] the list of storage IDs

getStorageInfo

Added in API level 12
MtpStorageInfo getStorageInfo (int storageId)

Retrieves the MtpStorageInfo for a storage unit.

Parameters
storageId int: the ID of the storage unit
Returns
MtpStorageInfo the MtpStorageInfo

getThumbnail

Added in API level 12
byte[] getThumbnail (int objectHandle)

Returns the thumbnail data for an object as a byte array. The size and format of the thumbnail data can be determined via getThumbCompressedSize() and getThumbFormat(). For typical devices the format is JPEG.

Parameters
objectHandle int: handle of the object to read
Returns
byte[] the object's thumbnail, or null if reading fails

importFile

Added in API level 12
boolean importFile (int objectHandle, 
                String destPath)

Copies the data for an object to a file in external storage. This call may block for an arbitrary amount of time depending on the size of the data and speed of the devices.

Parameters
objectHandle int: handle of the object to read
destPath String: path to destination for the file transfer. This path should be in the external storage as defined by getExternalStorageDirectory()
Returns
boolean true if the file transfer succeeds

importFile

Added in API level 24
boolean importFile (int objectHandle, 
                ParcelFileDescriptor descriptor)

Copies the data for an object to a file descriptor. This call may block for an arbitrary amount of time depending on the size of the data and speed of the devices. The file descriptor is not closed on completion, and must be done by the caller.

Parameters
objectHandle int: handle of the object to read
descriptor ParcelFileDescriptor: file descriptor to write the data to for the file transfer.
Returns
boolean true if the file transfer succeeds

open

Added in API level 12
boolean open (UsbDeviceConnection connection)

Opens the MTP device. Once the device is open it takes ownership of the UsbDeviceConnection. The connection will be closed when you call close() The connection will also be closed if this method fails.

Parameters
connection UsbDeviceConnection: an open UsbDeviceConnection for the device
Returns
boolean true if the device was successfully opened.

readEvent

Added in API level 24
MtpEvent readEvent (CancellationSignal signal)

Reads an event from the device. It blocks the current thread until it gets an event. It throws OperationCanceledException if it is cancelled by signal.

Parameters
signal CancellationSignal: signal for cancellation
Returns
MtpEvent obtained event
Throws
IOException

sendObject

Added in API level 24
boolean sendObject (int objectHandle, 
                long size, 
                ParcelFileDescriptor descriptor)

Copies the data for an object from a file descriptor. This call may block for an arbitrary amount of time depending on the size of the data and speed of the devices. The file descriptor is not closed on completion, and must be done by the caller.

Parameters
objectHandle int: handle of the target file
size long: size of the file in bytes
descriptor ParcelFileDescriptor: file descriptor to read the data from.
Returns
boolean true if the file transfer succeeds

sendObjectInfo

Added in API level 24
MtpObjectInfo sendObjectInfo (MtpObjectInfo info)

Uploads an object metadata for a new entry. The MtpObjectInfo can be created with the MtpObjectInfo.Builder class. The returned MtpObjectInfo has the new object handle field filled in.

Parameters
info MtpObjectInfo: metadata of the entry
Returns
MtpObjectInfo object info of the created entry or null if the operation failed.

toString

Added in API level 12
String toString ()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Returns
String a string representation of the object.

Protected methods

finalize

Added in API level 12
void finalize ()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

Throws
Throwable
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.