Is it possible to create a VectorDrawable from the file system (* .xml file)

I am trying to use VectorDrawables in my android application.
I want to download the xml file from the file system and get an instance of android.graphics.Drawable in order to display it in ImageView . If I add the xml file to the resource directory, it will work. But when I try to download it from the file system, I always get NullPointer.

I'm currently trying to upload a file via Drawable.createFromPath(*Path to File*) or VectorDrawable.createFromPath(*Path to File*) , but I keep getting 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 this:

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

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

Refresh . This is XML code taken from 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> 

+5
source share
2 answers

Unfortunately not. Not unless you somehow compile the source XML file in the way resources are compiled. VectorDrawable is currently accepting a compiled image source.

Otherwise, you could probably write something like this:

 VectorDrawable d = new VectorDrawable(); try( InputStream in = new FileInputStream( *Path to File* ); ) { XmlPullParser p = Xml.newPullParser(); p.setInput( in, /*encoding, self detect*/null ); d.inflate( getResources(), p, Xml.asAttributeSet(p) ); // FAILS } 

Currently this is not possible (API level 23, Marshmallow 6.0) because the bloat call is trying to pass the attribute set to XmlBlock.Parser that throws a ClassCastException . The reason is documented in source ; it will β€œonly work with compiled XML files,” for example, resources packaged with the aapt tool.

+3
source

Actually, yes, you can, and I did this in my projects.

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

To do this, simply put your images temporarily in the res directory so that they are compiled as normal resources during the build process, then find the generated APK and extract them from there (you will notice that they are not longer text files, but binary files). Now put them again, wherever you are, in your resource directory and use code like this to load them as drawables:

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

Pay attention to the "assets /" part of the path. This is necessary, afayk.

0
source

All Articles