public Bitmap drawableToBitmap ( Drawable drawable ) { if ( drawable == null ) // Don't do anything without a proper drawable return null ; else if ( drawable instanceof BitmapDrawable ) { // Use the getBitmap() method instead if BitmapDrawable Log . i ( TAG , "Bitmap drawable!" ) ; return ( ( BitmapDrawable ) drawable ) . getBitmap ( ) ; } int intrinsicWidth = drawable . getIntrinsicWidth ( ) ; int intrinsicHeight = drawable . getIntrinsicHeight ( ) ; if ( ! ( intrinsicWidth > 0 && intrinsicHeight > 0 ) ) return null ; try { // Create Bitmap object out of the drawable Bitmap bitmap = Bitmap . createBitmap ( intrinsicWidth , intrinsicHeight , Bitmap . Config . ARGB_8888 ) ; Canvas canvas = new Canvas ( bitmap ) ; drawable . setBounds ( 0 , 0 , canvas . getWidth ( ) , canvas . getHeight ( ) ) ; drawable . draw ( canvas ) ; return bitmap ; } catch ( OutOfMemoryError e ) { // Simply return null of failed bitmap creations Log . e ( TAG , "Encountered OutOfMemoryError while generating bitmap!" ) ; return null ; } }
Convert a drawable object into a Bitmap .