Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Nested Classes](https://developer.android.com/reference/android/os/ParcelFileDescriptor#nestedclasses) \| [Constants](https://developer.android.com/reference/android/os/ParcelFileDescriptor#constants) \| [Inherited Constants](https://developer.android.com/reference/android/os/ParcelFileDescriptor#inhconstants) \| [Fields](https://developer.android.com/reference/android/os/ParcelFileDescriptor#lfields) \| [Ctors](https://developer.android.com/reference/android/os/ParcelFileDescriptor#pubctors) \| [Methods](https://developer.android.com/reference/android/os/ParcelFileDescriptor#pubmethods) \| [Protected Methods](https://developer.android.com/reference/android/os/ParcelFileDescriptor#promethods) \| [Inherited Methods](https://developer.android.com/reference/android/os/ParcelFileDescriptor#inhmethods)

# ParcelFileDescriptor

*** ** * ** ***

` public class ParcelFileDescriptor `   
` extends https://developer.android.com/reference/java/lang/Object ` ` implements https://developer.android.com/reference/java/io/Closeable, https://developer.android.com/reference/android/os/Parcelable `

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

<br />

*** ** * ** ***

The FileDescriptor returned by `https://developer.android.com/reference/android/os/Parcel#readFileDescriptor()`, allowing you to close it when done with it.

## Summary

| ### Nested classes ||
|---|---|
| ` class` | `https://developer.android.com/reference/android/os/ParcelFileDescriptor.AutoCloseInputStream` An InputStream you can create on a ParcelFileDescriptor, which will take care of calling `https://developer.android.com/reference/android/os/ParcelFileDescriptor#close()` for you when the stream is closed. |
| ` class` | `https://developer.android.com/reference/android/os/ParcelFileDescriptor.AutoCloseOutputStream` An OutputStream you can create on a ParcelFileDescriptor, which will take care of calling `https://developer.android.com/reference/android/os/ParcelFileDescriptor#close()` for you when the stream is closed. |
| ` class` | `https://developer.android.com/reference/android/os/ParcelFileDescriptor.FileDescriptorDetachedException` Exception that indicates that the file descriptor was detached. |
| ` interface` | `https://developer.android.com/reference/android/os/ParcelFileDescriptor.OnCloseListener` Callback indicating that a ParcelFileDescriptor has been closed. |

| ### Constants ||
|---|---|
| `int` | `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_APPEND` For use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`: append to end of file while writing. |
| `int` | `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_CREATE` For use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`: create the file if it doesn't already exist. |
| `int` | `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_READ_ONLY` For use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`: open the file with read-only access. |
| `int` | `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_READ_WRITE` For use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`: open the file with read and write access. |
| `int` | `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_TRUNCATE` For use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`: erase contents of file when opening. |
| `int` | `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_WORLD_READABLE` *This constant was deprecated in API level 19. Creating world-readable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, applications should use more formal mechanism for interactions such as `https://developer.android.com/reference/android/content/ContentProvider`, `https://developer.android.com/reference/android/content/BroadcastReceiver`, and `https://developer.android.com/reference/android/app/Service`. There are no guarantees that this access mode will remain on a file, such as when it goes through a backup and restore.* |
| `int` | `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_WORLD_WRITEABLE` *This constant was deprecated in API level 19. Creating world-writable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, applications should use more formal mechanism for interactions such as `https://developer.android.com/reference/android/content/ContentProvider`, `https://developer.android.com/reference/android/content/BroadcastReceiver`, and `https://developer.android.com/reference/android/app/Service`. There are no guarantees that this access mode will remain on a file, such as when it goes through a backup and restore.* |
| `int` | `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_WRITE_ONLY` For use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`: open the file with write-only access. |

### Inherited constants

From interface `https://developer.android.com/reference/android/os/Parcelable` `int` `https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR`

Descriptor bit used with `https://developer.android.com/reference/android/os/Parcelable#describeContents()`: indicates that the Parcelable object's flattened representation includes a file descriptor.
`int` `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE`

Flag for use with `https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)`: the object being written is a return value, that is the result of a function such as "`Parcelable someFunction()`", "`void someFunction(out Parcelable)`", or "`void someFunction(inout Parcelable)`".

| ### Fields ||
|---|---|
| ` public static final https://developer.android.com/reference/android/os/Parcelable.Creator<https://developer.android.com/reference/android/os/ParcelFileDescriptor>` | `https://developer.android.com/reference/android/os/ParcelFileDescriptor#CREATOR` <br /> |

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#ParcelFileDescriptor(android.os.ParcelFileDescriptor)(https://developer.android.com/reference/android/os/ParcelFileDescriptor wrapped) ` Create a new ParcelFileDescriptor wrapped around another descriptor. |

| ### Public methods ||
|---|---|
| ` static https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#adoptFd(int)(int fd) ` Take ownership of a raw native fd in to a new ParcelFileDescriptor. |
| ` boolean` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#canDetectErrors()() ` Indicates if this ParcelFileDescriptor can communicate and detect remote errors/crashes. |
| ` void` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#checkError()() ` Detect and throw if the other end of a pipe or socket pair encountered an error or crashed. |
| ` void` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#close()() ` Close the ParcelFileDescriptor. |
| ` void` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#closeWithError(java.lang.String)(https://developer.android.com/reference/java/lang/String msg) ` Close the ParcelFileDescriptor, informing any peer that an error occurred while processing. |
| ` static https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#createPipe()() ` Create two ParcelFileDescriptors structured as a data pipe. |
| ` static https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#createReliablePipe()() ` Create two ParcelFileDescriptors structured as a data pipe. |
| ` static https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#createReliableSocketPair()() ` Create two ParcelFileDescriptors structured as a pair of sockets connected to each other. |
| ` static https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#createSocketPair()() ` Create two ParcelFileDescriptors structured as a pair of sockets connected to each other. |
| ` int` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#describeContents()() ` Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. |
| ` int` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#detachFd()() ` Return the native fd int for this ParcelFileDescriptor and detach it from the object here. |
| ` static https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#dup(java.io.FileDescriptor)(https://developer.android.com/reference/java/io/FileDescriptor orig) ` Create a new ParcelFileDescriptor that is a dup of an existing FileDescriptor. |
| ` https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#dup()() ` Create a new ParcelFileDescriptor that is a dup of the existing FileDescriptor. |
| ` static https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#fromDatagramSocket(java.net.DatagramSocket)(https://developer.android.com/reference/java/net/DatagramSocket datagramSocket) ` Create a new ParcelFileDescriptor from the specified DatagramSocket. |
| ` static https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#fromFd(int)(int fd) ` Create a new ParcelFileDescriptor from a raw native fd. |
| ` static https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#fromSocket(java.net.Socket)(https://developer.android.com/reference/java/net/Socket socket) ` Create a new ParcelFileDescriptor from the specified Socket. |
| ` int` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#getFd()() ` Return the native fd int for this ParcelFileDescriptor. |
| ` https://developer.android.com/reference/java/io/FileDescriptor` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#getFileDescriptor()() ` Retrieve the actual FileDescriptor associated with this object. |
| ` long` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#getStatSize()() ` Return the total size of the file representing this fd, as determined by `stat()`. |
| ` static https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)(https://developer.android.com/reference/java/io/File file, int mode) ` Create a new ParcelFileDescriptor accessing a given file. |
| ` static https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int,%20android.os.Handler,%20android.os.ParcelFileDescriptor.OnCloseListener)(https://developer.android.com/reference/java/io/File file, int mode, https://developer.android.com/reference/android/os/Handler handler, https://developer.android.com/reference/android/os/ParcelFileDescriptor.OnCloseListener listener) ` Create a new ParcelFileDescriptor accessing a given file. |
| ` static int` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#parseMode(java.lang.String)(https://developer.android.com/reference/java/lang/String mode) ` Converts a string representing a file mode, such as "rw", into a bitmask suitable for use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#toString()() ` Returns a string representation of the object. |
| ` static https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#wrap(android.os.ParcelFileDescriptor,%20android.os.Handler,%20android.os.ParcelFileDescriptor.OnCloseListener)(https://developer.android.com/reference/android/os/ParcelFileDescriptor pfd, https://developer.android.com/reference/android/os/Handler handler, https://developer.android.com/reference/android/os/ParcelFileDescriptor.OnCloseListener listener) ` Create a new ParcelFileDescriptor wrapping an already-opened file. |
| ` void` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#writeToParcel(android.os.Parcel,%20int)(https://developer.android.com/reference/android/os/Parcel out, int flags) ` Flatten this object in to a Parcel. If `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE` is set in flags, the file descriptor will be closed after a copy is written to the Parcel. |

| ### Protected methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/os/ParcelFileDescriptor#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 ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` 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 https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` 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` ` 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` ` 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*.
From interface ` https://developer.android.com/reference/java/io/Closeable ` ` abstract void` ` https://developer.android.com/reference/java/io/Closeable#close()() `

Closes this stream and releases any system resources associated with it.
From interface ` https://developer.android.com/reference/android/os/Parcelable ` ` abstract int` ` https://developer.android.com/reference/android/os/Parcelable#describeContents()() `

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.
` abstract void` ` https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)(https://developer.android.com/reference/android/os/Parcel dest, int flags) `

Flatten this object in to a Parcel.
From interface ` https://developer.android.com/reference/java/lang/AutoCloseable ` ` abstract void` ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() `

Closes this resource, relinquishing any underlying resources.

## Constants

### MODE_APPEND

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

```
public static final int MODE_APPEND
```

For use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`: append to end of file while writing.

<br />

Constant Value: 33554432 (0x02000000)

### MODE_CREATE

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

```
public static final int MODE_CREATE
```

For use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`: create the file if it doesn't already exist.

<br />

Constant Value: 134217728 (0x08000000)

### MODE_READ_ONLY

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

```
public static final int MODE_READ_ONLY
```

For use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`: open the file with read-only access.

<br />

Constant Value: 268435456 (0x10000000)

### MODE_READ_WRITE

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

```
public static final int MODE_READ_WRITE
```

For use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`: open the file with read and write access.

<br />

Constant Value: 805306368 (0x30000000)

### MODE_TRUNCATE

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

```
public static final int MODE_TRUNCATE
```

For use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`: erase contents of file when opening.

<br />

Constant Value: 67108864 (0x04000000)

### MODE_WORLD_READABLE

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

```
public static final int MODE_WORLD_READABLE
```

<br />

**This constant was deprecated in API level 19.**   

Creating world-readable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, applications should use more formal mechanism for interactions such as `https://developer.android.com/reference/android/content/ContentProvider`, `https://developer.android.com/reference/android/content/BroadcastReceiver`, and `https://developer.android.com/reference/android/app/Service`. There are no guarantees that this access mode will remain on a file, such as when it goes through a backup and restore.

For use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`: if `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_CREATE` has been supplied and this file doesn't already exist, then create the file with permissions such that any application can read it.

<br />

Constant Value: 1 (0x00000001)

### MODE_WORLD_WRITEABLE

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

```
public static final int MODE_WORLD_WRITEABLE
```

<br />

**This constant was deprecated in API level 19.**   

Creating world-writable files is very dangerous, and likely to cause security holes in applications. It is strongly discouraged; instead, applications should use more formal mechanism for interactions such as `https://developer.android.com/reference/android/content/ContentProvider`, `https://developer.android.com/reference/android/content/BroadcastReceiver`, and `https://developer.android.com/reference/android/app/Service`. There are no guarantees that this access mode will remain on a file, such as when it goes through a backup and restore.

For use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`: if `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_CREATE` has been supplied and this file doesn't already exist, then create the file with permissions such that any application can write it.

<br />

Constant Value: 2 (0x00000002)

### MODE_WRITE_ONLY

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

```
public static final int MODE_WRITE_ONLY
```

For use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`: open the file with write-only access.

<br />

Constant Value: 536870912 (0x20000000)

## Fields

### CREATOR

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

```
public static final Creator<ParcelFileDescriptor> CREATOR
```

<br />

<br />

## Public constructors

### ParcelFileDescriptor

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

```
public ParcelFileDescriptor (ParcelFileDescriptor wrapped)
```

Create a new ParcelFileDescriptor wrapped around another descriptor. By default all method calls are delegated to the wrapped descriptor.

<br />

| Parameters ||
|---|---|
| `wrapped` | `ParcelFileDescriptor` <br /> |

## Public methods

### adoptFd

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

```
public static ParcelFileDescriptor adoptFd (int fd)
```

Take ownership of a raw native fd in to a new ParcelFileDescriptor. The returned ParcelFileDescriptor now owns the given fd, and will be responsible for closing it.

**WARNING:** You must not close the fd yourself after this call, and ownership of the file descriptor must have been released prior to the call to this function.

<br />

| Parameters ||
|---|---|
| `fd` | `int`: The native fd that the ParcelFileDescriptor should adopt. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | Returns a new ParcelFileDescriptor holding a FileDescriptor for the given fd. <br /> |

### canDetectErrors

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

```
public boolean canDetectErrors ()
```

Indicates if this ParcelFileDescriptor can communicate and detect remote errors/crashes.

<br />

| Returns ||
|---|---|
| `boolean` | <br /> |

**See also:**

- `https://developer.android.com/reference/android/os/ParcelFileDescriptor#checkError()`

### checkError

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

```
public void checkError ()
```

Detect and throw if the other end of a pipe or socket pair encountered an error or crashed. This allows a reader to distinguish between a valid EOF and an error/crash.

If this ParcelFileDescriptor is unable to detect remote errors, it will return silently.

<br />

| Throws ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor.FileDescriptorDetachedException` | if the remote side called `https://developer.android.com/reference/android/os/ParcelFileDescriptor#detachFd()`. Once detached, the remote side is unable to communicate any errors through `https://developer.android.com/reference/android/os/ParcelFileDescriptor#closeWithError(java.lang.String)`. |
| `https://developer.android.com/reference/java/io/IOException` | for normal errors. |

**See also:**

- `https://developer.android.com/reference/android/os/ParcelFileDescriptor#canDetectErrors()`

### close

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

```
public void close ()
```

Close the ParcelFileDescriptor. This implementation closes the underlying OS resources allocated to represent this stream.

<br />

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | If an error occurs attempting to close this ParcelFileDescriptor. |

### closeWithError

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

```
public void closeWithError (String msg)
```

Close the ParcelFileDescriptor, informing any peer that an error occurred while processing. If the creator of this descriptor is not observing errors, it will close normally.

<br />

| Parameters ||
|---|---|
| `msg` | `String`: describing the error; must not be null. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### createPipe

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

```
public static ParcelFileDescriptor[] createPipe ()
```

Create two ParcelFileDescriptors structured as a data pipe. The first ParcelFileDescriptor in the returned array is the read side; the second is the write side.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### createReliablePipe

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

```
public static ParcelFileDescriptor[] createReliablePipe ()
```

Create two ParcelFileDescriptors structured as a data pipe. The first ParcelFileDescriptor in the returned array is the read side; the second is the write side.

The write end has the ability to deliver an error message through `https://developer.android.com/reference/android/os/ParcelFileDescriptor#closeWithError(java.lang.String)` which can be handled by the read end calling `https://developer.android.com/reference/android/os/ParcelFileDescriptor#checkError()`, usually after detecting an EOF. This can also be used to detect remote crashes.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### createReliableSocketPair

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

```
public static ParcelFileDescriptor[] createReliableSocketPair ()
```

Create two ParcelFileDescriptors structured as a pair of sockets connected to each other. The two sockets are indistinguishable.

Both ends have the ability to deliver an error message through `https://developer.android.com/reference/android/os/ParcelFileDescriptor#closeWithError(java.lang.String)` which can be detected by the other end calling `https://developer.android.com/reference/android/os/ParcelFileDescriptor#checkError()`, usually after detecting an EOF. This can also be used to detect remote crashes.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### createSocketPair

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

```
public static ParcelFileDescriptor[] createSocketPair ()
```

Create two ParcelFileDescriptors structured as a pair of sockets connected to each other. The two sockets are indistinguishable.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### describeContents

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

```
public int describeContents ()
```

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of `https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)`, the return value of this method must include the `https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR` bit.

<br />

| Returns ||
|---|---|
| `int` | a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either `0` or - `https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR` |

### detachFd

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

```
public int detachFd ()
```

Return the native fd int for this ParcelFileDescriptor and detach it from the object here. You are now responsible for closing the fd in native code.

You should not detach when the original creator of the descriptor is expecting a reliable signal through `https://developer.android.com/reference/android/os/ParcelFileDescriptor#close()` or `https://developer.android.com/reference/android/os/ParcelFileDescriptor#closeWithError(java.lang.String)`.

<br />

| Returns ||
|---|---|
| `int` | <br /> |

**See also:**

- `https://developer.android.com/reference/android/os/ParcelFileDescriptor#canDetectErrors()`

### dup

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

```
public static ParcelFileDescriptor dup (FileDescriptor orig)
```

Create a new ParcelFileDescriptor that is a dup of an existing FileDescriptor. This obeys standard POSIX semantics, where the new file descriptor shared state such as file position with the original file descriptor.

<br />

| Parameters ||
|---|---|
| `orig` | `FileDescriptor` <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### dup

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

```
public ParcelFileDescriptor dup ()
```

Create a new ParcelFileDescriptor that is a dup of the existing FileDescriptor. This obeys standard POSIX semantics, where the new file descriptor shared state such as file position with the original file descriptor.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### fromDatagramSocket

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

```
public static ParcelFileDescriptor fromDatagramSocket (DatagramSocket datagramSocket)
```

Create a new ParcelFileDescriptor from the specified DatagramSocket. The new ParcelFileDescriptor holds a dup of the original FileDescriptor in the DatagramSocket, so you must still close the DatagramSocket as well as the new ParcelFileDescriptor.

**WARNING:** Prior to API level 29, this function would not actually dup the DatagramSocket's FileDescriptor, and would take a reference to the its internal FileDescriptor instead. If the DatagramSocket gets garbage collected before the ParcelFileDescriptor, this may lead to the ParcelFileDescriptor being unexpectedly closed. To avoid this, the following pattern can be used:

    ParcelFileDescriptor pfd = ParcelFileDescriptor.fromDatagramSocket(socket).dup();

<br />

<br />

| Parameters ||
|---|---|
| `datagramSocket` | `DatagramSocket`: The DatagramSocket whose FileDescriptor is used to create a new ParcelFileDescriptor. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | A new ParcelFileDescriptor with a duped copy of the FileDescriptor of the specified Socket. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/UncheckedIOException` | if `https://developer.android.com/reference/android/os/ParcelFileDescriptor#dup(java.io.FileDescriptor)` throws IOException. |

### fromFd

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

```
public static ParcelFileDescriptor fromFd (int fd)
```

Create a new ParcelFileDescriptor from a raw native fd. The new ParcelFileDescriptor holds a dup of the original fd passed in here, so you must still close that fd as well as the new ParcelFileDescriptor.

<br />

| Parameters ||
|---|---|
| `fd` | `int`: The native fd that the ParcelFileDescriptor should dup. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | Returns a new ParcelFileDescriptor holding a FileDescriptor for a dup of the given fd. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### fromSocket

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

```
public static ParcelFileDescriptor fromSocket (Socket socket)
```

Create a new ParcelFileDescriptor from the specified Socket. The new ParcelFileDescriptor holds a dup of the original FileDescriptor in the Socket, so you must still close the Socket as well as the new ParcelFileDescriptor.

**WARNING:** Prior to API level 29, this function would not actually dup the Socket's FileDescriptor, and would take a reference to the its internal FileDescriptor instead. If the Socket gets garbage collected before the ParcelFileDescriptor, this may lead to the ParcelFileDescriptor being unexpectedly closed. To avoid this, the following pattern can be used:

    ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket).dup();

