Skip to content

Instantly share code, notes, and snippets.

@MyricSeptember
Created March 19, 2024 13:33
Show Gist options
  • Save MyricSeptember/a510c7105a5bc3aae495b92809090838 to your computer and use it in GitHub Desktop.
Save MyricSeptember/a510c7105a5bc3aae495b92809090838 to your computer and use it in GitHub Desktop.
Launch Activity For Result
var pages by remember {
mutableStateOf<List<Uri>>(emptyList())
}
scannerLauncher =
rememberLauncherForActivityResult(contract = ActivityResultContracts.StartIntentSenderForResult(),
onResult = { activityResult ->
val resultCode = activityResult.resultCode
val result = GmsDocumentScanningResult.fromActivityResultIntent(
activityResult.data
)
when (resultCode) {
RESULT_OK -> {
//get images
pages = result?.pages?.map { it.imageUri } ?: emptyList()
//getPDF
result?.pdf?.let { pdf ->
val fileOutputStream = FileOutputStream(
File(
filesDir,
"$documentName.pdf"
)
)
contentResolver.openInputStream(pdf.uri).use {
it?.copyTo(fileOutputStream)
}
}
}
RESULT_CANCELED -> {
showToast("Scanner Cancelled")
}
else -> {
showToast("Something went wrong")
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment