is there a way to ask for an image file using comp...
# compose-desktop
a
is there a way to ask for an image file using compose desktop? my use case is letting users select a file from their computer as their profile photo in my app. for anyone doing android, this would be done using the OpenDocument contract (or ACTION_OPEN_DOCUMENT intent)
2
h
https://kotlinlang.slack.com/archives/C01D6HTPATV/p1644256277241549 I've asked that question before and this worked for me. But Kamel also looks great.
d
I have used
java.awt.FileDialog
message has been deleted
this is how I use it for desktop
for android I have this:
Copy code
@Composable
actual fun ImagePicker(
    path: String,
    onPicked: (Uri?) -> Unit
) {
    val context = LocalContext.current
    var target: Uri? = null
    val cameraLauncher = rememberLauncherForActivityResult(
        ActivityResultContracts.TakePicture()
    ) {
        if (it) onPicked(target)
    }
    val galleryLauncher = rememberLauncherForActivityResult(
        ActivityResultContracts.GetContent(),
        onResult = onPicked
    )
    val cameraPermission = rememberLauncherForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) {
        if (it) {
            //create target for camera
            target = context.createPrivateImageUri(path = path)
            cameraLauncher.launch(target)
        }
    }
    AlertDialog(
        onDismissRequest = { onPicked(null) },
        dismissButton = {
            Button(onClick = { onPicked(null) }) {
                Text("Cancel")
            }
        },
        confirmButton = {},
        title = { Text("Pick photo") },
        text = {
            Row(
                horizontalArrangement = Arrangement.spacedBy(16.dp)
            ) {
                Button(
                    modifier = Modifier.weight(1f),
                    onClick = { cameraPermission.launch(Manifest.permission.CAMERA) }
                ) {
                    Text("Camera")
                }
                Button(
                    modifier = Modifier.weight(1f),
                    onClick = { galleryLauncher.launch("image/*") }
                ) {
                    Text("Gallery")
                }
            }
        },
        backgroundColor = MaterialTheme.colorScheme.primaryContainer,
        shape = RoundedCornerShape(16.dp)
    )
}
it shows a dialog with 2 options, camera and gallery
a
@Dragos Rachieru awesome stuff. thanks for code. the file dialog seems to be what i am looking for, even though i somehow need to request images only. i'll have a look at the docs
d
maybe this will help
Copy code
fileDialog.setFile("*.jpg;*.jpeg");
💯 1
🤩 1
k
Thanks for that last file type tip @Dragos Rachieru. Yes, I found the FileDialog window worked really well for me, both opening image files and saving files.
👍🏻 1