<br />

<br />

| Parameters ||
|---|---|
| `socket` | `Socket`: The Socket whose FileDescriptor is used to create a new ParcelFileDescriptor. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | A new ParcelFileDescriptor with a duped copy of the FileDescriptor of the specified Socket. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/UncheckedIOException` | if `https://developer.android.com/reference/android/os/ParcelFileDescriptor#dup(java.io.FileDescriptor)` throws IOException. |

### getFd

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

```
public int getFd ()
```

Return the native fd int for this ParcelFileDescriptor. The ParcelFileDescriptor still owns the fd, and it still must be closed through this API.

**WARNING:** Do not call close on the return value of this function or pass it to a function that assumes ownership of the fd.

<br />

| Returns ||
|---|---|
| `int` | <br /> |

### getFileDescriptor

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

```
public FileDescriptor getFileDescriptor ()
```

Retrieve the actual FileDescriptor associated with this object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/FileDescriptor` | Returns the FileDescriptor associated with this object. <br /> |

### getStatSize

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

```
public long getStatSize ()
```

Return the total size of the file representing this fd, as determined by `stat()`. Returns -1 if the fd is not a file.

<br />

| Returns ||
|---|---|
| `long` | <br /> |

### open

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

```
public static ParcelFileDescriptor open (File file, 
                int mode)
