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 displa
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.