hfhbd
03/01/2022, 11:21 AMActivityResultContracts.TakePicture()
should return the file to match Compose declarative style. Currently, you have to create the file first, and keep a reference to it to use it after the camera intent finished successfully. Creating a TakePicturePlus()
returning Uri?
instead does not work, because the TakePicture
result intent is almost empty.val context = LocalContext.current
val id = remember { ImageDTO.ID(UUID()) }
val uri = remember(id) {
FileProvider.getUriForFile(
context,
"sample.provider",
id.file(context)
)
}
val cameraLauncher = rememberLauncherForActivityResult(TakePicture()) { success ->
if (success) {
action(uri) // Needs a reference to the always created uri...
}
}
Button(onClick = {
cameraLauncher.launch(uri) // I want to create the uri here!
}) {
Text(stringResource(id = R.string.takeAPicture))
}
gildor
03/01/2022, 1:26 PMhfhbd
03/01/2022, 1:28 PMIntent
does not return the given Input? Or do you mean by simple storing the reference in the custom ActivityContract?... This sounds too easy, I simple forgot itgildor
03/01/2022, 4:41 PMhfhbd
03/07/2022, 4:24 PMclass TakePicturePlus(private val fileAuthority: String) : ActivityResultContract<String, Uri?>() {
private lateinit var uri: Uri
private lateinit var file: File
override fun createIntent(context: Context, input: String): Intent {
file = input.file(context)
uri = FileProvider.getUriForFile(context, fileAuthority, file)
return Intent(MediaStore.ACTION_IMAGE_CAPTURE)
.putExtra(MediaStore.EXTRA_OUTPUT, uri)
}
override fun parseResult(resultCode: Int, intent: Intent?) =
if (resultCode == Activity.RESULT_OK) {
uri
} else {
file.delete()
null
}
private fun String.file(context: Context): File {
val folder = File(context.filesDir, "images")
if (!folder.exists()) {
folder.mkdir()
}
val file = File(folder, "$this.jpg")
file.createNewFile()
return file
}
}
Ian Lake
03/08/2022, 3:45 AMFile
is saved (i.e., via rememberSaveable
, etc.) so that it is actually available when the result returns. That code looks like it'll fail completely in those cases (not to mention cases like doing a configuration change if your activity doesn't handle all config changes)remember
that Contract, you'll get a new instance every time that Composable recomposes - you should really make all of your Contracts stateless if at all possiblehfhbd
03/08/2022, 11:39 AMStylianos Gakis
05/01/2023, 2:12 AM