```

Create a new ParcelFileDescriptor accessing a given file.

This method should only be used for files that you have direct access to; if you'd like to work with files hosted outside your app, use an API like `https://developer.android.com/reference/android/content/ContentResolver#openFile(android.net.Uri,%20java.lang.String,%20android.os.CancellationSignal)`.

<br />

| Parameters ||
|---|---|
| `file` | `File`: The file to be opened. <br /> |
| `mode` | `int`: The desired access mode, must be one of `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_READ_ONLY`, `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_WRITE_ONLY`, or `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_READ_WRITE`; may also be any combination of `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_CREATE`, `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_TRUNCATE`, `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_WORLD_READABLE`, and `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_WORLD_WRITEABLE`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | a new ParcelFileDescriptor pointing to the given file. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/FileNotFoundException` | if the given file does not exist or can not be opened with the requested mode. |

**See also:**

- `https://developer.android.com/reference/android/os/ParcelFileDescriptor#parseMode(java.lang.String)`

### open

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

```
public static ParcelFileDescriptor open (File file, 
                int mode, 
                Handler handler, 
                ParcelFileDescriptor.OnCloseListener listener)
```

Create a new ParcelFileDescriptor accessing a given file.

This method should only be used for files that you have direct access to; if you'd like to work with files hosted outside your app, use an API like `https://developer.android.com/reference/android/content/ContentResolver#openFile(android.net.Uri,%20java.lang.String,%20android.os.CancellationSignal)`.

