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.
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 |
String
|
getDeviceName()
Returns the name of the USB device
This returns the same value as |
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 |
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 |
MtpStorageInfo
|
getStorageInfo(int storageId)
Retrieves the |
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
|
MtpDevice (UsbDevice device)
MtpClient constructor
Parameters | |
---|---|
device |
UsbDevice :
the UsbDevice for the MTP or PTP device
|
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
.
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 |
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 |
MtpDeviceInfo getDeviceInfo ()
Returns the MtpDeviceInfo
for this device
Returns | |
---|---|
MtpDeviceInfo |
the device info |
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 |
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 |
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 |
MtpObjectInfo getObjectInfo (int objectHandle)
Retrieves the MtpObjectInfo
for an object.
Parameters | |
---|---|
objectHandle |
int :
the handle of the object |
Returns | |
---|---|
MtpObjectInfo |
the MtpObjectInfo |
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 |
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 |
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:
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 |
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 |
MtpStorageInfo getStorageInfo (int storageId)
Retrieves the MtpStorageInfo
for a storage unit.
Parameters | |
---|---|
storageId |
int :
the ID of the storage unit |
Returns | |
---|---|
MtpStorageInfo |
the MtpStorageInfo |
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 |
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 |
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 |
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. |
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 |
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 |
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. |
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. |
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 |