2

I have a need to show svg files in my android app. svg-android seems like the only library that has any documentation and thus my first approach. The only example available demonstrates how to create an imageview attach an svg image and attach it to the main content view. I however want a svg file to show up on a RelativeLayout I already have defined. I attempted an implementation like so:

ImageView imageView = new ImageView(this); 
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.logo); 
imageView.setImageDrawable(svg.createPictureDrawable());
RelativeLayout home_header = (RelativeLayout) findViewById(R.id.home_header);
home_header.addView(imageView);

All appears well (no warnings/errors) but when I test the app log cat reports:

05-27 11:25:43.940: I/Adreno200-EGLSUB(28492): <ConfigWindowMatch:2078>: Format RGBA_8888.
05-27 11:25:43.950: E/(28492): Can't open file for reading
05-27 11:25:43.960: E/(28492): Can't open file for reading

I have verified the following: - File is not open in any other program - File is properly formatted

What am I missing here? Any suggestions on what might be going on?

Kevin
  • 388
  • 7
  • 23
  • UPDATE: seems to not be related to svg-android but android itself. I've got a working example with the code above on my emulator...but the issue remains on my galaxy s3. Any attempt to load and read an svg file results in a "can't open file for reading" error. – Kevin May 29 '13 at 22:27

5 Answers5

15

android:hardwareAccelerated="false" will disable hardware rendering for the whole activity. An alternative might be to just use:

imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

Which should disable it only for that View.

PS. If you are looking for an SVG library with better documentation (and more features), try mine: http://code.google.com/p/androidsvg/

Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
  • +1, that library is almost usable :) - I am trying, but lot of undesired effects –  Sep 14 '13 at 14:31
  • Is it svg-android, or AndroidSVG you are having problems with? If the latter, send me feedback and I will do what I can. – Paul LeBeau Sep 14 '13 at 14:41
  • it is yours, the other one is useless :) One of major problem is: takes relative lot time to load the svg, almost the same amount time to render to a canvas. It is device and svg dependent, but my svg lock the gui 2-5 seconds or even more. I can get ANR too. Trying to save the rendered image to SD card to load from there next time. Texts elements aren't supported well, and found some bug to in a Path or some other element, but I have changed the svg file format now. I am doing what I can first before send a feature requests list :) –  Sep 14 '13 at 15:47
  • For me didn't work "software" I have to use: imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); – OriolJ Sep 24 '13 at 23:42
  • That's weird. I don't why it had "software" there. I must have had a brain fart. Fixed it now. – Paul LeBeau Sep 25 '13 at 00:23
  • +1 for answer. However refer the link by Romian Guy. He has expained it nicely. http://android-developers.blogspot.it/2011/03/android-30-hardware-acceleration.html – Ritesh Gune Aug 03 '15 at 10:08
4

After some debugging and comparing the emulator to the native app I discovered that the "can't open file for reading" is not related to the svg files not displaying. Instead it was related to hardware acceleration. I had to set the following in my manifest

android:hardwareAccelerated="false" 

problem solved. Time wasted. Brain blown.

Kevin
  • 388
  • 7
  • 23
0

There is another option now that doesn't require android:hardwareAccelerated="false" Use this library - https://github.com/wnafee/vector-compat (api 14+)

android {
    // use version 22 or higher
    buildToolsVersion "22.0.1"
    ...
}
dependencies {
    compile 'com.wnafee:vector-compat:1.0.5'
    ...
}

And create a custom ImageView class that uses vector compat class -

public class SvgImageView extends ImageView {        
    private Drawable icon;


    public SvgImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.button_left, 0, 0);

        try {
            int resId = ta.getResourceId(R.styleable.button_left_b_icon, -1);            
            if (resId != -1) {
                icon = ResourcesCompat.getDrawable(this.getContext(), resId);

            }


        } finally {
            ta.recycle();
        }


        if (icon != null) {
            setImage(icon);
        }

    }   

    public void setImage(Drawable icon) {
        SvgImageView.this.setImageDrawable(icon);

    }



}

