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

后端 未结 3 1000
你的背包
你的背包 2021-02-08 12:07

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

3条回答
  •  暖寄归人
    2021-02-08 12:40

    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.

提交回复
热议问题