Here you can find the source of bitmap2Bytes(Bitmap bm, Bitmap.CompressFormat format)
public static byte[] bitmap2Bytes(Bitmap bm, Bitmap.CompressFormat format)
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import java.io.ByteArrayOutputStream; public class Main { public static byte[] bitmap2Bytes(Bitmap bm, Bitmap.CompressFormat format) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(format, 100, baos);//from w ww. j av a 2 s. c o m return baos.toByteArray(); } public static Bitmap compress(String filePath, int width, int height) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(filePath, options); int bmpWidth = options.outWidth; int bmpHeight = options.outHeight; options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(filePath, options); float scaleWidth = bmpWidth; float scaleHeight = bmpHeight; if (bmpWidth > width) { scaleWidth = width / scaleWidth; } else { scaleWidth = 1; } if (bmpHeight > height) { scaleHeight = height / scaleHeight; } else { scaleHeight = 1; } Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, false); if (bitmap != resizeBitmap) { bitmap.recycle(); } return resizeBitmap; } }