Vector image example -

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:width="@dimen/logo_dimen"
    android:height="@dimen/logo_dimen"
    android:viewportWidth="@integer/view_port_dimen_logo"
    android:viewportHeight="@integer/view_port_dimen_logo"

    app:vc_viewportWidth="@integer/view_port_dimen_logo"
    app:vc_viewportHeight="@integer/view_port_dimen_logo">
    <group
        android:name="rotationGroup"
        android:pivotX="0"
        android:pivotY="0"
        android:rotation="0">

        <path
            android:name="v"
            android:fillColor="@color/white"
            android:pathData="m15.5,15.6c0,-1.5 2.8,-1.9 2.8,-5c0,-1.5 -0.7,-2.6 -1.8,-3.5h1.6l1.7,-1.1h-5c-1.7,0 -3.5,0.4 -4.8,1.6c-1,0.8 -1.6,2.1 -1.6,3.4c0,2.4 1.9,4.1 4.2,4.1c0.3,0 0.5,0 0.8,0c-0.1,0.3 -0.3,0.6 -0.3,1c0,0.7 0.3,1.2 0.8,1.8c-1.6,0.1 -3.4,0.3 -4.9,1.2c-1.1,0.7 -2,1.8 -2,3.2c0,0.6 0.2,1.1 0.4,1.6c1,1.7 3.2,2.2 5,2.2c2.3,0 4.9,-0.7 6.1,-2.8c0.4,-0.6 0.6,-1.3 0.6,-2.1c0.2,-3.5 -3.6,-4 -3.6,-5.6zm-1.7,-1.2c-2.2,0 -3.2,-2.8 -3.2,-4.6c0,-0.7 0.1,-1.4 0.6,-1.9c0.4,-0.6 1.1,-0.9 1.7,-0.9c2.2,0 3.2,3 3.2,4.8c0,0.7 -0.1,1.4 -0.6,1.9c-0.4,0.4 -1.1,0.7 -1.7,0.7zm0,10.5c-1.9,0 -4.5,-0.8 -4.5,-3.2c0,-2.5 2.9,-3.1 4.9,-3.1c0.2,0 0.4,0 0.6,0c1.2,0.8 2.8,1.8 2.8,3.4c-0.1,2.2 -2,2.9 -3.8,2.9zm9.7,-10.5v-2.6h-1.3v2.6h-2.5v1.3h2.5v2.6h1.3v-2.6h2.6v-1.3h-2.6l0,0z"
            app:vc_fillColor="@color/white"
            app:vc_pathData="m15.5,15.6c0,-1.5 2.8,-1.9 2.8,-5c0,-1.5 -0.7,-2.6 -1.8,-3.5h1.6l1.7,-1.1h-5c-1.7,0 -3.5,0.4 -4.8,1.6c-1,0.8 -1.6,2.1 -1.6,3.4c0,2.4 1.9,4.1 4.2,4.1c0.3,0 0.5,0 0.8,0c-0.1,0.3 -0.3,0.6 -0.3,1c0,0.7 0.3,1.2 0.8,1.8c-1.6,0.1 -3.4,0.3 -4.9,1.2c-1.1,0.7 -2,1.8 -2,3.2c0,0.6 0.2,1.1 0.4,1.6c1,1.7 3.2,2.2 5,2.2c2.3,0 4.9,-0.7 6.1,-2.8c0.4,-0.6 0.6,-1.3 0.6,-2.1c0.2,-3.5 -3.6,-4 -3.6,-5.6zm-1.7,-1.2c-2.2,0 -3.2,-2.8 -3.2,-4.6c0,-0.7 0.1,-1.4 0.6,-1.9c0.4,-0.6 1.1,-0.9 1.7,-0.9c2.2,0 3.2,3 3.2,4.8c0,0.7 -0.1,1.4 -0.6,1.9c-0.4,0.4 -1.1,0.7 -1.7,0.7zm0,10.5c-1.9,0 -4.5,-0.8 -4.5,-3.2c0,-2.5 2.9,-3.1 4.9,-3.1c0.2,0 0.4,0 0.6,0c1.2,0.8 2.8,1.8 2.8,3.4c-0.1,2.2 -2,2.9 -3.8,2.9zm9.7,-10.5v-2.6h-1.3v2.6h-2.5v1.3h2.5v2.6h1.3v-2.6h2.6v-1.3h-2.6l0,0z" />

    </group>
</vector>

Example -

<packagename.SvgImageView     

            app:b_icon="@drawable/google_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView3" />
Mikelis Kaneps
  • 4,576
  • 2
  • 34
  • 48
0

Using androidx.appcompat.widget.AppCompatImageView instead of ImageView worked for me.

Nouman Ch
  • 4,023
  • 4
  • 29
  • 42
-2

AppCompatImageView

from package

android.support.v7.widget

Check this post, I have given all the details to use svg. As per my experience, you can use svg in Android flawlessly.

Pros:

  1. No third party library (official android support library needed)
  2. No changes in gradle file
  3. Use `android:src` for all `ImageViews` instead of 'app:srcCompat` for svg and `android:src` for other images.
  4. No need to use AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); in static block of BaseActivity.
Community
  • 1
  • 1
Harish Gyanani
  • 1,366
  • 2
  • 22
  • 43
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/14534977) – Vivek Mishra Dec 08 '16 at 13:11
  • @VivekMishra Simple use `AppCompatImageView` instead of ImageView and do nothing. All is written in this answer. the link contains the rightDrawable and leftDrawable information and how to use svg in that case. Which is not directly asked in this question. – Harish Gyanani Dec 08 '16 at 13:17