public final class MtpDevice extends Object
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.Constructor and Description |
---|
MtpDevice(UsbDevice device)
MtpClient constructor
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes all resources related to the MtpDevice object.
|
boolean |
deleteObject(int objectHandle)
Deletes an object on the device.
|
protected void |
finalize()
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
|
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
UsbDevice.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 |
getObjectSizeLong(int handle,
int format)
Returns object size in 64-bit integer.
|
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,
ParcelFileDescriptor descriptor)
Copies the data for an object to a file descriptor.
|
boolean |
importFile(int objectHandle,
String destPath)
Copies the data for an object to a file in external storage.
|
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.
|
public boolean open(UsbDeviceConnection connection)
UsbDeviceConnection
.
The connection will be closed when you call close()
The connection will also be closed if this method fails.connection
- an open UsbDeviceConnection
for the devicepublic void close()
open(android.hardware.usb.UsbDeviceConnection)
is called again
with a new UsbDeviceConnection
.protected void finalize() throws Throwable
Object
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.
public String getDeviceName()
UsbDevice.getDeviceName()
for the device's UsbDevice
public int getDeviceId()
UsbDevice.getDeviceId()
for the device's UsbDevice
public String toString()
Object
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())
public MtpDeviceInfo getDeviceInfo()
MtpDeviceInfo
for this devicepublic int[] getStorageIds()
getStorageInfo(int)
.public int[] getObjectHandles(int storageId, int format, int objectHandle)
getObjectInfo(int)
.storageId
- the storage unit to queryformat
- the format of the object to return, or zero for all formatsobjectHandle
- the parent object to query, -1 for the storage root,
or zero for all objectspublic byte[] getObject(int objectHandle, int objectSize)
objectHandle
- handle of the object to readobjectSize
- the size of the object (this should match
MtpObjectInfo.getCompressedSize()
)public long getPartialObject(int objectHandle, long offset, long size, byte[] buffer) throws IOException
objectHandle
- handle of the object to readoffset
- Start index of reading range. It must be a non-negative value at most
0xffffffff.size
- 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
- Array to write data.IOException
public long getPartialObject64(int objectHandle, long offset, long size, byte[] buffer) throws IOException
MtpDeviceInfo.getOperationsSupported()
.objectHandle
- handle of the object to readoffset
- Start index of reading range. It must be a non-negative value.size
- Size of reading range. It must be a non-negative value at most Integer.MAX_VALUE.buffer
- Array to write data.IOException
MtpConstants.OPERATION_GET_PARTIAL_OBJECT_64
public byte[] getThumbnail(int objectHandle)
MtpObjectInfo.getThumbCompressedSize()
and
MtpObjectInfo.getThumbFormat()
.
For typical devices the format is JPEG.objectHandle
- handle of the object to readpublic MtpStorageInfo getStorageInfo(int storageId)
MtpStorageInfo
for a storage unit.storageId
- the ID of the storage unitpublic MtpObjectInfo getObjectInfo(int objectHandle)
MtpObjectInfo
for an object.objectHandle
- the handle of the objectpublic boolean deleteObject(int objectHandle)
objectHandle
- handle of the object to deletepublic long getParent(int objectHandle)
objectHandle
- handle of the object to querypublic long getStorageId(int objectHandle)
objectHandle
- handle of the object to querypublic boolean importFile(int objectHandle, String destPath)
objectHandle
- handle of the object to readdestPath
- path to destination for the file transfer.
This path should be in the external storage as defined by
Environment.getExternalStorageDirectory()
public boolean importFile(int objectHandle, ParcelFileDescriptor descriptor)
objectHandle
- handle of the object to readdescriptor
- file descriptor to write the data to for the file transfer.public boolean sendObject(int objectHandle, long size, ParcelFileDescriptor descriptor)
objectHandle
- handle of the target filesize
- size of the file in bytesdescriptor
- file descriptor to read the data from.public MtpObjectInfo sendObjectInfo(MtpObjectInfo info)
MtpObjectInfo
can be
created with the MtpObjectInfo.Builder
class.
The returned MtpObjectInfo
has the new object handle field filled in.info
- metadata of the entrypublic MtpEvent readEvent(CancellationSignal signal) throws IOException
signal
- signal for cancellationIOException
public long getObjectSizeLong(int handle, int format) throws IOException
IOException