From d205202e52ae6941fca0cbd43520481e013cfb2b Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 4 Oct 2022 13:34:13 +0000 Subject: [PATCH] [Bugfix] Fix crash on previewing image to upload on Android P (#7184) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix crash on image upload preview on Android P Using hardware bitmap allocation on Android framework versions prior to Android Q causes a crash when decoding a bitmap if GL context wasn't initialised. The issue is not documented in ImageDecoder reference but it is mentioned in the comments of glide[1] with a link to internal google discussion. [1] https://github.com/bumptech/glide/blob/f83cc274b42cf02af6611d2a8c21e4a9b1f5d592/library/src/main/java/com/bumptech/glide/load/resource/bitmap/HardwareConfigState.java#L22 Signed-off-by: Paweł Matuszewski --- changelog.d/7184.bugfix | 1 + .../java/im/vector/lib/multipicker/utils/ImageUtils.kt | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 changelog.d/7184.bugfix diff --git a/changelog.d/7184.bugfix b/changelog.d/7184.bugfix new file mode 100644 index 0000000000..50d7beedd6 --- /dev/null +++ b/changelog.d/7184.bugfix @@ -0,0 +1 @@ +Fix crash on previewing images to upload on Android Pie. diff --git a/library/multipicker/src/main/java/im/vector/lib/multipicker/utils/ImageUtils.kt b/library/multipicker/src/main/java/im/vector/lib/multipicker/utils/ImageUtils.kt index a3d69ae8cf..705223c55e 100644 --- a/library/multipicker/src/main/java/im/vector/lib/multipicker/utils/ImageUtils.kt +++ b/library/multipicker/src/main/java/im/vector/lib/multipicker/utils/ImageUtils.kt @@ -30,7 +30,15 @@ object ImageUtils { fun getBitmap(context: Context, uri: Uri): Bitmap? { return try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, uri)) + val source = ImageDecoder.createSource(context.contentResolver, uri) + val listener = ImageDecoder.OnHeaderDecodedListener { decoder, _, _ -> + if (Build.VERSION.SDK_INT == Build.VERSION_CODES.P) { + // Allocating hardware bitmap may cause a crash on framework versions prior to Android Q + decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE + } + } + + ImageDecoder.decodeBitmap(source, listener) } else { context.contentResolver.openInputStream(uri)?.use { inputStream -> BitmapFactory.decodeStream(inputStream)