@@ -4,9 +4,9 @@ import android.content.Context
44import android.graphics.Bitmap
55import android.graphics.BitmapFactory
66import android.graphics.Canvas
7- import android.graphics.Rect
87import android.graphics.drawable.BitmapDrawable
98import android.graphics.drawable.Drawable
9+ import android.os.Build
1010import android.util.Base64
1111import android.util.TypedValue
1212import android.view.View
@@ -119,10 +119,16 @@ class ResourceImage(private val resId: Int) : Image() {
119119 }
120120
121121 override fun getStream (context : Context , compressFormat : Bitmap .CompressFormat , quality : Int ): InputStream {
122- return BitmapFactory .decodeResource(context.resources, resId)
123- .toInputStream(compressFormat, quality)
124- }
122+ val drawable = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
123+ context.getDrawable(resId)!!
124+ } else {
125+ context.resources.getDrawable(resId)!!
126+ }
125127
128+ return drawable.toBitmap {
129+ toInputStream(compressFormat, quality)
130+ }
131+ }
126132}
127133
128134class DrawableImage (private val drawable : Drawable ) : Image() {
@@ -136,7 +142,9 @@ class DrawableImage(private val drawable: Drawable) : Image() {
136142 }
137143
138144 override fun getStream (context : Context , compressFormat : Bitmap .CompressFormat , quality : Int ): InputStream {
139- return drawable.toBitmap().toInputStream(compressFormat, quality)
145+ return drawable.toBitmap {
146+ toInputStream(compressFormat, quality)
147+ }
140148 }
141149
142150}
@@ -163,30 +171,28 @@ fun Bitmap.toInputStream(compressFormat: Bitmap.CompressFormat, quality: Int): I
163171 return ByteArrayInputStream (byteArray)
164172}
165173
166- fun Drawable.toBitmap (
167- width : Int = intrinsicWidth,
168- height : Int = intrinsicHeight,
169- config : Bitmap .Config ? = null
170- ): Bitmap {
174+ private inline fun <R > Drawable.toBitmap (converter : Bitmap .() -> R ): R {
171175 if (this is BitmapDrawable ) {
172- if (config == null || bitmap.config == config) {
173- // Fast-path to return original. Bitmap.createScaledBitmap will do this check, but it
174- // involves allocation and two jumps into native code so we perform the check ourselves.
175- if (width == intrinsicWidth && height == intrinsicHeight) {
176- return bitmap
177- }
178- return Bitmap .createScaledBitmap(bitmap, width, height, true )
179- }
176+ return converter(bitmap)
177+ }
178+
179+ val newBitmap = if (intrinsicWidth <= 0 || intrinsicHeight <= 0 ) {
180+ Bitmap .createBitmap(500 , 500 , Bitmap .Config .ARGB_8888 )!!
181+ } else {
182+ Bitmap .createBitmap(intrinsicWidth, intrinsicHeight, Bitmap .Config .ARGB_8888 )!!
180183 }
181184
182- val newRect = Rect (bounds)
185+ try {
186+ val oldBounds = copyBounds()
187+ setBounds(0 , 0 , newBitmap.width, newBitmap.height)
183188
184- val bitmap = Bitmap .createBitmap(width, height, config ? : Bitmap .Config .ARGB_8888 )
185- setBounds(0 , 0 , width, height)
186- draw(Canvas (bitmap))
189+ draw(Canvas (newBitmap))
187190
188- setBounds(newRect.left, newRect.top, newRect.right, newRect.bottom)
189- return bitmap
191+ bounds = oldBounds
192+ return converter(newBitmap)
193+ } finally {
194+ newBitmap.recycle()
195+ }
190196}
191197
192198@JvmOverloads
0 commit comments