Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a VectorDrawable from File System (*.xml File)

I am trying to use VectorDrawables in my Android App.
I want to load an xml File from the File System and get an Instance of android.graphics.Drawable to display it in an ImageView. If i add the xml File to the Resources directory it works. But when i try to load it from the filesystem i always get a NullPointer.

I am currently trying to load the File via Drawable.createFromPath(*Path to File*) or VectorDrawable.createFromPath(*Path to File*), but i keep getting a NullPointer. The File exists and is a valid xml File (see below).

In adb log i always get :

SkImageDecoder::Factory returned null

When i use mContext.getFilesDir() the Path looks something like

/data/data/*packagename*/files/*filename*.xml

I also tried some public Folders like "Downloads". When i check the File with the File.io Api it exists(), canRead() and canWrite()

Update This ist the XML Code taken from the android Dev Pages

 <vector xmlns:android="http://schemas.android.com/apk/res/android"
 android:height="64dp"
 android:width="64dp"
 android:viewportHeight="600"
 android:viewportWidth="600" >
 <group
     android:name="rotationGroup"
     android:pivotX="300.0"
     android:pivotY="300.0"
     android:rotation="45.0" >
     <path
         android:name="v"
         android:fillColor="#000000"
         android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
 </group>

like image 920
Jay_DK Avatar asked Sep 15 '15 14:09

Jay_DK


1 Answers

Actually, yes you can, and I've done it in my own projects.

As mentioned in the previous answer though, you indeed need the compiled version of the XML drawable instead of the one you get from, say, Android Studio. AFAIK, the VectorDrawable API does not yet support loading from raw XML.

To do this, simply place your images temporarily under the res directory so that they get compiled as normal resources during the build process, then find your generated APK and extract them from there (you'll notice they're no longer text files, but binary files). Now put them again wherever you want under your assets dir and use code similar to this to load them as drawables:

XmlResourceParser parser = context.getAssets()
    .openXmlResourceParser("assets/folder/image.xml");
drawable = VectorDrawableCompat.createFromXml(context.getResources(), parser);

Notice the 'assets/' part of the path. That's required, afaik.

like image 150
n00bmind Avatar answered Oct 20 '22 01:10

n00bmind