Skip to content

Instantly share code, notes, and snippets.

@oussama-dz
Created June 20, 2023 08:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oussama-dz/606b7830aac20c53bfab37acbcb41c4e to your computer and use it in GitHub Desktop.
Save oussama-dz/606b7830aac20c53bfab37acbcb41c4e to your computer and use it in GitHub Desktop.
basic image picker using kotlin and jetpack compose
@Composable
fun ImagePicker() {
var imageUri: Uri? by remember {
mutableStateOf(null)
}
val launcher =
rememberLauncherForActivityResult(contract = ActivityResultContracts.OpenDocument()) {
it?.let { uri ->
imageUri = uri
}
}
Column(
modifier = Modifier
.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(16.dp, Alignment.CenterVertically)
) {
AsyncImage(
model = imageUri,
contentDescription = null,
modifier = Modifier
.size(200.dp),
contentScale = ContentScale.Crop
)
Button(
onClick = {
launcher.launch(arrayOf("image/*"))
}
) {
Text(text = "Pick Image")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment