Canvas.drawBitmap() fails on Android 9 device

96 views Asked by At

I'm using canvas.drawBitmap(bitmap, matrix, null) to render an image (bitmap) onto a custom view, where matrix performs scaling and translation. This works perfectly on emulated devices in Android Studio, but when testing on an Android 9 mobile device (Sony Xperia XZ1 Compact), the rendering fails at higher magnification -- nothing is rendered, but there are no errors.

Has anyone experienced similar problems with drawBitmap on any device? Are there any known bugs, workarounds?

As an alternative, I also tried rendering the image using canvas.drawBitmap(bitmap, sourceRect, destRect, null) where the sourceRect and destRect rectangles are calculated to perform the same scaling and translation. This gives identical results, the rendering still fails specifically on my Android 9 device.

I do not see the problem when testing on an emulated device running Android 9. I'm guessing it might be related to some GPU operation on the Sony Xperia, but I have not seen any graphics problems with other apps on it.

1

There are 1 answers

0
Roland On

I found a workaround. It seems that the problem was caused by some kind of memory limitation on the Sony Xperia. I replaced my image drawing class with davemorrissey's excellent implementation, and found that the problem goes away when sourcing the image directly from a URI, avoiding loading the bitmap directly into memory. When sourcing the image from a bitmap in memory (like I did before), the same problem appeared again. I saw no memory allocation errors, but possibly some limit on the GPU or in other layers was exceeded.