Compare commits
1 commit
7b5de0ee0f
...
d490d6f239
Author | SHA1 | Date | |
---|---|---|---|
d490d6f239 |
1 changed files with 17 additions and 20 deletions
|
@ -50,28 +50,19 @@ class DownloadWizardMethodSelectFragment : DownloadWizardActivity.DownloadWizard
|
|||
|
||||
private val gallerySelectorLauncher =
|
||||
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?) {
|
||||
fun <T> Bitmap.use(block: Bitmap.() -> T): T = try {
|
||||
block()
|
||||
} finally {
|
||||
recycle()
|
||||
}
|
||||
|
||||
val decoded = try {
|
||||
requireContext().contentResolver.openInputStream(result ?: return)
|
||||
private fun onGalleryResult(result: Uri) = try {
|
||||
requireContext().contentResolver.openInputStream(result)
|
||||
.use(BitmapFactory::decodeStream)
|
||||
.use(::decodeQrFromBitmap)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to decode QR code from gallery", e)
|
||||
return
|
||||
}
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
processLpaString(decoded)
|
||||
}
|
||||
null
|
||||
}
|
||||
|
||||
val downloadMethods = arrayOf(
|
||||
|
@ -200,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