Daniel Perez
Daniel Perez

Reputation: 73

Vector images imported from SVG images not being recognised in ImageView

The SVG that I was attempting to import was not correctly formatted, this lead to the xml for the vecto image not being correctly formatted either Try the following samples here as these are all correctly formatted SVGs.

I am attempting to add a SVG image into a ImageView but i keep on getting an error every time I try to call its @drawable/ value. The file seems to be in the right location since when I use a PNg image it works, but it does not get recognised when its an SVG image.

The XML to add the image is :

<ImageView
        android:id="@+id/statusImage"
        android:layout_width="304dp"
        android:layout_height="391dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/statusText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_temp" />

and the location of the image file is:

enter image description here

I have tried to modify the Gradle file so that it contains:

vectorDrawables.useSupportLibrary = true

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'

But that does not seem to solve the issue I'm having.

The error log seems to print that :

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: sc17dpc.individualproject, PID: 3467
    android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class ImageView
    Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class ImageView
    Caused by: android.content.res.Resources$NotFoundException: Drawable sc17dpc.individualproject:drawable/ic_temp with resource ID #0x7f07006d
    Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_temp.xml from drawable resource ID #0x7f07006d
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:1166)
        at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:917)
        at android.content.res.Resources.getDrawableForDensity(Resources.java:1074)
        at android.content.res.Resources.getDrawable(Resources.java:1013)
        at android.content.Context.getDrawable(Context.java:630)
        at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:463)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:203)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
        at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
        at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:59)
        at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:78)
        at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
        at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
        at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
        at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
        at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
        at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:783)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:874)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at sc17dpc.individualproject.HomeFragment.onCreateView(HomeFragment.java:23)

and points to my class HomeFragment on the line:

View view = inflater.inflate(R.layout.fragment_home, container, false);

Any help is appreciated since the solutions that I've followed currently have not solved the problem.

*********EDIT*********

The SVG file is imported using the "New -> Vector Asset" built in feature in Android Studio and by using a local file.

The image file looks like:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="640dp"
    android:height="640dp"
    android:viewportWidth="640"
    android:viewportHeight="640">
  <path

followed by a load of information about the image path.

I have attempted to change app:srcCompat to android:src and this still gives the same error as before.

Upvotes: 0

Views: 577

Answers (2)

Daniel Perez
Daniel Perez

Reputation: 73

Issue has been sumarised at the top of the article. Thanks for the help.

Upvotes: 0

sinanbasormanci
sinanbasormanci

Reputation: 46

Can you try again?

android:src="@drawable/ic_temp"

You can't use viewportHeight and viewportWidth as 640

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="640dp"
android:height="640dp">

Source VectorDrawable

Upvotes: 1

Related Questions