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

Asked

Viewed 821 times

0

Good guys, in this app I intend to do a test inserting a vector image in the main Activity.

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

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

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

inserir a descrição da imagem aqui

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 - I didn’t transcribe all the code as they must have perceived, because there is no need, since the rest is function smooth.

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

2 answers

3

Probably this error is because Voce is trying to grab the file as an Assets item "SVG svg = Svgparser.getSVGFromAsset(getResources(), R.raw.Tree);",but probably the Assets folder has not been defined, to create it, right-click project and choose new/Folder/Assets Folder.

try it this way:

//...
@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 number or upper case letters, so, if possible, rename it to another name that contains only letters.

  • I’ve changed, I’ve made a mistake. it was supposed to be SVG svg = Svgparser.getSVGFromResource(getResources(), R.raw.Tree); now you can run, but the app for out of nowhere. I’ll look at the android monitor

  • gave an exception to what you told me to do, I’ll put it

  • 1

    if you know English, go here ó: http://blog.fordemobile.com/2013/01/display-svg-inside-imageview.html

3


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

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

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

inserir a descrição da imagem aqui Source: android documentation

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


Note 1:

To use Vectordrawable in lower versions 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 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 it is necessary to explicitly do the enable of functionality by putting

static {
    AppCompatDelegate.setCompatVectorFromSourcesEnabled(true);
}

at the top of Activity.

Note 2:

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

Note 3

Retro-compatibility is not always guaranteed.

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

  • 1

    From the version 23.4.0 you need to add the list AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); at the top of her Activity to support API <21. Ref https://goo.gl/HL8wVw

  • 2

    @rsicarelli A documentation/guide does not refer to this, however there is such indication in the Revision 23.4.0. I didn’t realize if setCompatVectorFromResourcesEnabled(true) replaces vectorDrawables.useSupportLibrary = true or if it is necessary to use both.

  • The documentation really does not make this clear, but the two methods are necessary.

  • Thank you guys!!! Now when I try to put this SVG as logo of the app appears a picture anything to see (native android)... I could settle here or I should ask a new question since my question has already been resolved?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.