Error trying to display a vector imageView (SVG)!

Good Guys, in this app I intend to do a test by inserting a vector image in the main activity.

I made the integration process all right, as you can see:

insert the description of the image here

insert the description of the image here

But even if you have followed the entire tutorial, the following error appears, so that you can not even run the app:

insert the description of the image here

MainActivity:

package genesysgeneration.ruleoftree;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGParser;

public class Main01Activity extends AppCompatActivity implements View.OnClickListener{

    private EditText et01, et02, et03;
    private TextView tv01, tvTest;
    private double l01, l02, l03, equalizer, lxx;
    private Button btnChange01, btnCompras, btnMoeda;
    private ImageView logo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main01);

        logo.setBackgroundColor(Color.WHITE);
        SVG svg = SVGParser.getSVGFromAsset(getResources(), R.raw.tree);
        logo.setImageDrawable(svg.createPictureDrawable());
        setContentView(logo);

        tvTest=(TextView)findViewById(R.id.tvTest);
        tv01=(TextView)findViewById(R.id.tv01);
        tvTest.setText(String.valueOf(0));
        tv01.setText(String.valueOf(0));

    }
  
}

OBS-no I transcribed all the code as you must have noticed, as there is no need, since the rest is funfando smooth.

Tutorial in question (video) = > Integrate SVG = > (site) Android Coffe

Author: ramaral, 2017-02-04

2 answers

In the latest versions of Android Studio it is possible to use SVG files without resorting to external libraries.

Right-click on top of the folder drawable and in the menu that opens, choose new- > Vector Asset.

In the Choose Local file (SVG, PSD) window, indicate the path to the file, click next and then finish in the next window.

insert the description of the image here source: android documentation

This procedure creates an xml file ( VectorDrawable ) that can be used like any other icon.


Note 1:

To use VectorDrawable in versions lower than Android 5.0 (API level 21) you need Support Library 23.2.0 and configure the build.gradle as follows:

//For Gradle Plugin 2.0+
 android {
   defaultConfig {
     vectorDrawables.useSupportLibrary = true
    }
 }

And instead of android:src you should use android:srcCompat. See documentation for information more complete.

As pointed out by @rsicarelli, from version 23.4.0 from the Support Library it is explicitly necessary to enable of the functionality by putting

static {
    AppCompatDelegate.setCompatVectorFromSourcesEnabled(true);
}

At the top of Activity.

Note 2:

In the latest versions of the Support Library (25.1.1 on this date), as long as you don't worry about the APK having a few more Kbs, you don't need to apply the one described in Note 1.
Android Studio generates a PNG by each VectorDrawable to be used in versions below 21.

Note 3

Backward compatibility is not always guaranteed.

Is post it explains in the form of a diagram what is described in the notes.

 3
Author: ramaral, 2017-02-08 12:24:08

Probably this error is because you are trying to get the file as an asset item " SVG svg = SVGParser.getSVGFromAsset (getResources (), R. raw.tree); ", but probably the assets folder has not been defined, to create it, right-click on project and choose new/folder / assets folder.

Try like this o:

//...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main01);

    logo.setBackgroundColor(Color.WHITE);
    // SVG svg = SVGParser.getSVGFromAsset(getResources(), R.raw.tree);
    // melhor voce criar uma pasta assets, copiar o arquivo para ela e usar 
    final SVG svg = SVGParser.getSVGFromAsset(getAssets(), "tree.svg");
    logo.setImageDrawable(svg.createPictureDrawable());
    //setContentView(logo);
    // desnecessario essa linha
}
//...

And it is never recommended to have the name of an activity with a number or capital letters, so if possible, rename it to another name that contains only letters.

 3
Author: Armando Marques Sobrinho, 2017-02-05 00:26:03