3

I have a string that represents a svg.

<svg height="210" width="400"><path d="M150 0 L75 200 L225 200 Z" />

How can I turn this string into a Drawable, Bitmap or similar to display it in an imageview?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

8

There are a few SVG libraries for Android you can choose. Each will do it slightly differently.

For AndroidSVG, you would do it like this:

SVG  svg = SVG.getFromString(myStringWithSvgInIt);
PictureDrawable  pd = new PictureDrawable(svg.renderToPicture());

myImageView.setImageDrawable(pd);
Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
  • thanks it works :D i have only add try catch at your answer. – Emiliano Spirito Aug 24 '18 at 07:48
  • 2
    For those who are new to Android Studio and don't know how to install AndroidSVG: add `mavenCentral()` to `ProjectRoot/build.gradle` and add `implementation 'com.caverock:androidsvg-aar:1.4'` to `ProjectRoot/app/build.gradle` as explained [here](https://bigbadaboom.github.io/androidsvg/download.html) (where `ProjectRoot` is the root directory of your project), then sync Gradle (Android Studio will suggest doing so, just click on Sync Now), and then when you type `SVG` in your Java code Android Studio will suggest importing AndroidSVG just as if it were a build-in library. – Donald Duck Aug 10 '21 at 10:52