compress Bitmap To Byte - Android Graphics

Android examples for Graphics:Bitmap Byte Array

Description

compress Bitmap To Byte

Demo Code


import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;

public class Main {
  public static byte[] compressBitmapToByte(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int options = 80;
    image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 100baos
    while (baos.toByteArray().length / 1024 > 100) { // 1M,,
      baos.reset();// baosbaos
      options -= 10;// 10
      image.compress(Bitmap.CompressFormat.JPEG, options, baos);// options%baos

    }//from   w ww . j  a v a2  s . c  om
    byte[] b = baos.toByteArray();// baosByteArrayInputStream
    if (image != null && !image.isRecycled()) {
      image.recycle();
    }
    return b;
  }
}

Related Tutorials