Java
Bitmap
Byte Array
Image Conversion
Programming Tutorial

converting Java bitmap to byte array

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When Android developers say "convert a bitmap to a byte array," they usually mean encode a Bitmap into an image format such as PNG or JPEG and then read the resulting bytes. The standard approach is to compress the bitmap into a ByteArrayOutputStream and call toByteArray().

The Standard Approach

Here is the basic pattern in Java:

java
1import android.graphics.Bitmap;
2import java.io.ByteArrayOutputStream;
3
4public static byte[] bitmapToBytes(Bitmap bitmap) {
5    ByteArrayOutputStream stream = new ByteArrayOutputStream();
6    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
7    return stream.toByteArray();
8}

This does three things:

  • creates an in-memory byte stream
  • encodes the bitmap into an image format
  • returns the encoded bytes

Those bytes can then be uploaded, stored in a database, written to disk, or passed through an API.

Choose the Right Format

The output format matters more than many people realize.

PNG

  • lossless
  • preserves transparency
  • often larger than JPEG
java
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

JPEG

  • lossy
  • smaller for photos
  • does not preserve transparency
java
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, stream);

For JPEG, the quality number matters. Lower values reduce file size but also reduce image quality.

Full Example

java
1import android.graphics.Bitmap;
2import android.graphics.BitmapFactory;
3import java.io.ByteArrayOutputStream;
4
5public class BitmapUtils {
6
7    public static byte[] toJpegBytes(Bitmap bitmap, int quality) {
8        ByteArrayOutputStream stream = new ByteArrayOutputStream();
9        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream);
10        return stream.toByteArray();
11    }
12
13    public static Bitmap fromBytes(byte[] data) {
14        return BitmapFactory.decodeByteArray(data, 0, data.length);
15    }
16}

This example shows both directions: encode to bytes and decode back to a bitmap.

Encoded Bytes vs Raw Pixel Bytes

Sometimes the requirement is not "save as JPEG or PNG" but "get raw pixel bytes." Those are different tasks.

The compression approach above gives you encoded image bytes. If you need raw pixel memory, you might copy pixels into a ByteBuffer instead.

java
1import android.graphics.Bitmap;
2import java.nio.ByteBuffer;
3
4public static byte[] rawBitmapBytes(Bitmap bitmap) {
5    ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
6    bitmap.copyPixelsToBuffer(buffer);
7    return buffer.array();
8}

Use raw bytes only if another part of the system expects raw pixel data. They are larger and not suitable as a drop-in replacement for standard image files.

Memory Considerations

Bitmaps can be large, and converting them to byte arrays duplicates data in memory. That matters on Android, especially with photos.

Practical ways to reduce memory pressure:

  • downsample the bitmap before conversion
  • choose JPEG instead of PNG for photos
  • avoid converting oversized camera images on the main thread

If the source bitmap is much larger than the final upload or thumbnail size, resize first and then encode.

Run It Off the UI Thread

Compression and large memory allocations can block rendering if done on the main thread. Use a background thread, executor, or coroutine-backed Java interop path for nontrivial images.

Even if the code is only a few lines, the underlying work may be expensive for large bitmaps.

When Quality Is Ignored

One subtle point: the quality parameter affects JPEG and some other lossy formats, but for PNG it is effectively ignored because PNG is lossless. Developers sometimes keep changing the quality value on PNG output and wonder why the file size does not change much.

That behavior is normal and not a bug in the code.

Common Pitfalls

  • Assuming the byte array contains raw pixels when compress actually produces encoded PNG or JPEG data.
  • Choosing JPEG for an image that needs transparency. JPEG discards alpha information.
  • Expecting the quality parameter to meaningfully affect PNG output. PNG is lossless, so quality is not the tuning knob there.
  • Converting very large bitmaps on the UI thread and then blaming Android for jank or freezes.
  • Forgetting that bitmap-to-byte-array conversion increases memory usage because both the bitmap and encoded bytes may exist at the same time.

Summary

  • The standard bitmap-to-byte-array conversion uses ByteArrayOutputStream plus bitmap.compress(...).
  • PNG is lossless and preserves transparency, while JPEG is smaller for photo-like images.
  • Encoded image bytes are different from raw pixel bytes.
  • Large bitmap conversions should be done carefully to avoid memory spikes and UI stalls.
  • Pick the format and quality settings based on the actual storage or transport requirement.

Course illustration
Course illustration

All Rights Reserved.