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 |
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 | |
---|---|
Public constructors
MtpDevice
public MtpDevice (UsbDevice device)
MtpClient constructor
Parameters | |
---|---|
device |
UsbDevice : the UsbDevice for the MTP or PTP device
This value cannot be null . |
Public methods
close
public 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
public 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
public int getDeviceId ()
Returns the USB ID of the USB device.
This returns the same value as UsbDevice.getDeviceId()
for the device's UsbDevice
Returns | |
---|---|
int |
the device ID |
getDeviceInfo
public MtpDeviceInfo getDeviceInfo ()
Returns the MtpDeviceInfo
for this device
Returns | |
---|---|
MtpDeviceInfo |
the device info, or null if fetching device info fails |
getDeviceName
public String getDeviceName ()
Returns the name of the USB device
This returns the same value as UsbDevice.getDeviceName()
for the device's UsbDevice
Returns | |
---|---|
String |
the device name
This value cannot be null . |
getObject
public 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
MtpObjectInfo#getCompressedSize ) |
Returns | |
---|---|
byte[] |
the object's data, or null if reading fails |
getObjectHandles
public 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, or null if fetching object handles fails |
getObjectInfo
public MtpObjectInfo getObjectInfo (int objectHandle)
Retrieves the MtpObjectInfo
for an object.
Parameters | |
---|---|
objectHandle |
int : the handle of the object |
Returns | |
---|---|
MtpObjectInfo |
the MtpObjectInfo, or null if fetching object info fails |
getParent
public 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
public 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.
This value cannot be null . |
Returns | |
---|---|
long |
Size of bytes that are actually read. |
Throws | |
---|---|
IOException |
getPartialObject64
public 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
MtpDeviceInfo#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.
This value cannot be null . |
Returns | |
---|---|
long |
Size of bytes that are actually read. |
Throws | |
---|---|
IOException |
getStorageId
public 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
public 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, or null if fetching storage IDs fails |
getStorageInfo
public MtpStorageInfo getStorageInfo (int storageId)
Retrieves the MtpStorageInfo
for a storage unit.
Parameters | |
---|---|
storageId |
int : the ID of the storage unit |
Returns | |
---|---|
MtpStorageInfo |
the MtpStorageInfo, or null if fetching storage info fails |
getThumbnail
public 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
MtpObjectInfo#getThumbCompressedSize
and
MtpObjectInfo#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
public 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
Environment.getExternalStorageDirectory()
This value cannot be null . |
Returns | |
---|---|
boolean |
true if the file transfer succeeds |
importFile
public 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.
This value cannot be null . |
Returns | |
---|---|
boolean |
true if the file transfer succeeds |
open
public 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
This value cannot be null . |
Returns | |
---|---|
boolean |
true if the device was successfully opened. |
readEvent
public 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
This value may be null . |
Returns | |
---|---|
MtpEvent |
obtained event
This value cannot be null . |
Throws | |
---|---|
|
java.io.IOException |
IOException |
sendObject
public 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.
This value cannot be null . |
Returns | |
---|---|
boolean |
true if the file transfer succeeds |
sendObjectInfo
public 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
This value cannot be null . |
Returns | |
---|---|
MtpObjectInfo |
object info of the created entry, or null if sending object info fails |
toString
public String toString ()
Returns a string representation of the object.
Returns | |
---|---|
String |
This value cannot be null . |
Protected methods
finalize
protected 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 Java 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 |