Compare commits
1 commit
7b5de0ee0f
...
d490d6f239
Author | SHA1 | Date | |
---|---|---|---|
d490d6f239 |
1 changed files with 17 additions and 13 deletions
|
@ -50,21 +50,19 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard
|
||||||
|
|
||||||
private val gallerySelectorLauncher =
|
private val gallerySelectorLauncher =
|
||||||
registerForActivityResult(ActivityResultContracts.GetContent()) {
|
registerForActivityResult(ActivityResultContracts.GetContent()) {
|
||||||
lifecycleScope.launch(Dispatchers.IO) { onGalleryResult(it) }
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
|
val decoded = onGalleryResult(it ?: return@launch) ?: return@launch
|
||||||
|
withContext(Dispatchers.Main) { processLpaString(decoded) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun onGalleryResult(result: Uri?) {
|
private fun onGalleryResult(result: Uri) = try {
|
||||||
var bmp: Bitmap? = null
|
requireContext().contentResolver.openInputStream(result)
|
||||||
try {
|
.use(BitmapFactory::decodeStream)
|
||||||
bmp = requireContext().contentResolver.openInputStream(result ?: return)
|
.use(::decodeQrFromBitmap)
|
||||||
.use(BitmapFactory::decodeStream)
|
} catch (e: Exception) {
|
||||||
val decoded = decodeQrFromBitmap(bmp)
|
Log.e(TAG, "Failed to decode QR code from gallery", e)
|
||||||
withContext(Dispatchers.Main) { processLpaString(decoded) }
|
null
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.e(TAG, "Failed to decode QR code from gallery", e)
|
|
||||||
} finally {
|
|
||||||
bmp?.recycle()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val downloadMethods = arrayOf(
|
val downloadMethods = arrayOf(
|
||||||
|
@ -193,3 +191,9 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun <T> Bitmap.use(block: Bitmap.() -> T): T = try {
|
||||||
|
block()
|
||||||
|
} finally {
|
||||||
|
recycle()
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue