wizard: Make sure bitmaps are recycled properly
All checks were successful
/ build-debug (push) Successful in 4m28s

Co-authored-by: septs <github@septs.pw>
This commit is contained in:
Peter Cai 2025-03-08 16:27:36 -05:00
parent 53f9459aed
commit d3df70501a
2 changed files with 13 additions and 9 deletions

View file

@ -44,17 +44,14 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard
lifecycleScope.launch(Dispatchers.IO) {
runCatching {
requireContext().contentResolver.openInputStream(result)?.let { input ->
val bmp = BitmapFactory.decodeStream(input)
input.close()
decodeQrFromBitmap(bmp)?.let {
withContext(Dispatchers.Main) {
processLpaString(it)
requireContext().contentResolver.openInputStream(result)?.use { input ->
BitmapFactory.decodeStream(input).use { bmp ->
decodeQrFromBitmap(bmp)?.let {
withContext(Dispatchers.Main) {
processLpaString(it)
}
}
}
bmp.recycle()
}
}
}

View file

@ -86,6 +86,13 @@ suspend fun connectSEService(context: Context): SEService = suspendCoroutine { c
}
}
inline fun <T> Bitmap.use(f: (Bitmap) -> T): T =
try {
f(this)
} finally {
recycle()
}
fun decodeQrFromBitmap(bmp: Bitmap): String? =
runCatching {
val pixels = IntArray(bmp.width * bmp.height)