[Bugfix] Fix crash on previewing image to upload on Android P (#7184)
* 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] f83cc274b4/library/src/main/java/com/bumptech/glide/load/resource/bitmap/HardwareConfigState.java (L22)
Signed-off-by: Paweł Matuszewski <pamat@protonmail.com>
This commit is contained in:
parent
81b5fcdc7d
commit
d205202e52
|
@ -0,0 +1 @@
|
||||||
|
Fix crash on previewing images to upload on Android Pie.
|
|
@ -30,7 +30,15 @@ object ImageUtils {
|
||||||
fun getBitmap(context: Context, uri: Uri): Bitmap? {
|
fun getBitmap(context: Context, uri: Uri): Bitmap? {
|
||||||
return try {
|
return try {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
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 {
|
} else {
|
||||||
context.contentResolver.openInputStream(uri)?.use { inputStream ->
|
context.contentResolver.openInputStream(uri)?.use { inputStream ->
|
||||||
BitmapFactory.decodeStream(inputStream)
|
BitmapFactory.decodeStream(inputStream)
|
||||||
|
|
Loading…
Reference in New Issue