Error while trying to display a vector imageView (SVG)!

0

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

I made the entire integration process, as you can see:

ButeventhoughIhavefollowedthewholetutorial,thefollowingerrorappears,soIcannotevenruntheapp:

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));

    }
  
}

NOTE: I did not transcribe all the code as you should have noticed, because there is no need, since the rest is funfando smooth.

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

    
asked by anonymous 05.02.2017 / 00:51

2 answers

3

In newer versions of Android Studio, you can use SVG files without resorting to external libraries.

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

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

Source: android documentation

This 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 the Support Library 23.2.0 and configure 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 the documentation for more complete information.

As pointed out by @rsicarelli, from the version 23.4 .0 of the Support Library is explicitly required to enable functionality by placing

static {
    AppCompatDelegate.setCompatVectorFromSourcesEnabled(true);
}

at the top of the Activity.

Note 2:

In the latest versions of the Support Library (25.1.1 on this date), as long as you do not worry about APK having a few more Kbs, you do not need to apply what is described in note 1.
Android Studio generates a PNG for each VectorDrawable to be used in versions under 21.

Note 3

Backwards compatibility is not always guaranteed.

This post explains in diagram form what is described in the notes.

    
05.02.2017 / 12:31
3

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.

So try this:

//...
@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
}
//...

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

    
05.02.2017 / 01:17