Skip to content

Instantly share code, notes, and snippets.

@Kolyall
Created February 28, 2019 07:26
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 Kolyall/2cfc3ec8609c3b0a6e96c2401d4c68f5 to your computer and use it in GitHub Desktop.
Save Kolyall/2cfc3ec8609c3b0a6e96c2401d4c68f5 to your computer and use it in GitHub Desktop.
Get Bitmap from assets
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