Created
May 13, 2020 14:48
-
-
Save kelmer44/50c3f7cd4fac44b16ddfd5720d1d6778 to your computer and use it in GitHub Desktop.
This file contains 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 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