<br />

| Parameters ||
|---|---|
| `file` | `File`: The file to be opened. <br /> |
| `mode` | `int`: The desired access mode, must be one of `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_READ_ONLY`, `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_WRITE_ONLY`, or `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_READ_WRITE`; may also be any combination of `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_CREATE`, `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_TRUNCATE`, `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_WORLD_READABLE`, and `https://developer.android.com/reference/android/os/ParcelFileDescriptor#MODE_WORLD_WRITEABLE`. <br /> |
| `handler` | `Handler`: to call listener from; must not be null. <br /> |
| `listener` | `ParcelFileDescriptor.OnCloseListener`: to be invoked when the returned descriptor has been closed; must not be null. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | a new ParcelFileDescriptor pointing to the given file. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/FileNotFoundException` | if the given file does not exist or can not be opened with the requested mode. |
| `https://developer.android.com/reference/java/io/IOException` |   |

**See also:**

- `https://developer.android.com/reference/android/os/ParcelFileDescriptor#parseMode(java.lang.String)`

### parseMode

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

```
public static int parseMode (String mode)
```

Converts a string representing a file mode, such as "rw", into a bitmask suitable for use with `https://developer.android.com/reference/android/os/ParcelFileDescriptor#open(java.io.File,%20int)`.

The argument must define at least one of the following base access modes:

