How to convert byte array containing SVG image to ...
# compose-android
p
How to convert byte array containing SVG image to Painter (Android target)?
s
Coil 3 supports displaying SVG images.
p
I'm looking for Android implementation without any extra dependencies.
s
If you need to display arbitrary SVGs downloaded from your backend, there’s no out-of-the-box solution. It's not a simple task, accurately parsing and rendering even a typical subset of the SVG spec involves a fair amount of complexity. You can, of course, look at the source code of the SVG module in Coil and reuse some parts. Still, it’s a great learning opportunity. 🙂
v
I find it very odd that `painterResource()`/`ImageVector.vectorResource()` does exactly what you want, but only for resources. Looking at its internals you see calls to a compose internal
AndroidVectorParser
class (built up from Android's
XmlPullParser
)
1
p
Found function
androidx.compose.ui.graphics.vector.createVectorPainterFromImageVector
but it is internal. Also wonder why not to make decoding public?
m
You can’t do that without an external library on Android (unless you write that library yourself) because Android does not directly support SVG. It only supports its own simplified vector format. However, you can do that easily on all other platforms that are based on Skia via the built-in SVG support of Skia. That’s what I’ve been doing all the time and for Android I do use an external helper library.
2
p
@Michael Paus could you share external helper library name for Android?
m
Sure, its
Copy code
androidsvg = { module = "com.caverock:androidsvg-aar", version.ref = "androidsvg" }
I’ve also published my code here in this forum some time ago, if I remember correctly. My code does exactly what you want. Converts a ByteArray into an SVGPainter.
p
@Michael Paus Thanks! But i can't figure out how to make Painter using this lib. I'll appreciate any code snipped for this case.