Created
February 28, 2019 07:26
-
-
Save Kolyall/2cfc3ec8609c3b0a6e96c2401d4c68f5 to your computer and use it in GitHub Desktop.
Get Bitmap from assets
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 getPicFromAsset(imageView: ImageView, assetName: String): Bitmap? { | |
// Get the dimensions of the View | |
val targetW = imageView.width | |
val targetH = imageView.height | |
if (targetW == 0 || targetH == 0) { | |
// view has no dimensions set | |
return null | |
} | |
try { | |
val inputStream = mAssetManager.open("img/$assetName") | |
// Get the dimensions of the bitmap | |
val bmOptions = BitmapFactory.Options() | |
bmOptions.inJustDecodeBounds = true | |
BitmapFactory.decodeStream(inputStream, Rect(-1, -1, -1, -1), bmOptions) | |
val photoW = bmOptions.outWidth | |
val photoH = bmOptions.outHeight | |
// Determine how much to scale down the image | |
val scaleFactor = Math.min(photoW / targetW, photoH / targetH) | |
inputStream.reset() | |
// Decode the image file into a Bitmap sized to fill the View | |
bmOptions.inJustDecodeBounds = false | |
bmOptions.inSampleSize = scaleFactor | |
bmOptions.inPurgeable = true | |
return BitmapFactory.decodeStream(inputStream, Rect(-1, -1, -1, -1), bmOptions) | |
} catch (e: IOException) { | |
e.printStackTrace() | |
return null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment