Mehdi Haghgoo
05/31/2022, 6:09 PM@Composable
fun MyPicker() {
val context = LocalContext.current
val permissionLauncher = rememberLauncherForActivityResult(contract = ActivityResultContracts.RequestPermission().apply{
}){
}
val imgUri by remember{mutableStateOf("${context.filesDir}/temp.jpg".toUri())}
val captureLauncher = rememberLauncherForActivityResult(contract = ActivityResultContracts.TakePicture()){
Toast.makeText(context, "Image capture: ${if(it) "Successful" else "Failed"}", Toast.LENGTH_SHORT)
.show()
}
Column {
Button(onClick = {
if(ContextCompat.checkSelfPermission(context, android.Manifest.permission.CAMERA) != PERMISSION_GRANTED)
permissionLauncher.launch(android.Manifest.permission.CAMERA)
else captureLauncher.launch(imgUri)
}) {
Text("Load")
}
Image(painter = rememberAsyncImagePainter(imgUri), null)
}
}
Ian Lake
05/31/2022, 10:20 PMTakePicture
contract. That would simplify your code considerablyMehdi Haghgoo
06/01/2022, 4:38 AMIan Lake
06/01/2022, 4:50 AMStylianos Gakis
04/30/2023, 9:46 PMIan Lake
04/30/2023, 9:48 PMStylianos Gakis
04/30/2023, 9:53 PMActivityResultContracts.TakePicture()
contract there’s some ceremony that needs to be done for all this.
I’ve tried to find some part in the docs that mention how to use it but I haven’t been lucky.
There’s a bunch of things to get right. How to use this xml file which will contain the <paths> <external-path> and so on, getting the right path to there, creating a file to save there, getting the right Uri to it and then properly handling everything after process death so that when you’re back on your app everything still works.