1

I'm facing an issue with SVG files. I am trying to convert an svg file, which is transferred from Android material library resource(see below) to bitmap. I use this library to do this: https://github.com/bigfishcat/svg-android.

Sag file I added is called ic_accessibility_black_24dp and it's added by going New->Vector Asset->Material Icon->Choose from Android studio. This is standard SVG, not edited in any way. Here is its xml:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
<path
    android:fillColor="#FF000000"
    android:pathData="M12,2c1.1,0 2,0.9 2,2s-0.9,2 -2,2 -2,-0.9 -2,-2 0.9,-2 2,-2zM21,9h-6v13h-2v-6h-2v6L9,22L9,9L3,9L3,7h18v2z"/>

Method I use to convert is as follows:

private Bitmap getBitmapFromSvg(int svgResId){
    SVG svgIcon = new SVGBuilder().readFromResource(getResources(), svgResId).build();
    Drawable pictureDrawable = svgIcon.getDrawable();
    return Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}

The exception is on the first line of the method, where I am trying to get SVG file.

08-17 22:14:35.795 31286-31286/com.shearwater.patientportal E/SVGAndroid: Failed to parse SVG.
                                                                      org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: not well-formed (invalid token)
                                                                          at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:515)
                                                                          at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:474)
                                                                          at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:316)
                                                                          at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279)
                                                                          at com.larvalabs.svgandroid.SVGParser.parse(SVGParser.java:79)
                                                                          at com.larvalabs.svgandroid.SVGBuilder.build(SVGBuilder.java:187)
                                                                          at 

I don't even know how to go about this, I tried googling, looked and this post, but, can't seem to be able to find a solution. Please, could anyone point me to the right direction?

Community
  • 1
  • 1
El_o.di
  • 821
  • 2
  • 11
  • 22
  • 1
    An SVG declaration would start with ` – Cruceo Aug 17 '16 at 21:51
  • Ah, so how would I go about converting it to Bitmap? Or, perhaps, what would be the format for svg file to use? How can I change this vector drawable to be compatible with the library? – El_o.di Aug 17 '16 at 22:00
  • I'm not sure _why_ you're doing this, but if that's what you want to do, all the icons that are in Android Studio are also available here: https://design.google.com/icons/ you can download the svg versions there. – Lewis McGeary Aug 17 '16 at 23:18
  • No, I don't need to convert actual icons. There is another file, which is given to me, that I need to convert. It's just that the format of this file is the same as what is in this icon. That's why I thought I might give it a go. Thank you very much for your comments. At least I know the reason now, I can at least look at it a bit more. – El_o.di Aug 18 '16 at 00:11

1 Answers1

0

Right, silly me. I was confused, because tried to find a solution with svg files rather than focusing on vector graphics. But comments to my question actually helped me a lot, I clicked now. The answer was here.

When I add local svg file to drawables, it gets .xml extension. And then I can use its id to convert it to bitmap. So there is no need to use additional library. It is helpful though to be aware of it. :-)

Community
  • 1
  • 1
El_o.di
  • 821
  • 2
  • 11
  • 22