我试图从我的android项目中的jar文件中读取可绘制的xml文件。

private Drawable readDrawableFromJar(String p_strFilePath) {
        InputStream stream = getClass().getClassLoader().getResourceAsStream(p_strFilePath);
        XmlPullParser parser = null;
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setValidating(true);
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();
            parser.setInput(stream, "UTF-8");
        } catch (XmlPullParserException e) {
            Log.e(LOG_TAG, "there is something for " + p_strFilePath + " " + e);
        }


        Drawable drawable = null;
        try {
            if (parser != null) {
                drawable = VectorDrawableCompat.createFromXml(getContext().getResources(), parser);

               return drawable ;
            } else {
                Log.e(LOG_TAG, "parser is null for " + p_strFilePath);
            }

        } catch (XmlPullParserException e) {
            Log.e(LOG_TAG, "there is something for " + p_strFilePath + " " + e);
        } catch (IOException e) {
            Log.e(LOG_TAG, "there is something for " + p_strFilePath + " " + e);
        }
       return null;
    }

该方法可以找到文件,在文本内部读取。但是,它在设置输入时抛出以下异常:
org.xmlpull.v1.XmlPullParserException: unsupported feature: http://xmlpull.org/v1/doc/features.html#validation (position:START_DOCUMENT null@1:1)

这真的很奇怪,因为xml文件不包含这样的文本:http://xmlpull.org/v1/doc/features.html#validation
这是jar中的xml文件
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:viewportWidth="24"
    android:viewportHeight="24"
    android:width="24dp"
    android:height="24dp">
    <path
        android:pathData="M12 3C7.031 3 3 7.027 3 12.001 3 16.967 7.031 21 12 21 16.971 21 20.999 16.967 20.999 12.001 20.998 7.027 16.971 3 12 3m0 20C5.925 23 1 18.073 1 12.001 1 5.925 5.925 1 12 1 18.077 1 23 5.925 23 12.001 22.998 18.073 18.076 23 12 23"
        android:fillColor="#242838" />
    <path
        android:pathData="M13 18h-2v-7h2z"
        android:fillColor="#242838" />
    <path
        android:pathData="M10 10h3v2h-3z"
        android:fillColor="#242838" />
    <path
        android:pathData="M10 16h4v2h-4z"
        android:fillColor="#242838" />
    <path
        android:pathData="M11 6h2v2h-2z"
        android:fillColor="#242838" />
</vector>

最佳答案

您遇到的问题与您要验证的xml文件的内容无关。相反,XmlPullParserFactory.setValidating(true)行似乎是导致您异常的原因。此行的作用是配置为验证xml而创建的解析器。不幸的是,由XmlPullParserFactory.newPullParser()创建的解析器实现似乎不支持验证。注释代码中的factory.setValidating(true)行应该可以解决该问题。

10-02 06:27