- "r" indicates the file should be opened in read-only mode, equivalent to `https://developer.android.com/reference/android/system/OsConstants#O_RDONLY`.
- "w" indicates the file should be opened in write-only mode, equivalent to `https://developer.android.com/reference/android/system/OsConstants#O_WRONLY`.
- "rw" indicates the file should be opened in read-write mode, equivalent to `https://developer.android.com/reference/android/system/OsConstants#O_RDWR`.

In addition to a base access mode, the following additional modes may requested:

<!-- -->

- "a" indicates the file should be opened in append mode, equivalent to `https://developer.android.com/reference/android/system/OsConstants#O_APPEND`. Before each write, the file offset is positioned at the end of the file.
- "t" indicates the file should be opened in truncate mode, equivalent to `https://developer.android.com/reference/android/system/OsConstants#O_TRUNC`. If the file already exists and is a regular file and is opened for writing, it will be truncated to length 0.

<br />

<br />

| Parameters ||
|---|---|
| `mode` | `String`: The string representation of the file mode. Can be "r", "w", "wt", "wa", "rw" or "rwt". <br /> |

| Returns ||
|---|---|
| `int` | A bitmask representing the given file mode. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the given string does not match a known file mode. |

### toString

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

```
public String toString ()
```

Returns a string representation of the object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a string representation of the object. <br /> |

