Last active
May 30, 2022 16:21
-
-
Save abdulhafizramadan-ittp/cbb2a2c4248b050221daba8a7375189f to your computer and use it in GitHub Desktop.
Android | Compress File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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