Skip to content

Instantly share code, notes, and snippets.

@tahaak67
Created February 19, 2023 13:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tahaak67/2dcc4a8913c8078b08c011904ac4faa7 to your computer and use it in GitHub Desktop.
Save tahaak67/2dcc4a8913c8078b08c011904ac4faa7 to your computer and use it in GitHub Desktop.
An example of image picker in jetpack compose, the image is diplayed using Coil library.
//make sure your target sdk is 33+ and androidx.activity:activity-compose dependency is updated
var imageUri by remember {
mutableStateOf<Uri?>(null)
}
val imageLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.PickVisualMedia(),
onResult = { uri -> imageUri = uri }
)
Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally) {
Button(onClick = {
imageLauncher.launch(
PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo)
)
}) {
Text(text = "Pick an Image")
}
AsyncImage(
model = imageUri,
contentDescription = "image"
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment