# ImagePart

# ImagePart


```
public final class ImagePart extends Part implements AutoCloseable
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [com.google.mlkit.genai.embedding.Part](https://developers.google.com/android/reference/com/google/mlkit/genai/embedding/Part.md.txt) ||
|   | ↳ | [com.google.mlkit.genai.embedding.ImagePart](https://developers.google.com/android/reference/com/google/mlkit/genai/embedding/ImagePart.md.txt) |

*** ** * ** ***

A class representing an image part of `Content` for embedding. This class handles the conversion of various image sources into a `Bitmap` upon creation.

The `Bitmap` lifecycle is managed by the caller and is not recycled by the SDK during embedding generation. Callers may optionally call `close` (or use `.use {}` / try-with-resources) to recycle `bitmap` when finished, which is particularly useful when `ImagePart` decodes a new `Bitmap` via the `Uri` or `ByteArray` constructors.

## Summary

| ### Public constructors |
|---|
| `ImagePart(@NonNull Bitmap bitmap)` |
| `ImagePart(@NonNull byte[] blob)` Creates an `ImagePart` from a `ByteArray`. |
| `ImagePart(@NonNull Uri uri)` Creates an `ImagePart` from a `Uri`. |

| ### Public methods |
|---|---|
| `void` | `close()` Recycles the underlying `bitmap` if it has not already been recycled. |
| `boolean` | `equals(Object other)` Compares this `ImagePart` to another object for equality. |
| `final @NonNull Bitmap` | `getBitmap()` The `Bitmap` representation of the image. |
| `final int` | `getHeight()` The height of the image in pixels. |
| `final int` | `getWidth()` The width of the image in pixels. |
| `int` | `hashCode()` Returns a hash code value for the object, consistent with the overridden `equals` method. |
| `final boolean` | `isClosed()` Returns whether this `ImagePart` has been closed and its underlying bitmap recycled. |
| `@NonNull String` | `toString()` |

## Public constructors

### ImagePart

```
public ImagePart(@NonNull Bitmap bitmap)
```

### ImagePart

```
public ImagePart(@NonNull byte[] blob)
```

Creates an `ImagePart` from a `ByteArray`.

| Parameters |
|---|---|
| `@NonNull byte[] blob` | the `ByteArray` of the encoded image. |

| Throws |
|---|---|
| `IllegalArgumentException` | if the byte array cannot be decoded into a bitmap. |

### ImagePart

```
public ImagePart(@NonNull Uri uri)
```

Creates an `ImagePart` from a `Uri`. This constructor will perform I/O to decode the image.

| Parameters |
|---|---|
| `@NonNull Uri uri` | the `Uri` pointing to the image. |

| Throws |
|---|---|
| `IOException` | if the URI cannot be read. |
| `IllegalArgumentException` | if the data from the URI cannot be decoded into a bitmap. |

## Public methods

### close

```
public void close()
```

Recycles the underlying `bitmap` if it has not already been recycled.

### equals

```
public boolean equals(Object other)
```

Compares this `ImagePart` to another object for equality.

| Returns |
|---|---|
| `boolean` | `true` if the other object is an `ImagePart` and references the same `bitmap` instance. |

### getBitmap

```
public final @NonNull Bitmap getBitmap()
```

The `Bitmap` representation of the image.

### getHeight

```
public final int getHeight()
```

The height of the image in pixels.

### getWidth

```
public final int getWidth()
```

The width of the image in pixels.

### hashCode

```
public int hashCode()
```

Returns a hash code value for the object, consistent with the overridden `equals` method.

### isClosed

```
public final boolean isClosed()
```

Returns whether this `ImagePart` has been closed and its underlying bitmap recycled.

### toString

```
public @NonNull String toString()
```