Hello, I'm having trouble specifying a path to an ...
# compose-desktop
k
Hello, I'm having trouble specifying a path to an image file to use in a Composable. See in thread please. Thanks.
When loading an Image on a Surface, it works when configured this way:
Copy code
var planPath = mutableStateOf("E:\\Users\\User\\Sample.jpg")
var planFile = File("${planPath.value}")
Image(
  painter = BitmapPainter(image = loadImageBitmap(planFile.inputStream())),
  contentDescription = "",
  contentScale = ContentScale.Fit,
)
But when I try loading a path into
planPath
from a FileDialog like this, it gives an error: (The system cannot find the path specified)
Copy code
fun FrameWindowScope.setPlan(): String {
  val dia = java.awt.FileDialog(window)
  dia.isVisible=true
  println("dia.directory: ${dia.directory} dia.file: ${dia.file}")

  return if (dia.file == null) {
    println("No file selected.")
    ""
  } else {
    val file = dia.directory.replace("\\","\\\\") + dia.file
    println("File selected: $file")
    file
  }
}
The FileDialog function works, and captures the path and filename, but it must be formatting differently to the literal string? The output of the FileDialog window appears identical to what I try manually specifying in the variable! Thanks!
@Zach Klippenstein (he/him) [MOD] Have you had anything to do with setting file paths like this?? šŸ¤”šŸ˜•
z
Nope, sorry. There are a few issues with your code though, probably unrelated to this error. • Any mutableStateOf in a composable should be wrapped in a
remember
. •
ā€œ$somethingā€
is the same as
something.toString()
, and the latter form is typically preferred - if
something
is a string you don't need the toString. • Is
loadImageBitmap
a composable or does it actually load the image? If the latter, you shouldn't call it from the composable but put it in an effect.
k
You're prob right, mate. I have so much to learn. I need to do some more reading I guess. Thanks for looking anyway, and thanks for the tips.
šŸ‘ 1