0

I would like to insert a svg picture in my app so I looked for in Youtube ans I follow this tutorial :

https://www.youtube.com/watch?v=S65WtImJOvI

I create a directory by the name of raw, I download svg-android.jar and I added the following lines in my MainActivity.java :

        ImageView imageView = new ImageView(this);
        imageView.setBackgroundColor(Color.WHITE);
        SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);
        imageView.setImageDrawable(svg.createPictureDrawable());
        setContentView(imageView);

The svg picture is android.svg. But when I tried to compile and execute with an emulator it crashes ! The app does not open !

So, May I have to do something in the activity_main.xml ?

What do you think about this ?

Thank you very much !

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

3 Answers3

1

Easy,

Add it like this, and use inside of ImageView

app:srcCompat="@drawable/ic_person_outline_black_24dp"

enter image description here enter image description here

Mohamed
  • 1,251
  • 3
  • 15
  • 36
0

Default Android ImageView does not support SVG files. You can include a custom library to perform this operation.

This is a wonderfull library I've used so far : https://github.com/Pixplicity/sharp

Sangeeth Nandakumar
  • 1,362
  • 1
  • 12
  • 23
-1

The easier way is to transform your SVG into a XML vector image (using this website for example). Then you can use it almost like a standard resource drawable. For example in your xml layout, on an ImageView, you'll have to use app:srcCompat instead of android:src.

You may have to add vectorDrawables.useSupportLibrary = true in your gradle file too.

Eselfar
  • 3,759
  • 3
  • 23
  • 43
  • 1
    Please note that **SVG != VectorDrawable**. The latter derives from the former, but represent only a **limited subset** of it. – Phantômaxx May 31 '17 at 16:32
  • @Rotwang You seem to have given the same answer 2y ago to this post (https://stackoverflow.com/q/31926212/1827254), can you please develop or share a link as I'm interested to know more about that? – Eselfar May 31 '17 at 16:44
  • This isn't and it wasn't an answer. Just a comment (still valid). Commonsware answered. You can google for svg file format and see how much more complex it is in regards of VectorDrawables. – Phantômaxx May 31 '17 at 16:49
  • I've found this article (https://medium.com/@nandankaushik/android-working-with-vectordrawable-can-we-easily-convert-svg-to-vectordrawable-e4000143b13f). But if you don't use things like gradient, for most of the cases a VectorDrawable is enough and, like that, you don't need to embedded an external library to display your SVG. – Eselfar May 31 '17 at 17:02
  • Well, if you only need paths and no text, no gradients, no primitives, ... that's fine. – Phantômaxx May 31 '17 at 17:07