# MtpDevice

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Ctors](https://developer.android.com/reference/android/mtp/MtpDevice#pubctors)\|[Methods](https://developer.android.com/reference/android/mtp/MtpDevice#pubmethods)\|[Protected Methods](https://developer.android.com/reference/android/mtp/MtpDevice#promethods)\|[Inherited Methods](https://developer.android.com/reference/android/mtp/MtpDevice#inhmethods)  

# MtpDevice

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/mtp/MtpDevice "View this page in Kotlin")\|Java

`
public

final

class
MtpDevice
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|-----------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.mtp.MtpDevice |

<br />

*** ** * ** ***

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](https://developer.android.com/reference/android/hardware/usb/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](https://developer.android.com/reference/android/mtp/MtpDevice#MtpDevice(android.hardware.usb.UsbDevice))`(`[UsbDevice](https://developer.android.com/reference/android/hardware/usb/UsbDevice)` device) ` MtpClient constructor |

|                                                                                                                                                                                                                                   ### Public methods                                                                                                                                                                                                                                    ||
|-----------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void`                                                                                 | ` `[close](https://developer.android.com/reference/android/mtp/MtpDevice#close())`() ` Closes all resources related to the MtpDevice object.                                                                                                                                                                                                                                                   |
| ` boolean`                                                                              | ` `[deleteObject](https://developer.android.com/reference/android/mtp/MtpDevice#deleteObject(int))`(int objectHandle) ` Deletes an object on the device.                                                                                                                                                                                                                                       |
| ` int`                                                                                  | ` `[getDeviceId](https://developer.android.com/reference/android/mtp/MtpDevice#getDeviceId())`() ` Returns the USB ID of the USB device.                                                                                                                                                                                                                                                       |
| ` `[MtpDeviceInfo](https://developer.android.com/reference/android/mtp/MtpDeviceInfo)   | ` `[getDeviceInfo](https://developer.android.com/reference/android/mtp/MtpDevice#getDeviceInfo())`() ` Returns the[MtpDeviceInfo](https://developer.android.com/reference/android/mtp/MtpDeviceInfo)for this device                                                                                                                                                                            |
| ` `[String](https://developer.android.com/reference/java/lang/String)                   | ` `[getDeviceName](https://developer.android.com/reference/android/mtp/MtpDevice#getDeviceName())`() ` Returns the name of the USB device This returns the same value as[UsbDevice.getDeviceName()](https://developer.android.com/reference/android/hardware/usb/UsbDevice#getDeviceName())for the device's[UsbDevice](https://developer.android.com/reference/android/hardware/usb/UsbDevice) |
| ` byte[]`                                                                               | ` `[getObject](https://developer.android.com/reference/android/mtp/MtpDevice#getObject(int,%20int))`(int objectHandle, int objectSize) ` Returns the data for an object as a byte array.                                                                                                                                                                                                       |
| ` int[]`                                                                                | ` `[getObjectHandles](https://developer.android.com/reference/android/mtp/MtpDevice#getObjectHandles(int,%20int,%20int))`(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](https://developer.android.com/reference/android/mtp/MtpObjectInfo)   | ` `[getObjectInfo](https://developer.android.com/reference/android/mtp/MtpDevice#getObjectInfo(int))`(int objectHandle) ` Retrieves the[MtpObjectInfo](https://developer.android.com/reference/android/mtp/MtpObjectInfo)for an object.                                                                                                                                                        |
| ` long`                                                                                 | ` `[getParent](https://developer.android.com/reference/android/mtp/MtpDevice#getParent(int))`(int objectHandle) ` Retrieves the object handle for the parent of an object on the device.                                                                                                                                                                                                       |
| ` long`                                                                                 | ` `[getPartialObject](https://developer.android.com/reference/android/mtp/MtpDevice#getPartialObject(int,%20long,%20long,%20byte[]))`(int objectHandle, long offset, long size, byte[] buffer) ` Obtains object bytes in the specified range and writes it to an array.                                                                                                                        |
| ` long`                                                                                 | ` `[getPartialObject64](https://developer.android.com/reference/android/mtp/MtpDevice#getPartialObject64(int,%20long,%20long,%20byte[]))`(int objectHandle, long offset, long size, byte[] buffer) ` Obtains object bytes in the specified range and writes it to an array.                                                                                                                    |
| ` long`                                                                                 | ` `[getStorageId](https://developer.android.com/reference/android/mtp/MtpDevice#getStorageId(int))`(int objectHandle) ` Retrieves the ID of the storage unit containing the given object on the device.                                                                                                                                                                                        |
| ` int[]`                                                                                | ` `[getStorageIds](https://developer.android.com/reference/android/mtp/MtpDevice#getStorageIds())`() ` Returns the list of IDs for all storage units on this device Information about each storage unit can be accessed via[getStorageInfo(int)](https://developer.android.com/reference/android/mtp/MtpDevice#getStorageInfo(int)).                                                           |
| ` `[MtpStorageInfo](https://developer.android.com/reference/android/mtp/MtpStorageInfo) | ` `[getStorageInfo](https://developer.android.com/reference/android/mtp/MtpDevice#getStorageInfo(int))`(int storageId) ` Retrieves the[MtpStorageInfo](https://developer.android.com/reference/android/mtp/MtpStorageInfo)for a storage unit.                                                                                                                                                  |
| ` byte[]`                                                                               | ` `[getThumbnail](https://developer.android.com/reference/android/mtp/MtpDevice#getThumbnail(int))`(int objectHandle) ` Returns the thumbnail data for an object as a byte array.                                                                                                                                                                                                              |
| ` boolean`                                                                              | ` `[importFile](https://developer.android.com/reference/android/mtp/MtpDevice#importFile(int,%20java.lang.String))`(int objectHandle, `[String](https://developer.android.com/reference/java/lang/String)` destPath) ` Copies the data for an object to a file in external storage.                                                                                                            |
| ` boolean`                                                                              | ` `[importFile](https://developer.android.com/reference/android/mtp/MtpDevice#importFile(int,%20android.os.ParcelFileDescriptor))`(int objectHandle, `[ParcelFileDescriptor](https://developer.android.com/reference/android/os/ParcelFileDescriptor)` descriptor) ` Copies the data for an object to a file descriptor.                                                                       |
| ` boolean`                                                                              | ` `[open](https://developer.android.com/reference/android/mtp/MtpDevice#open(android.hardware.usb.UsbDeviceConnection))`(`[UsbDeviceConnection](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection)` connection) ` Opens the MTP device.                                                                                                                         |
| ` `[MtpEvent](https://developer.android.com/reference/android/mtp/MtpEvent)             | ` `[readEvent](https://developer.android.com/reference/android/mtp/MtpDevice#readEvent(android.os.CancellationSignal))`(`[CancellationSignal](https://developer.android.com/reference/android/os/CancellationSignal)` signal) ` Reads an event from the device.                                                                                                                                |
| ` boolean`                                                                              | ` `[sendObject](https://developer.android.com/reference/android/mtp/MtpDevice#sendObject(int,%20long,%20android.os.ParcelFileDescriptor))`(int objectHandle, long size, `[ParcelFileDescriptor](https://developer.android.com/reference/android/os/ParcelFileDescriptor)` descriptor) ` Copies the data for an object from a file descriptor.                                                  |
| ` `[MtpObjectInfo](https://developer.android.com/reference/android/mtp/MtpObjectInfo)   | ` `[sendObjectInfo](https://developer.android.com/reference/android/mtp/MtpDevice#sendObjectInfo(android.mtp.MtpObjectInfo))`(`[MtpObjectInfo](https://developer.android.com/reference/android/mtp/MtpObjectInfo)` info) ` Uploads an object metadata for a new entry.                                                                                                                         |
| ` `[String](https://developer.android.com/reference/java/lang/String)                   | ` `[toString](https://developer.android.com/reference/android/mtp/MtpDevice#toString())`() ` Returns a string representation of the object.                                                                                                                                                                                                                                                    |

|                                                                                                         ### Protected methods                                                                                                          ||
|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void` | ` `[finalize](https://developer.android.com/reference/android/mtp/MtpDevice#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](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Public constructors

### MtpDevice

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public MtpDevice (UsbDevice device)
```

MtpClient constructor

<br />

|                                                                                Parameters                                                                                 ||
|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `device` | `UsbDevice`: the[UsbDevice](https://developer.android.com/reference/android/hardware/usb/UsbDevice)for the MTP or PTP device This value cannot be`null`. <br /> |

## Public methods

### close

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void close ()
```

Closes all resources related to the MtpDevice object. After this is called, the object can not be used until[open(UsbDeviceConnection)](https://developer.android.com/reference/android/mtp/MtpDevice#open(android.hardware.usb.UsbDeviceConnection))is called again with a new[UsbDeviceConnection](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection).

<br />

### deleteObject

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

|                          Parameters                          ||
|----------------|----------------------------------------------|
| `objectHandle` | `int`: handle of the object to delete <br /> |

|                     Returns                     ||
|-----------|--------------------------------------|
| `boolean` | true if the deletion succeeds <br /> |

### getDeviceId

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public int getDeviceId ()
```

Returns the USB ID of the USB device. This returns the same value as[UsbDevice.getDeviceId()](https://developer.android.com/reference/android/hardware/usb/UsbDevice#getDeviceId())for the device's[UsbDevice](https://developer.android.com/reference/android/hardware/usb/UsbDevice)

<br />

|           Returns           ||
|-------|----------------------|
| `int` | the device ID <br /> |

### getDeviceInfo

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public MtpDeviceInfo getDeviceInfo ()
```

Returns the[MtpDeviceInfo](https://developer.android.com/reference/android/mtp/MtpDeviceInfo)for this device

<br />

|                                                                      Returns                                                                      ||
|------------------------------------------------------------------------------------|---------------------------------------------------------------|
| [MtpDeviceInfo](https://developer.android.com/reference/android/mtp/MtpDeviceInfo) | the device info, or null if fetching device info fails <br /> |

### getDeviceName

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String getDeviceName ()
```

Returns the name of the USB device This returns the same value as[UsbDevice.getDeviceName()](https://developer.android.com/reference/android/hardware/usb/UsbDevice#getDeviceName())for the device's[UsbDevice](https://developer.android.com/reference/android/hardware/usb/UsbDevice)

<br />

|                                                        Returns                                                         ||
|--------------------------------------------------------------------|----------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | the device name This value cannot be`null`. <br /> |

### getObject

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

|                                                                                            Parameters                                                                                            ||
|----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `objectHandle` | `int`: handle of the object to read <br />                                                                                                                                       |
| `objectSize`   | `int`: the size of the object (this should match[MtpObjectInfo.getCompressedSize](https://developer.android.com/reference/android/mtp/MtpObjectInfo#getCompressedSize())) <br /> |

|                           Returns                            ||
|----------|----------------------------------------------------|
| `byte[]` | the object's data, or null if reading fails <br /> |

### getObjectHandles

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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)](https://developer.android.com/reference/android/mtp/MtpDevice#getObjectInfo(int)).

<br />

|                                                 Parameters                                                 ||
|----------------|--------------------------------------------------------------------------------------------|
| `storageId`    | `int`: the storage unit to query <br />                                                    |
| `format`       | `int`: the format of the object to return, or zero for all formats <br />                  |
| `objectHandle` | `int`: the parent object to query, -1 for the storage root, or zero for all objects <br /> |

|                                   Returns                                    ||
|---------|---------------------------------------------------------------------|
| `int[]` | the object handles, or null if fetching object handles fails <br /> |

### getObjectInfo

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public MtpObjectInfo getObjectInfo (int objectHandle)
```

Retrieves the[MtpObjectInfo](https://developer.android.com/reference/android/mtp/MtpObjectInfo)for an object.

<br />

|                       Parameters                       ||
|----------------|----------------------------------------|
| `objectHandle` | `int`: the handle of the object <br /> |

|                                                                       Returns                                                                       ||
|------------------------------------------------------------------------------------|-----------------------------------------------------------------|
| [MtpObjectInfo](https://developer.android.com/reference/android/mtp/MtpObjectInfo) | the MtpObjectInfo, or null if fetching object info fails <br /> |

### getParent

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public long getParent (int objectHandle)
```

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

<br />

|                         Parameters                          ||
|----------------|---------------------------------------------|
| `objectHandle` | `int`: handle of the object to query <br /> |

|                                     Returns                                     ||
|--------|-------------------------------------------------------------------------|
| `long` | the parent's handle, or zero if it is in the root of the storage <br /> |

### getPartialObject

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

|                                                                                               Parameters                                                                                                ||
|----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `objectHandle` | `int`: handle of the object to read <br />                                                                                                                                              |
| `offset`       | `long`: Start index of reading range. It must be a non-negative value at most 0xffffffff. <br />                                                                                        |
| `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. <br /> |
| `buffer`       | `byte`: Array to write data. This value cannot be`null`. <br />                                                                                                                         |

|                       Returns                        ||
|--------|----------------------------------------------|
| `long` | Size of bytes that are actually read. <br /> |

|                                    Throws                                     ||
|----------------------------------------------------------------------------|---|
| [IOException](https://developer.android.com/reference/java/io/IOException) |   |

### getPartialObject64

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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()](https://developer.android.com/reference/android/mtp/MtpDeviceInfo#getOperationsSupported()).

<br />

|                                                    Parameters                                                    ||
|----------------|--------------------------------------------------------------------------------------------------|
| `objectHandle` | `int`: handle of the object to read <br />                                                       |
| `offset`       | `long`: Start index of reading range. It must be a non-negative value. <br />                    |
| `size`         | `long`: Size of reading range. It must be a non-negative value at most Integer.MAX_VALUE. <br /> |
| `buffer`       | `byte`: Array to write data. This value cannot be`null`. <br />                                  |

|                       Returns                        ||
|--------|----------------------------------------------|
| `long` | Size of bytes that are actually read. <br /> |

|                                    Throws                                     ||
|----------------------------------------------------------------------------|---|
| [IOException](https://developer.android.com/reference/java/io/IOException) |   |

**See also:**

- [MtpConstants.OPERATION_GET_PARTIAL_OBJECT_64](https://developer.android.com/reference/android/mtp/MtpConstants#OPERATION_GET_PARTIAL_OBJECT_64)  

### getStorageId

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public long getStorageId (int objectHandle)
```

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

<br />

|                         Parameters                          ||
|----------------|---------------------------------------------|
| `objectHandle` | `int`: handle of the object to query <br /> |

|                   Returns                   ||
|--------|-------------------------------------|
| `long` | the object's storage unit ID <br /> |

### getStorageIds

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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)](https://developer.android.com/reference/android/mtp/MtpDevice#getStorageInfo(int)).

<br />

|                                    Returns                                     ||
|---------|-----------------------------------------------------------------------|
| `int[]` | the list of storage IDs, or null if fetching storage IDs fails <br /> |

### getStorageInfo

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public MtpStorageInfo getStorageInfo (int storageId)
```

Retrieves the[MtpStorageInfo](https://developer.android.com/reference/android/mtp/MtpStorageInfo)for a storage unit.

<br />

|                      Parameters                       ||
|-------------|------------------------------------------|
| `storageId` | `int`: the ID of the storage unit <br /> |

|                                                                         Returns                                                                         ||
|--------------------------------------------------------------------------------------|-------------------------------------------------------------------|
| [MtpStorageInfo](https://developer.android.com/reference/android/mtp/MtpStorageInfo) | the MtpStorageInfo, or null if fetching storage info fails <br /> |

### getThumbnail

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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](https://developer.android.com/reference/android/mtp/MtpObjectInfo#getThumbCompressedSize())and[MtpObjectInfo.getThumbFormat](https://developer.android.com/reference/android/mtp/MtpObjectInfo#getThumbFormat()). For typical devices the format is JPEG.

<br />

|                         Parameters                         ||
|----------------|--------------------------------------------|
| `objectHandle` | `int`: handle of the object to read <br /> |

|                              Returns                              ||
|----------|---------------------------------------------------------|
| `byte[]` | the object's thumbnail, or null if reading fails <br /> |

### importFile

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

|                                                                                                                                                Parameters                                                                                                                                                 ||
|----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `objectHandle` | `int`: handle of the object to read <br />                                                                                                                                                                                                                                                |
| `destPath`     | `String`: path to destination for the file transfer. This path should be in the external storage as defined by[Environment.getExternalStorageDirectory()](https://developer.android.com/reference/android/os/Environment#getExternalStorageDirectory())This value cannot be`null`. <br /> |

|                       Returns                        ||
|-----------|-------------------------------------------|
| `boolean` | true if the file transfer succeeds <br /> |

### importFile

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

|                                                               Parameters                                                               ||
|----------------|------------------------------------------------------------------------------------------------------------------------|
| `objectHandle` | `int`: handle of the object to read <br />                                                                             |
| `descriptor`   | `ParcelFileDescriptor`: file descriptor to write the data to for the file transfer. This value cannot be`null`. <br /> |

|                       Returns                        ||
|-----------|-------------------------------------------|
| `boolean` | true if the file transfer succeeds <br /> |

### open

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean open (UsbDeviceConnection connection)
```

Opens the MTP device. Once the device is open it takes ownership of the[UsbDeviceConnection](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection). The connection will be closed when you call[close()](https://developer.android.com/reference/android/mtp/MtpDevice#close())The connection will also be closed if this method fails.

<br />

|                                                                                              Parameters                                                                                              ||
|--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `connection` | `UsbDeviceConnection`: an open[UsbDeviceConnection](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection)for the device This value cannot be`null`. <br /> |

|                            Returns                            ||
|-----------|----------------------------------------------------|
| `boolean` | true if the device was successfully opened. <br /> |

### readEvent

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

|                                       Parameters                                        ||
|----------|-------------------------------------------------------------------------------|
| `signal` | `CancellationSignal`: signal for cancellation This value may be`null`. <br /> |

|                                                           Returns                                                           ||
|--------------------------------------------------------------------------|---------------------------------------------------|
| [MtpEvent](https://developer.android.com/reference/android/mtp/MtpEvent) | obtained event This value cannot be`null`. <br /> |

|                                             Throws                                              ||
|----------------------------------------------------------------------------|---------------------|
|                                                                            | java.io.IOException |
| [IOException](https://developer.android.com/reference/java/io/IOException) |                     |

### sendObject

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

|                                                    Parameters                                                     ||
|----------------|---------------------------------------------------------------------------------------------------|
| `objectHandle` | `int`: handle of the target file <br />                                                           |
| `size`         | `long`: size of the file in bytes <br />                                                          |
| `descriptor`   | `ParcelFileDescriptor`: file descriptor to read the data from. This value cannot be`null`. <br /> |

|                       Returns                        ||
|-----------|-------------------------------------------|
| `boolean` | true if the file transfer succeeds <br /> |

### sendObjectInfo

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public MtpObjectInfo sendObjectInfo (MtpObjectInfo info)
```

Uploads an object metadata for a new entry. The[MtpObjectInfo](https://developer.android.com/reference/android/mtp/MtpObjectInfo)can be created with the[MtpObjectInfo.Builder](https://developer.android.com/reference/android/mtp/MtpObjectInfo.Builder)class. The returned[MtpObjectInfo](https://developer.android.com/reference/android/mtp/MtpObjectInfo)has the new object handle field filled in.

<br />

|                                    Parameters                                     ||
|--------|---------------------------------------------------------------------------|
| `info` | `MtpObjectInfo`: metadata of the entry This value cannot be`null`. <br /> |

|                                                                              Returns                                                                              ||
|------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|
| [MtpObjectInfo](https://developer.android.com/reference/android/mtp/MtpObjectInfo) | object info of the created entry, or null if sending object info fails <br /> |

### toString

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String toString ()
```

Returns a string representation of the object.

<br />

|                                                Returns                                                 ||
|--------------------------------------------------------------------|------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | This value cannot be`null`. <br /> |

## Protected methods

### finalize

Added in[API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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.

<br />

|                                   Throws                                    ||
|--------------------------------------------------------------------------|---|
| [Throwable](https://developer.android.com/reference/java/lang/Throwable) |   |