Skip to content

Instantly share code, notes, and snippets.

@kelmer44
Created May 13, 2020 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kelmer44/50c3f7cd4fac44b16ddfd5720d1d6778 to your computer and use it in GitHub Desktop.
Save kelmer44/50c3f7cd4fac44b16ddfd5720d1d6778 to your computer and use it in GitHub Desktop.
private fun decodeFile(f: File, height: Int): Bitmap? {
try {
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeStream(FileInputStream(f), null, options)
// Width and height of the bitmap
val bmpWidth = options.outWidth
val bmpHeight = options.outHeight
val requiredSize = height.coerceAtMost(bmpHeight)
var widthTmp = bmpWidth
var heightTmp = bmpHeight
var scale = 1
while (true) {
if ((widthTmp / 2 < requiredSize) || heightTmp / 2 < requiredSize)
break
widthTmp /= 2
heightTmp /= 2
scale *= 2
}
// decode
val decodeOptions = BitmapFactory.Options()
decodeOptions.inSampleSize = scale
return BitmapFactory.decodeStream(FileInputStream(f), null, decodeOptions)
} catch (e: Exception) {
Log.e(TAG, "Error decoding bitmap", e)
}
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment