Skip to content

Instantly share code, notes, and snippets.

@abdulhafizramadan-ittp
Last active May 30, 2022 16:21
Show Gist options
  • Select an option

  • Save abdulhafizramadan-ittp/cbb2a2c4248b050221daba8a7375189f to your computer and use it in GitHub Desktop.

Select an option

Save abdulhafizramadan-ittp/cbb2a2c4248b050221daba8a7375189f to your computer and use it in GitHub Desktop.
Android | Compress File
private fun reduceFileImage(file: File): File {
val bitmap = BitmapFactory.decodeFile(file.path)
var compressQuality = 100
var streamLength: Int
do {
val bmpStream = ByteArrayOutputStream()
bitmap.compress(CompressFormat.JPEG, compressQuality, bmpStream)
val bmpPicByteArray = bmpStream.toByteArray()
streamLength = bmpPicByteArray.size
compressQuality -= 5
} while (streamLength > 1000000)
bitmap.compress(CompressFormat.JPEG, compressQuality, FileOutputStream(file))
return file
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment