Skip to content

Instantly share code, notes, and snippets.

@jesvs
Created October 23, 2019 23:57
Show Gist options
  • Save jesvs/b5c68103a0122fb71c2a66413b3ae9fb to your computer and use it in GitHub Desktop.
Save jesvs/b5c68103a0122fb71c2a66413b3ae9fb to your computer and use it in GitHub Desktop.
Get a Bitmap from a Drawable resource id
fun getBitmapFromDrawable(resId: Int): Bitmap? {
val drawable = AppCompatResources.getDrawable(requireContext(), resId) ?: return null
val bitmap = Bitmap.createBitmap(
drawable.intrinsicWidth,
drawable.intrinsicHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
return bitmap
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment