public static void compressToFile ( @ NonNull final Bitmap bitmap , @ NonNull final File file , @ NonNull final CompressFormat format , final int quality ) throws IOException { Condition . INSTANCE . ensureNotNull ( bitmap , "The bitmap may not be null" ) ; Condition . INSTANCE . ensureNotNull ( file , "The file may not be null" ) ; Condition . INSTANCE . ensureNotNull ( format , "The format may not be null" ) ; Condition . INSTANCE . ensureAtLeast ( quality , 0 , "The quality must be at least 0" ) ; Condition . INSTANCE . ensureAtMaximum ( quality , 100 , "The quality must be at maximum 100" ) ; OutputStream outputStream = null ; try { outputStream = new FileOutputStream ( file ) ; boolean result = bitmap . compress ( format , quality , outputStream ) ; if ( ! result ) { throw new IOException ( "Failed to compress bitmap to file \"" + file + "\" using format " + format + " and quality " + quality ) ; } } finally { StreamUtil . INSTANCE . close ( outputStream ) ; } }
Compresses a specific bitmap and stores it within a file .