### wrap

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

```
public static ParcelFileDescriptor wrap (ParcelFileDescriptor pfd, 
                Handler handler, 
                ParcelFileDescriptor.OnCloseListener listener)
```

Create a new ParcelFileDescriptor wrapping an already-opened file.

<br />

| Parameters ||
|---|---|
| `pfd` | `ParcelFileDescriptor`: The already-opened file. This value cannot be `null`. <br /> |
| `handler` | `Handler`: to call listener from. This value cannot be `null`. <br /> |
| `listener` | `ParcelFileDescriptor.OnCloseListener`: to be invoked when the returned descriptor has been closed. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | a new ParcelFileDescriptor pointing to the given file. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### writeToParcel

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

```
public void writeToParcel (Parcel out, 
                int flags)
```

Flatten this object in to a Parcel. If `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE` is set in flags, the file descriptor will be closed after a copy is written to the Parcel.

<br />

| Parameters ||
|---|---|
| `out` | `Parcel`: The Parcel in which the object should be written. This value cannot be `null`. <br /> |
| `flags` | `int`: Additional flags about how the object should be written. May be 0 or `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE`. Value is either `0` or a combination of the following: - `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE` |

## Protected methods

### finalize

Added in [API level 1](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 ||
|---|---|
| `https://developer.android.com/reference/java/lang/Throwable` |   |