public static File compressImageFiles(String filePath,String newFileName,int maxFileSize){
  BitmapFactory.Options options=new BitmapFactory.Options();
  options.inJustDecodeBounds=true;
  Bitmap bitmap=BitmapFactory.decodeFile(filePath,options);
  int actualHeight=options.outHeight;
  int actualWidth=options.outWidth;
  float imgRatio=actualWidth / actualHeight;
  int maxHeight=(2 * actualHeight) / 3;
  int maxWidth=(2 * actualWidth) / 3;
  float maxRatio=maxWidth / maxHeight;
  if (actualHeight > maxHeight || actualWidth > maxWidth) {
    if (imgRatio < maxRatio) {
      imgRatio=maxHeight / actualHeight;
      actualWidth=(int)(imgRatio * actualWidth);
      actualHeight=(int)maxHeight;
    }
 else     if (imgRatio > maxRatio) {
      imgRatio=maxHeight / actualWidth;
      actualHeight=(int)(imgRatio * actualHeight);
      actualWidth=(int)maxWidth;
    }
 else {
      actualHeight=(int)maxHeight;
      actualWidth=(int)maxWidth;
    }
  }
  options.inSampleSize=ImageUtils.calculateInSampleSize(options,actualWidth,actualHeight);
  options.inJustDecodeBounds=false;
  options.inTempStorage=new byte[16 * 1024];
  try {
    bitmap=BitmapFactory.decodeFile(filePath,options);
  }
 catch (  OutOfMemoryError exception) {
    exception.printStackTrace();
  }
  int streamLength=maxFileSize;
  int compressQuality=100;
  ByteArrayOutputStream bmpStream=new ByteArrayOutputStream();
  while (streamLength >= maxFileSize && compressQuality > 50) {
    try {
      bmpStream.flush();
      bmpStream.reset();
    }
 catch (    IOException e) {
      e.printStackTrace();
    }
    bitmap.compress(Bitmap.CompressFormat.JPEG,compressQuality,bmpStream);
    byte[] bmpPicByteArray=bmpStream.toByteArray();
    streamLength=bmpPicByteArray.length;
    if (BuildConfig.DEBUG) {
      Log.i("test upload","Quality: " + compressQuality);
      Log.i("test upload","Size: " + streamLength);
    }
    compressQuality-=3;
  }
  FileOutputStream fo;
  try {
    fo=new FileOutputStream(newFileName);
    fo.write(bmpStream.toByteArray());
    fo.flush();
    fo.close();
  }
 catch (  IOException e) {
    e.printStackTrace();
  }
  return new File(newFileName);
}

public static File compressImageFiles ( String filePath , String newFileName , int maxFileSize ) { BitmapFactory . Options options = new BitmapFactory . Options ( ) ; options . inJustDecodeBounds = true ; Bitmap bitmap = BitmapFactory . decodeFile ( filePath , options ) ; int actualHeight = options . outHeight ; int actualWidth = options . outWidth ; float imgRatio = actualWidth / actualHeight ; int maxHeight = ( 2 * actualHeight ) / 3 ; int maxWidth = ( 2 * actualWidth ) / 3 ; float maxRatio = maxWidth / maxHeight ; if ( actualHeight > maxHeight || actualWidth > maxWidth ) { if ( imgRatio < maxRatio ) { imgRatio = maxHeight / actualHeight ; actualWidth = ( int ) ( imgRatio * actualWidth ) ; actualHeight = ( int ) maxHeight ; } else if ( imgRatio > maxRatio ) { imgRatio = maxHeight / actualWidth ; actualHeight = ( int ) ( imgRatio * actualHeight ) ; actualWidth = ( int ) maxWidth ; } else { actualHeight = ( int ) maxHeight ; actualWidth = ( int ) maxWidth ; } } options . inSampleSize = ImageUtils . calculateInSampleSize ( options , actualWidth , actualHeight ) ; options . inJustDecodeBounds = false ; options . inTempStorage = new byte [ 16 * 1024 ] ; try { bitmap = BitmapFactory . decodeFile ( filePath , options ) ; } catch ( OutOfMemoryError exception ) { exception . printStackTrace ( ) ; } int streamLength = maxFileSize ; int compressQuality = 100 ; ByteArrayOutputStream bmpStream = new ByteArrayOutputStream ( ) ; while ( streamLength >= maxFileSize && compressQuality > 50 ) { try { bmpStream . flush ( ) ; bmpStream . reset ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } bitmap . compress ( Bitmap . CompressFormat . JPEG , compressQuality , bmpStream ) ; byte [ ] bmpPicByteArray = bmpStream . toByteArray ( ) ; streamLength = bmpPicByteArray . length ; if ( BuildConfig . DEBUG ) { Log . i ( "test upload" , "Quality: " + compressQuality ) ; Log . i ( "test upload" , "Size: " + streamLength ) ; } compressQuality -= 3 ; } FileOutputStream fo ; try { fo = new FileOutputStream ( newFileName ) ; fo . write ( bmpStream . toByteArray ( ) ) ; fo . flush ( ) ; fo . close ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } return new File ( newFileName ) ; }
this method will compressed image to a pre - configured files .
