public static Bitmap shrink(Bitmap src,int jpgCompression,long maxSize){
  final float factor=0.5f;
  ByteArrayOutputStream stream=new ByteArrayOutputStream();
  src.compress(Bitmap.CompressFormat.JPEG,jpgCompression,stream);
  while (maxSize != -1 && stream.size() > maxSize) {
    int height=(int)(src.getHeight() * factor);
    int width=(int)(src.getWidth() * factor);
    stream.reset();
    src=Bitmap.createScaledBitmap(src,width,height,false);
    src.compress(Bitmap.CompressFormat.JPEG,90,stream);
  }
  return src;
}

public static Bitmap shrink ( Bitmap src , int jpgCompression , long maxSize ) { final float factor = 0.5f ; ByteArrayOutputStream stream = new ByteArrayOutputStream ( ) ; src . compress ( Bitmap . CompressFormat . JPEG , jpgCompression , stream ) ; while ( maxSize != - 1 && stream . size ( ) > maxSize ) { int height = ( int ) ( src . getHeight ( ) * factor ) ; int width = ( int ) ( src . getWidth ( ) * factor ) ; stream . reset ( ) ; src = Bitmap . createScaledBitmap ( src , width , height , false ) ; src . compress ( Bitmap . CompressFormat . JPEG , 90 , stream ) ; } return src ; }
shrinks a bitmap so that , when it is encoded to a jpg with the given compression level , its size is less than the given number of bytes .
