fromString
Parses a string into the appropriate ImageSource variant.
Parsing rules:
"http://..."or"https://..."→ Url"data:image/png;base64,..."→ Bytes (decoded from Base64)"/path/to/file"or"file:///path/to/file"→ FilePath
Return
The corresponding ImageSource variant.
Parameters
source
The string to parse.
Samples
// Using .fromString() function.
// rawImageData is String.
when (val imageSource = ImageSource.fromString(rawImageData)) { // Try to detect image source.
is ImageSource.Url -> println("Source is URL: ${imageSource.url}")
is ImageSource.Bytes -> println("Source is Bytes: ${imageSource.bytes}")
is ImageSource.FilePath -> {
// Check path exists(example).
val path = imageSource.path
if (exists(path)) {
println("Source is file path: $path")
} else {
println("Source is file path: $path, but file doesn't exist")
}
}
}Content copied to clipboard