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

Unfortunately, no. Not unless you somehow compile the XML source file in the way that resources are compiled. VectorDrawable currently assumes a compiled image source.

Otherwise you could probably have written 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 that fails (API level 23, Marshmallow 6.0) because the inflate call attempts to cast the attribute set to an XmlBlock.Parser, which throws a ClassCastException. The cause is documented in the source; it will “only work with compiled XML files”, such as resources packaged by the aapt tool.


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.