# HeifWriter.Builder

Artifact: [androidx.heifwriter:heifwriter](https://developer.android.com/jetpack/androidx/releases/heifwriter) [View Source](https://cs.android.com/search?q=file:androidx/heifwriter/HeifWriter.java+class:androidx.heifwriter.HeifWriter.Builder) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/heifwriter#1.0.0)

*** ** * ** ***


```
public final class HeifWriter.Builder
```

<br />

*** ** * ** ***

Builder class for constructing a HeifWriter object from specified parameters.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder#Builder(java.io.FileDescriptor,int,int,int)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/io/FileDescriptor.html fd, @https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int width, @https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int height, int inputMode )` Construct a Builder with output specified by its file descriptor. |
| `https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder#Builder(java.lang.String,int,int,int)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html path, @https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int width, @https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int height, int inputMode )` Construct a Builder with output specified by its path. |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter` | `https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder#build()()` Build a HeifWriter object. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder` | `https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder#setEncoderPreference(androidx.heifwriter.EncoderPreference)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/EncoderPreference preference)` Sets the encoder preference for this builder. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder` | `https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder#setGridEnabled(boolean)(boolean gridEnabled)` Set whether to enable grid option. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder` | `https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder#setHandler(android.os.Handler)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Handler.html handler)` Provide a handler for the HeifWriter to use. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder` | `https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder#setMaxImages(int)(@https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int maxImages)` Set the maximum number of images to write. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder` | `https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder#setPrimaryIndex(int)(@https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int primaryIndex)` Set the primary image index. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder` | `https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder#setQuality(int)(@https://developer.android.com/reference/androidx/annotation/IntRange(from = 0, to = 100) int quality)` Set the quality for encoding images. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder` | `https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder#setRotation(int)(@https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int rotation)` Set the image rotation in degrees. |

## Public constructors

### Builder

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/heifwriter#1.0.0)

```
public Builder(
    @NonNull FileDescriptor fd,
    @IntRange(from = 1) int width,
    @IntRange(from = 1) int height,
    int inputMode
)
```

Construct a Builder with output specified by its file descriptor.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/io/FileDescriptor.html fd` | File descriptor of the file to be written. |
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int width` | Width of the image in number of pixels. |
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int height` | Height of the image in number of pixels. |
| `int inputMode` | Input mode for this writer, must be one of `https://developer.android.com/reference/androidx/heifwriter/HeifWriter#INPUT_MODE_BUFFER()`, `https://developer.android.com/reference/androidx/heifwriter/HeifWriter#INPUT_MODE_SURFACE()`, or `https://developer.android.com/reference/androidx/heifwriter/HeifWriter#INPUT_MODE_BITMAP()`. |

### Builder

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/heifwriter#1.0.0)

```
public Builder(
    @NonNull String path,
    @IntRange(from = 1) int width,
    @IntRange(from = 1) int height,
    int inputMode
)
```

Construct a Builder with output specified by its path.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html path` | Path of the file to be written. |
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int width` | Width of the image in number of pixels. |
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int height` | Height of the image in number of pixels. |
| `int inputMode` | Input mode for this writer, must be one of `https://developer.android.com/reference/androidx/heifwriter/HeifWriter#INPUT_MODE_BUFFER()`, `https://developer.android.com/reference/androidx/heifwriter/HeifWriter#INPUT_MODE_SURFACE()`, or `https://developer.android.com/reference/androidx/heifwriter/HeifWriter#INPUT_MODE_BITMAP()`. |

## Public methods

### build

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/heifwriter#1.0.0)

```
public @NonNull HeifWriter build()
```

Build a HeifWriter object.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter` | a HeifWriter object built according to the specifications. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | if failed to create the writer, possibly due to failure to create `https://developer.android.com/reference/android/media/MediaMuxer.html` or `https://developer.android.com/reference/android/media/MediaCodec.html`. |

### setEncoderPreference

Added in [1.2.0-rc02](https://developer.android.com/jetpack/androidx/releases/heifwriter#1.2.0-rc02)

```
public @NonNull HeifWriter.Builder setEncoderPreference(@NonNull EncoderPreference preference)
```

Sets the encoder preference for this builder.

This method allows you to configure the desired encoding type (hardware or software) and the bitrate mode (e.g., constant quality).

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/EncoderPreference preference` | The non-null `https://developer.android.com/reference/androidx/heifwriter/EncoderPreference` object used to specify the encoder's configuration. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder` | This `Builder` instance for method chaining. |

### setGridEnabled

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/heifwriter#1.0.0)

```
public @NonNull HeifWriter.Builder setGridEnabled(boolean gridEnabled)
```

Set whether to enable grid option.

| Parameters |
|---|---|
| `boolean gridEnabled` | Whether to enable grid option. If enabled, the tile size will be automatically chosen. Default is to enable. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder` | this Builder object. |

### setHandler

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/heifwriter#1.0.0)

```
public @NonNull HeifWriter.Builder setHandler(@Nullable Handler handler)
```

Provide a handler for the HeifWriter to use.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/Handler.html handler` | If not null, client will receive all callbacks on the handler's looper. Otherwise, client will receive callbacks on a looper created by the writer. Default is null. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder` | this Builder object. |

### setMaxImages

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/heifwriter#1.0.0)

```
public @NonNull HeifWriter.Builder setMaxImages(@IntRange(from = 1) int maxImages)
```

Set the maximum number of images to write.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 1) int maxImages` | Max number of images to write. Frames exceeding this number will not be written to file. The writing can be stopped earlier before this number of images are written by `https://developer.android.com/reference/androidx/heifwriter/HeifWriter#stop(long)`, except for the input mode of `https://developer.android.com/reference/androidx/heifwriter/HeifWriter#INPUT_MODE_SURFACE()`, where the EOS timestamp must be specified (via `https://developer.android.com/reference/androidx/heifwriter/HeifWriter#setInputEndOfStreamTimestamp(long)` and reached. Default is 1. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder` | this Builder object. |

### setPrimaryIndex

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/heifwriter#1.0.0)

```
public @NonNull HeifWriter.Builder setPrimaryIndex(@IntRange(from = 0) int primaryIndex)
```

Set the primary image index.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int primaryIndex` | Index of the image that should be marked as primary, must be within range \[0, maxImages - 1\] inclusive. Default is 0. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder` | this Builder object. |

### setQuality

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/heifwriter#1.0.0)

```
public @NonNull HeifWriter.Builder setQuality(@IntRange(from = 0, to = 100) int quality)
```

Set the quality for encoding images.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 0, to = 100) int quality` | A number between 0 and 100 (inclusive), with 100 indicating the best quality supported by this implementation. Default is 100. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder` | this Builder object. |

### setRotation

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/heifwriter#1.0.0)

```
public @NonNull HeifWriter.Builder setRotation(@IntRange(from = 0) int rotation)
```

Set the image rotation in degrees.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int rotation` | Rotation angle in degrees (clockwise) of the image, must be 0, 90, 180 or 270. Default is 0. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/heifwriter/HeifWriter.Builder` | this Builder object. |